【问题标题】:C# Powershell Execution Error (Script runs fine in Powershell)C# Powershell 执行错误(脚本在 Powershell 中运行良好)
【发布时间】:2017-03-04 18:07:43
【问题描述】:

问题的快速概要:我有一个从网上下载的 Powershell 脚本,目的是允许我提供一个名为“computername”的参数,其值为逗号分隔的 IP 地址,以在远程机器上运行 netstat 并返回值。当我从 Powershell 控制台执行此脚本时,它运行完美。然后我下载了为 C# 执行 powershell 脚本的示例并让它在我的本地机器上运行,但是当我添加计算机名参数和提供地址列表时,它失败并出现 RPC 服务器不可用错误。以下是 Powershell 脚本本身:

        <#
        .SYNOPSIS
            Display current TCP/IP connections for local or remote system

        .FUNCTIONALITY
            Computers

        .DESCRIPTION
            Display current TCP/IP connections for local or remote system.  Includes the process ID (PID) and process name for each connection.
            If the port is not yet established, the port number is shown as an asterisk (*).    

        .PARAMETER ProcessName
            Gets connections by the name of the process. The default value is '*'.

        .PARAMETER Port
            The port number of the local computer or remote computer. The default value is '*'.

        .PARAMETER Address
            Gets connections by the IP address of the connection, local or remote. Wildcard is supported. The default value is '*'.

        .PARAMETER Protocol
            The name of the protocol (TCP or UDP). The default value is '*' (all)

        .PARAMETER State
            Indicates the state of a TCP connection. The possible states are as follows:

            Closed       - The TCP connection is closed. 
            Close_Wait   - The local endpoint of the TCP connection is waiting for a connection termination request from the local user. 
            Closing      - The local endpoint of the TCP connection is waiting for an acknowledgement of the connection termination request sent previously. 
            Delete_Tcb   - The transmission control buffer (TCB) for the TCP connection is being deleted. 
            Established  - The TCP handshake is complete. The connection has been established and data can be sent. 
            Fin_Wait_1   - The local endpoint of the TCP connection is waiting for a connection termination request from the remote endpoint or for an acknowledgement of the connection termination request sent previously. 
            Fin_Wait_2   - The local endpoint of the TCP connection is waiting for a connection termination request from the remote endpoint. 
            Last_Ack     - The local endpoint of the TCP connection is waiting for the final acknowledgement of the connection termination request sent previously. 
            Listen       - The local endpoint of the TCP connection is listening for a connection request from any remote endpoint. 
            Syn_Received - The local endpoint of the TCP connection has sent and received a connection request and is waiting for an acknowledgment. 
            Syn_Sent     - The local endpoint of the TCP connection has sent the remote endpoint a segment header with the synchronize (SYN) control bit set and is waiting for a matching connection request. 
            Time_Wait    - The local endpoint of the TCP connection is waiting for enough time to pass to ensure that the remote endpoint received the acknowledgement of its connection termination request. 
            Unknown      - The TCP connection state is unknown.

            Values are based on the TcpState Enumeration:
            http://msdn.microsoft.com/en-us/library/system.net.networkinformation.tcpstate%28VS.85%29.aspx

            Cookie Monster - modified these to match netstat output per here:
            http://support.microsoft.com/kb/137984

        .PARAMETER ComputerName
            If defined, run this command on a remote system via WMI.  \\computername\c$\netstat.txt is created on that system and the results returned here

        .PARAMETER ShowHostNames
            If specified, will attempt to resolve local and remote addresses.

        .PARAMETER tempFile
            Temporary file to store results on remote system.  Must be relative to remote system (not a file share).  Default is "C:\netstat.txt"

        .PARAMETER AddressFamily
            Filter by IP Address family: IPv4, IPv6, or the default, * (both).

            If specified, we display any result where both the localaddress and the remoteaddress is in the address family.

        .EXAMPLE
            Get-NetworkStatistics | Format-Table

        .EXAMPLE
            Get-NetworkStatistics iexplore -computername k-it-thin-02 -ShowHostNames | Format-Table

        .EXAMPLE
            Get-NetworkStatistics -ProcessName md* -Protocol tcp

        .EXAMPLE
            Get-NetworkStatistics -Address 192* -State LISTENING

        .EXAMPLE
            Get-NetworkStatistics -State LISTENING -Protocol tcp

        .EXAMPLE
            Get-NetworkStatistics -Computername Computer1, Computer2

        .EXAMPLE
            'Computer1', 'Computer2' | Get-NetworkStatistics

        .OUTPUTS
            System.Management.Automation.PSObject

        .NOTES
            Author: Shay Levy, code butchered by Cookie Monster
            Shay's Blog: http://PowerShay.com
            Cookie Monster's Blog: http://ramblingcookiemonster.github.io/

        .LINK
            http://gallery.technet.microsoft.com/scriptcenter/Get-NetworkStatistics-66057d71
        #>  
        [OutputType('System.Management.Automation.PSObject')]
        [CmdletBinding()]
        param(

            [Parameter(Position=0)]
            [System.String]$ProcessName='*',

            [Parameter(Position=1)]
            [System.String]$Address='*',        

            [Parameter(Position=2)]
            $Port='*',

            [Parameter(Position=3,
                       ValueFromPipeline = $True,
                       ValueFromPipelineByPropertyName = $True)]
            [System.String[]]$ComputerName=$env:COMPUTERNAME,

            [ValidateSet('*','tcp','udp')]
            [System.String]$Protocol='*',

            [ValidateSet('*','Closed','Close_Wait','Closing','Delete_Tcb','DeleteTcb','Established','Fin_Wait_1','Fin_Wait_2','Last_Ack','Listening','Syn_Received','Syn_Sent','Time_Wait','Unknown')]
            [System.String]$State='*',

            [switch]$ShowHostnames,

            [switch]$ShowProcessNames = $true,  

            [System.String]$TempFile = "C:\netstat.txt",

            [validateset('*','IPv4','IPv6')]
            [string]$AddressFamily = '*'
        )

        begin{
            #Define properties
                $properties = 'ComputerName','Protocol','LocalAddress','LocalPort','RemoteAddress','RemotePort','State','ProcessName','PID'

            #store hostnames in array for quick lookup
                $dnsCache = @{}

        }

        process{

            foreach($Computer in $ComputerName) {

                #Collect processes
                if($ShowProcessNames){
                    Try {
                        $processes = Get-Process -ComputerName $Computer -ErrorAction stop | select name, id
                    }
                    Catch {
                        Write-warning "Could not run Get-Process -computername $Computer.  Verify permissions and connectivity.  Defaulting to no ShowProcessNames"
                        $ShowProcessNames = $false
                    }
                }

                #Handle remote systems
                    if($Computer -ne $env:COMPUTERNAME){

                        #define command
                            [string]$cmd = "cmd /c c:\windows\system32\netstat.exe -ano >> $tempFile"

                        #define remote file path - computername, drive, folder path
                            $remoteTempFile = "\\{0}\{1}`${2}" -f "$Computer", (split-path $tempFile -qualifier).TrimEnd(":"), (Split-Path $tempFile -noqualifier)

                        #delete previous results
                            Try{
                                $null = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList "cmd /c del $tempFile" -ComputerName $Computer -ErrorAction stop
                            }
                            Catch{
                                Write-Warning "Could not invoke create win32_process on $Computer to delete $tempfile"
                            }

                        #run command
                            Try{
                                $processID = (Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName $Computer -ErrorAction stop).processid
                            }
                            Catch{
                                #If we didn't run netstat, break everything off
                                Throw $_
                                Break
                            }

                        #wait for process to complete
                            while (
                                #This while should return true until the process completes
                                    $(
                                        try{
                                            get-process -id $processid -computername $Computer -ErrorAction Stop
                                        }
                                        catch{
                                            $FALSE
                                        }
                                    )
                            ) {
                                start-sleep -seconds 2 
                            }

                        #gather results
                            if(test-path $remoteTempFile){

                                Try {
                                    $results = Get-Content $remoteTempFile | Select-String -Pattern '\s+(TCP|UDP)'
                                }
                                Catch {
                                    Throw "Could not get content from $remoteTempFile for results"
                                    Break
                                }

                                Remove-Item $remoteTempFile -force

                            }
                            else{
                                Throw "'$tempFile' on $Computer converted to '$remoteTempFile'.  This path is not accessible from your system."
                                Break
                            }
                    }
                    else{
                        #gather results on local PC
                            $results = netstat -ano | Select-String -Pattern '\s+(TCP|UDP)'
                    }

                #initialize counter for progress
                    $totalCount = $results.count
                    $count = 0

                #Loop through each line of results    
                    foreach($result in $results) {

                        $item = $result.line.split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)

                        if($item[1] -notmatch '^\[::'){

                            #parse the netstat line for local address and port
                                if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6'){
                                    $localAddress = $la.IPAddressToString
                                    $localPort = $item[1].split('\]:')[-1]
                                }
                                else {
                                    $localAddress = $item[1].split(':')[0]
                                    $localPort = $item[1].split(':')[-1]
                                }

                            #parse the netstat line for remote address and port
                                if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6'){
                                    $remoteAddress = $ra.IPAddressToString
                                    $remotePort = $item[2].split('\]:')[-1]
                                }
                                else {
                                    $remoteAddress = $item[2].split(':')[0]
                                    $remotePort = $item[2].split(':')[-1]
                                }

                            #Filter IPv4/IPv6 if specified
                                if($AddressFamily -ne "*")
                                {
                                    if($AddressFamily -eq 'IPv4' -and $localAddress -match ':' -and $remoteAddress -match ':|\*' )
                                    {
                                        #Both are IPv6, or ipv6 and listening, skip
                                        Write-Verbose "Filtered by AddressFamily:`n$result"
                                        continue
                                    }
                                    elseif($AddressFamily -eq 'IPv6' -and $localAddress -notmatch ':' -and ( $remoteAddress -notmatch ':' -or $remoteAddress -match '*' ) )
                                    {
                                        #Both are IPv4, or ipv4 and listening, skip
                                        Write-Verbose "Filtered by AddressFamily:`n$result"
                                        continue
                                    }
                                }

                            #parse the netstat line for other properties
                                $procId = $item[-1]
                                $proto = $item[0]
                                $status = if($item[0] -eq 'tcp') {$item[3]} else {$null}    

                            #Filter the object
                                if($remotePort -notlike $Port -and $localPort -notlike $Port){
                                    write-verbose "remote $Remoteport local $localport port $port"
                                    Write-Verbose "Filtered by Port:`n$result"
                                    continue
                                }

                                if($remoteAddress -notlike $Address -and $localAddress -notlike $Address){
                                    Write-Verbose "Filtered by Address:`n$result"
                                    continue
                                }

                                if($status -notlike $State){
                                    Write-Verbose "Filtered by State:`n$result"
                                    continue
                                }

                                if($proto -notlike $Protocol){
                                    Write-Verbose "Filtered by Protocol:`n$result"
                                    continue
                                }

                            #Display progress bar prior to getting process name or host name
                                Write-Progress  -Activity "Resolving host and process names"`
                                    -Status "Resolving process ID $procId with remote address $remoteAddress and local address $localAddress"`
                                    -PercentComplete (( $count / $totalCount ) * 100)

                            #If we are running showprocessnames, get the matching name
                                if($ShowProcessNames -or $PSBoundParameters.ContainsKey -eq 'ProcessName'){

                                    #handle case where process spun up in the time between running get-process and running netstat
                                    if($procName = $processes | Where {$_.id -eq $procId} | select -ExpandProperty name ){ }
                                    else {$procName = "Unknown"}

                                }
                                else{$procName = "NA"}

                                if($procName -notlike $ProcessName){
                                    Write-Verbose "Filtered by ProcessName:`n$result"
                                    continue
                                }

                            #if the showhostnames switch is specified, try to map IP to hostname
                                if($showHostnames){
                                    $tmpAddress = $null
                                    try{
                                        if($remoteAddress -eq "127.0.0.1" -or $remoteAddress -eq "0.0.0.0"){
                                            $remoteAddress = $Computer
                                        }
                                        elseif($remoteAddress -match "\w"){

                                            #check with dns cache first
                                                if ($dnsCache.containskey( $remoteAddress)) {
                                                    $remoteAddress = $dnsCache[$remoteAddress]
                                                    write-verbose "using cached REMOTE '$remoteAddress'"
                                                }
                                                else{
                                                    #if address isn't in the cache, resolve it and add it
                                                        $tmpAddress = $remoteAddress
                                                        $remoteAddress = [System.Net.DNS]::GetHostByAddress("$remoteAddress").hostname
                                                        $dnsCache.add($tmpAddress, $remoteAddress)
                                                        write-verbose "using non cached REMOTE '$remoteAddress`t$tmpAddress"
                                                }
                                        }
                                    }
                                    catch{ }

                                    try{

                                        if($localAddress -eq "127.0.0.1" -or $localAddress -eq "0.0.0.0"){
                                            $localAddress = $Computer
                                        }
                                        elseif($localAddress -match "\w"){
                                            #check with dns cache first
                                                if($dnsCache.containskey($localAddress)){
                                                    $localAddress = $dnsCache[$localAddress]
                                                    write-verbose "using cached LOCAL '$localAddress'"
                                                }
                                                else{
                                                    #if address isn't in the cache, resolve it and add it
                                                        $tmpAddress = $localAddress
                                                        $localAddress = [System.Net.DNS]::GetHostByAddress("$localAddress").hostname
                                                        $dnsCache.add($localAddress, $tmpAddress)
                                                        write-verbose "using non cached LOCAL '$localAddress'`t'$tmpAddress'"
                                                }
                                        }
                                    }
                                    catch{ }
                                }

                            #Write the object   
                                New-Object -TypeName PSObject -Property @{
                                    ComputerName = $Computer
                                    PID = $procId
                                    ProcessName = $procName
                                    Protocol = $proto
                                    LocalAddress = $localAddress
                                    LocalPort = $localPort
                                    RemoteAddress =$remoteAddress
                                    RemotePort = $remotePort
                                    State = $status
                                } | Select-Object -Property $properties                             

                            #Increment the progress counter
                                $count++
                        }
                    }
            }
        }

这是我提供给上述脚本的 Powershell 命令(名为 Get-NetworkStatistics2.ps1)

    PS C:\Scripts> C:\Scripts\Get-NetworkStatistics2.ps1 -AddressFamily IPv4 -ComputerName 10.66.64.43, 10.66.41.69, 10.66.41.70, 10.66.41.71, 10.66.44.92, 10.27.40.105, 10.66.40.20, 10.66.40.182, 10.66.64.46, 10.66.64.45, 10.27.40.106, 10.40.21.249, 10.40.21.195, 10.40.21.225, 10.40.21.213, 10.40.21.221, 10.66.44.60, 10.66.62.65, 10.66.62.66 | Format-Table -autosize >> "C:\scripts\201703041042.txt"

当我执行上面的命令时,它运行完美,并将结果通过管道传输到命令末尾指示的文件。

这里是创建 exe 以运行相同位的 C# 代码:

    public void ExecuteSynchronously()
    {
        string sTab = "\t";  //make a tab character for file line formatting

        //this will be my output file
        TextWriter oTxt = File.AppendText(@"C:\Clients\FOO\NetStats\PSDumps.txt");

        //here we read the script in from a file
        string sScript;
        TextReader oScript = File.OpenText(@"C:\Clients\FOO\Scripts\Get-NetworkStatistics2.ps1");
        sScript = oScript.ReadToEnd();
        //textreader cleanup
        oScript.Close();
        oScript.Dispose();
        oScript = null;

        using (PowerShell PowerShellInstance = PowerShell.Create())
        {

            // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
            // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
            PowerShellInstance.AddScript(sScript); //add my script

            //add the list of computers
            PowerShellInstance.AddParameter("computername", "10.66.64.43, 10.66.41.69, 10.66.41.70, 10.66.41.71, 10.66.44.92, 10.27.40.105, 10.66.40.20, 10.66.40.182, 10.66.64.46, 10.66.64.45, 10.27.40.106, 10.40.21.249, 10.40.21.195, 10.40.21.225, 10.40.21.213, 10.40.21.221, 10.66.44.60, 10.66.62.65, 10.66.62.66");

            //add the flag to say I am not interested in IPv6
            PowerShellInstance.AddParameter("AddressFamily", "IPv4");

            // invoke execution on the pipeline (collecting output)
            var PSOutput = PowerShellInstance.Invoke();

            // check the other output streams (for example, the error stream)
            if (PowerShellInstance.Streams.Error.Count > 0)
            {
                //not done this yet : )
                // error records were written to the error stream.
                // do something with the items found.
            }

            string sLine = "";

            //// loop through each output object item and build my own tab delimed file...
            foreach (PSObject outputItem in PSOutput)
            {
                foreach (PSNoteProperty prop in outputItem.Properties)
                {
                    if (prop.Value == null)
                    {
                        sLine += "" + sTab;
                    }
                    else
                    {
                        sLine += prop.Value.ToString() + sTab;
                    }
                }

                sLine = sLine.Trim();
                oTxt.WriteLine(sLine);
                sLine = "";

            }
            //textwriter cleanup
            oTxt.Close();
            oTxt.Dispose();
            oTxt = null;
        }
    }

最后...这是 C# 可执行文件生成的错误消息。

    Unhandled Exception: System.Management.Automation.RuntimeException: The RPC serv
    er is unavailable. (Exception from HRESULT: 0x800706BA) ---> System.Runtime.Inte
    ropServices.COMException: The RPC server is unavailable. (Exception from HRESULT
    : 0x800706BA)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 e
    rrorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode
    )
       at System.Management.ManagementScope.InitializeGuts(Object o)
       at System.Management.ManagementScope.Initialize()
       at System.Management.ManagementObject.Initialize(Boolean getObject)
       at System.Management.ManagementObject.GetMethodParameters(String methodName,
    ManagementBaseObject& inParameters, IWbemClassObjectFreeThreaded& inParametersCl
    ass, IWbemClassObjectFreeThreaded& outParametersClass)
       at System.Management.ManagementObject.GetMethodParameters(String methodName)
       at Microsoft.PowerShell.Commands.InvokeWmiMethod.ProcessRecord()
       --- End of inner exception stack trace ---
       at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable inp
    ut)
       at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(
    Runspace rs, Boolean performSyncInvoke)
       at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDo
    Work(Runspace rsToUse, Boolean isSync)
       at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](P
    SDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings setting
    s)
       at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataC
    ollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
       at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable in
    put, PSDataCollection`1 output, PSInvocationSettings settings)
       at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvoca
    tionSettings settings)
       at System.Management.Automation.PowerShell.Invoke()
       at PowerShellExecutionSample.PowerShellExecutor.ExecuteSynchronously()
       at PowerShellExecutionSample.Program.Main(String[] args)        

任何人都可以在我尝试使用我的 C# exe 运行它的同一个盒子上的 Powershell 中解释它是如何工作的吗?

我应该注意,我制作了一个只在本地机器上运行 netstat 的版本,它运行良好并且生成了完美的文件。

非常感谢您提供的任何帮助。

【问题讨论】:

  • 参数 -ComputerName 在 PowerShell 中被声明为 [System.String[]],因此请尝试将 string[] 参数传递给 .AddParameter("computername", ...) 而不是 单个 字符串。
  • 非常感谢您的建议。我更改了代码以将其生成为数组,但得到了相同的结果。我真的要放弃了。我发现了一些关于 .NET 4.0 存在问题的信息,但我可以让它在更早的任何东西上运行,因为 NUGET Powershell 不会在早期版本中安装。有趣的是,system.management.automation.dll 似乎是 .net 2.0 hmmm....
  • 我鼓励您将其缩减为 MCVE (Minimal, Complete, and Verifiable Example)。在这样做的过程中,您可能会自己发现解决方案;如果没有,其他人会发现帮助你更容易。
  • @mklement0 我想让你知道,我已经连续坐在这把椅子上将近 24 个小时来解决这个问题。您的建议,再加上您之前的建议,解决了我的问题。它与数组有关。如果我在其中传递一个 IP,它就可以正常工作。所以那里有些混乱。再次感谢。如果您将其作为答案,我会将其标记为已解决。祝先生周末愉快。

标签: c# powershell


【解决方案1】:

真的,我欠@mklement0 的道具。他鼓励我采取更简单的方法来隔离问题,并且很有效。

正如我在上面的问题中提到的,有一个名为“ComputerName”的参数。使用逗号分隔的值字符串(即使使用逗号 + 空格)从 Powershell ISE 运行脚本效果很好。当我把它带到 C# 程序运行时,它并不好。所以我接受了@mklement 的建议并插入了一个远程 IP,它工作得很好。那时我知道问题出在这个领域。我将 IP 地址字符串转换为数组,将该数组用作 ComputerName 参数值,瞧。这是工作。我是 Powershell 的新手,在 C# 方面也不是很好。感谢您的帮助和阅读它的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2020-11-12
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多