Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Powershell启动进程-远程脚本块中的等待参数失败_Powershell_Remoting_Invoke Command_Start Process - Fatal编程技术网

Powershell启动进程-远程脚本块中的等待参数失败

Powershell启动进程-远程脚本块中的等待参数失败,powershell,remoting,invoke-command,start-process,Powershell,Remoting,Invoke Command,Start Process,下面是我想做的一个例子: Invoke-Command [Connection Info] -ScriptBlock { param ( [various parameters] ) Start-Process [some .exe] -Wait } -ArgumentList [various parameters] 它可以很好地连接到另一台机器,并且可以很好地启动流程。问题在于,它不会等到流程完成后再继续。这导致了一些问题。有什么想法吗 快速编辑:为

下面是我想做的一个例子:

Invoke-Command [Connection Info] -ScriptBlock {
    param (
        [various parameters]
    )
    Start-Process [some .exe] -Wait
} -ArgumentList [various parameters]
它可以很好地连接到另一台机器,并且可以很好地启动流程。问题在于,它不会等到流程完成后再继续。这导致了一些问题。有什么想法吗


快速编辑:为什么远程运行进程时-Wait参数会失败?

我以前遇到过一次,IIRC,解决方法是:

Invoke-Command [Connection Info] -ScriptBlock {
    param (
        [various parameters]
    )
    $process = Start-Process [some .exe] -Wait -Passthru

    do {Start-Sleep -Seconds 1 }
     until ($Process.HasExited)

} -ArgumentList [various parameters]

我以前遇到过一次,IIRC的解决方法是:

Invoke-Command [Connection Info] -ScriptBlock {
    param (
        [various parameters]
    )
    $process = Start-Process [some .exe] -Wait -Passthru

    do {Start-Sleep -Seconds 1 }
     until ($Process.HasExited)

} -ArgumentList [various parameters]

您还可以使用System.Diagnostics.Process类来解决此问题。如果您不关心输出,您可以使用:

Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.WaitForExit()
}
如果你真的在意,你可以做以下类似的事情:

Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.StandardOutput.ReadToEnd()
}

这将等待进程完成,然后返回标准输出流。

您也可以使用System.Diagnostics.process类解决此问题。如果您不关心输出,您可以使用:

Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.WaitForExit()
}
如果你真的在意,你可以做以下类似的事情:

Invoke-Command [Connection Info] -ScriptBlock {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = "powershell.exe"
$psi.Arguments = "dir c:\windows\fonts"
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$proc = [System.Diagnostics.Process]::Start($psi)
$proc.StandardOutput.ReadToEnd()
}

这将等待进程完成,然后返回标准输出流。

这是Powershell版本3的问题,而不是版本2的问题,在该版本中,-wait可以正常工作

在Powershell 3中,。WaitForExit()为我提供了窍门:

$p = Start-Process [some .exe] -Wait -Passthru
$p.WaitForExit()
if ($p.ExitCode -ne 0) {
    throw "failed"
}

只需开始睡眠,直到退出-不设置。退出代码,通常最好知道.exe是如何完成的。

这是Powershell版本3的问题,而不是版本2的问题,在该版本中,-Wait正常工作

在Powershell 3中,。WaitForExit()为我提供了窍门:

$p = Start-Process [some .exe] -Wait -Passthru
$p.WaitForExit()
if ($p.ExitCode -ne 0) {
    throw "failed"
}

只需开始睡眠,直到退出-不设置。退出代码,了解.exe是如何完成的通常是很好的。

此解决方案的问题是$process.ExitCode没有设置此解决方案的问题是$process.ExitCode没有设置为Powershell版本3的问题,而不是版本2的问题。似乎是Powershell版本3的问题,而不是版本2的问题。