使用提升的权限运行

时间:2013-03-08 16:31:34

标签: asp.net vb.net powershell

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

打印机脚本:

# 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)

1 个答案:

答案 0 :(得分:0)

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

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

您需要具有所需权限的人的凭据。此外,如果您在C#中执行此操作,那么您也可以使用System.Diagnostic.Process.Start API。