使用包含标志的值数组(splatting)调用cmdlet

Phi*_*ght 1 arrays powershell

我正在调用start-process命令传递一组值...

$args = @{
    FilePath = "c:\myapp.exe"
    RedirectStandardOutput = "c:\output.txt"
};

start-process @args;
Run Code Online (Sandbox Code Playgroud)

...这工作正常,但我现在想要将Wait标志添加到start-process的参数中,但是当它不需要赋值给它时,如何将它添加到$ args数组中...

$args = @{
    Wait = ?
    FilePath = "c:\myapp.exe"
    RedirectStandardOutput = "c:\output.txt"
};
Run Code Online (Sandbox Code Playgroud)

我想坚持将参数作为数组传递,因为它们可以传递给一个例程,这个参数很适合我参与进程.

Mat*_*att 5

没有最好的参考,但如果你看看TechNet了解Start-Process,你会看到的默认值-WaitFalse.这支持开关或"标志" 可以被视为布尔值.

为您图示-Wait参数,您只需要提供一个布尔值.这与您可以选择在cmdlet中调用交换机的方式类似-Wait:$true.

$args = @{
    Wait = $true
    FilePath = "c:\myapp.exe"
    RedirectStandardOutput = "c:\output.txt"
};
Run Code Online (Sandbox Code Playgroud)