How to get PowerShell to wait for Invoke-Item comp

2019-04-04 06:59发布

How do I get PowerShell to wait until the Invoke-Item call has finished? I'm invoking a non-executable item, so I need to use Invoke-Item to open it.

4条回答
三岁会撩人
2楼-- · 2019-04-04 07:43

Pipe your command to Out-Null.

查看更多
Viruses.
3楼-- · 2019-04-04 07:46

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();
    }
}
查看更多
Luminary・发光体
4楼-- · 2019-04-04 07:50

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.

查看更多
神经病院院长
5楼-- · 2019-04-04 08:02

One easy way

$session = New-PSSession -ComputerName "xxxxx" -Name "mySession"
$Job = Invoke-Command -Session $session -FilePath "xxxxx" -AsJob
Wait-Job -Job $Job
查看更多
登录 后发表回答