2013-03-08 70 views
0

我想调用一个powershell脚本,通过vb.net在asp.net应用程序中创建打印机。我正在使用this tutorial中给出的函数。它通过Visual Studio工作正常,但是在我发布应用程序后,运行脚本时出现Access拒绝错误。我发现这是因为脚本需要提高privilages来创建目标服务器上的打印机,我似乎无法找到一种有效的方法。我将不胜感激任何建议。以高权限运行

打印机脚本:

# Test Printserver Server Running 
$test = Get-Service -ComputerName $Server -Name Spooler | Select-Object -Property 'Status' 
If ($test.status -ne 'Running') { 
Out-Default -InputObject 'The Server You Requested is Unavailable' 
Exit 
} 

# Test Printer Exist 
$Test = get-wmiobject -class "Win32_Printer" -namespace "root\CIMV2" -computername $Server -Filter ("name = '$name'") 
If ($test -ne $null) { 
Out-Default -InputObject 'A Printer by This Name Already Exists!' 
Exit 
} 

# Test Printer Port Exist 
$Test = get-wmiobject -class "Win32_TCPIPPrinterPort" -namespace "root\CIMV2" -computername $Server -Filter ("hostaddress = '$Address'") 
If ($test -ne $null) { 
Out-Default -InputObject 'A Printer With This IP Address Already Exists!' 
#Exit 
} 


# Generate Port Name 
$Portname = 'IP_' + $address 


# Create Port 
$port = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_TCPIPPrinterPort").createInstance() 
$port.Name= $Portname 
$port.SNMPEnabled=$false 
$port.Protocol=1 
$port.HostAddress= $address 
$site = get-spsite "http://localhost/nonfarmadminsitecollection"; 
$port.Put() 

# Create Printer 
$print = ([WMICLASS]"\\$server\ROOT\cimv2:Win32_Printer").createInstance() 
$print.drivername = $Driver 
$print.PortName = $Portname 
$print.Shared = $true 
$print.Sharename = $Name 
$print.Location = $location 
$print.DeviceID = $name 
$print.Put() 

VB PowerShell函数

' helper method that takes your script path, loads up the script 
' into a variable, and passes the variable to the RunScript method 
' that will then execute the contents 
Shared Function LoadScript(ByVal filename As String) As String 

    Try 

     ' Create an instance of StreamReader to read from our file. 
     ' The using statement also closes the StreamReader. 
     Dim sr As New StreamReader(filename) 

     ' use a string builder to get all our lines from the file 
     Dim fileContents As New StringBuilder() 

     ' string to hold the current line 
     Dim curLine As String = "" 


     ' loop through our file and read each line into our 
     ' stringbuilder as we go along 
     Do 
      ' read each line and MAKE SURE YOU ADD BACK THE 
      ' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF 
      curLine = sr.ReadLine() 
      fileContents.Append(curLine + vbCrLf) 
     Loop Until curLine Is Nothing 

     ' close our reader now that we are done 
     sr.Close() 


     ' call RunScript and pass in our file contents 
     ' converted to a string 
     Return fileContents.ToString() 


    Catch e As Exception 
     ' Let the user know what went wrong. 
     Dim errorText As String = "The file could not be read:" 
     errorText += e.Message + "\n" 
     Return errorText 
    End Try 

End Function 


' Takes script text as input and runs it, then converts 
' the results to a string to return to the user 
Shared Function RunScript(ByVal scriptText As String) As String 

    ' create Powershell runspace 
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace() 

    ' open it 
    MyRunSpace.Open() 

    ' create a pipeline and feed it the script text 
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline() 

    MyPipeline.Commands.AddScript(scriptText) 

    ' add an extra command to transform the script output objects into nicely formatted strings 
    ' remove this line to get the actual objects that the script returns. For example, the script 
    ' "Get-Process" returns a collection of System.Diagnostics.Process instances. 
    ' MyPipeline.Commands.Add("Out-String") 

    ' execute the script 
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke() 

    ' close the runspace 
    MyRunSpace.Close() 

    ' convert the script result into a single string 
    Dim MyStringBuilder As New StringBuilder() 

    For Each obj As PSObject In results 
     MyStringBuilder.AppendLine(obj.ToString()) 
    Next 

    ' return the results of the script that has 
    ' now been converted to text 
    Return MyStringBuilder.ToString() 
    ' Return results.ToString 

End Function 

调用PowerShell的

Dim script As String = Global_asax.LoadScript("path")  
Global_asax.RunScript(script) 
+0

你能告诉我们你用来调用脚本的VB.NET代码吗? – 2013-03-08 16:33:02

回答

0

不能更改IIS的海拔状态。您可以启动另一个进程,如提升PowerShell.exe以运行需要提升高度的脚本部分,例如

Start-Process PowerShell -arg <path-to-script> -Credential $cred 

您将需要一个具有所需权限的凭据。另外,如果你在C#中这样做,那么你也可以使用System.Diagnostic.Process.Start API。