0

我正在尝试通过 vb.net 调用一个在 asp.net 应用程序中创建打印机的 powershell 脚本。我正在使用本教程中给出的函数。它在 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)
4

1 回答 1

0

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

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

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

于 2013-03-08T17:17:22.633 回答