Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用提升的权限运行_Asp.net_Vb.net_Powershell - Fatal编程技术网

Asp.net 使用提升的权限运行

Asp.net 使用提升的权限运行,asp.net,vb.net,powershell,Asp.net,Vb.net,Powershell,我试图调用powershell脚本,该脚本通过vb.net在asp.net应用程序中创建打印机。我正在使用中给出的函数。通过VisualStudio它可以正常工作,但在发布应用程序后,脚本运行时会出现拒绝访问错误。我发现这是因为脚本需要提升权限才能在目标服务器上创建打印机,而我似乎找不到一种有效的方法。如果您有任何建议,我将不胜感激 打印机脚本: # Test Printserver Server Running $test = Get-Service -ComputerName $Server

我试图调用powershell脚本,该脚本通过vb.net在asp.net应用程序中创建打印机。我正在使用中给出的函数。通过VisualStudio它可以正常工作,但在发布应用程序后,脚本运行时会出现拒绝访问错误。我发现这是因为脚本需要提升权限才能在目标服务器上创建打印机,而我似乎找不到一种有效的方法。如果您有任何建议,我将不胜感激

打印机脚本:

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

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

Start-Process PowerShell -arg <path-to-script> -Credential $cred
启动进程PowerShell-arg-凭证$cred

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

您可以向我们展示用于调用脚本的VB.NET代码吗?