如何使PowerShell等待调用项完成? [英] How to get PowerShell to wait for Invoke-Item completion?

查看:192
本文介绍了如何使PowerShell等待调用项完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让PowerShell等待Invoke-Item调用完成?我正在调用不可执行的项目,因此我需要使用Invoke-Item来打开它.

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.

推荐答案

不幸的是,您不能直接使用Invoke-Item Commandlet.此命令let具有无效的返回类型,并且没有允许等待的选项.

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.

最好的选择是定义自己的函数,像这样包装Process API

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

这篇关于如何使PowerShell等待调用项完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆