How to get PowerShell to wait for Invoke-Item completion?

≯℡__Kan透↙ 提交于 2019-12-03 22:58:57

Unfortunately you can't by using the Invoke-Item Commandlet directly. This command let has a void return type and no options that allow for a wait.

The best option available is to define your own function which wraps the Process API like so

function Invoke-Command() {
    param ( [string]$program = $(throw "Please specify a program" ),
            [string]$argumentString = "",
            [switch]$waitForExit )

    $psi = new-object "Diagnostics.ProcessStartInfo"
    $psi.FileName = $program 
    $psi.Arguments = $argumentString
    $proc = [Diagnostics.Process]::Start($psi)
    if ( $waitForExit ) {
        $proc.WaitForExit();
    }
}
stej

Just use Start-Process -wait, for example Start-Process -wait c:\image.jpg. That should work in the same way as the one by @JaredPar.

Pipe your command to Out-Null.

One easy way

$session = New-PSSession -ComputerName "xxxxx" -Name "mySession"
$Job = Invoke-Command -Session $session -FilePath "xxxxx" -AsJob
Wait-Job -Job $Job
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!