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

后端 未结 4 2187
萌比男神i
萌比男神i 2021-02-18 22:49

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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 23:55

    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();
        }
    }
    

提交回复
热议问题