2013-03-08 42 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。