mgu*_*ssa 3 powershell arguments process
我已经尽可能地简化了我的情况。我有一个 PowerShell 脚本,它使用Start-Process cmdlet通过“以管理员身份运行”选项以编程方式启动进程:
param
(
# Arguments to pass to the process, if any
[string[]] $ArgList
)
if($ArgList)
{
Start-Process -FilePath notepad.exe -ArgumentList $ArgList -Verb RunAs
}
else
{
Start-Process -FilePath notepad.exe -Verb RunAs
}
Run Code Online (Sandbox Code Playgroud)
由于ArgumentList不能为 NULL 或为空,我不得不添加if-else以避免ParameterArgumentValidationError错误。
一切都按预期工作没有问题,但我只是想知道是否有更优雅(但仍然简单)的方法来完成它而不使用if-else块。
您可以使用“splat”运算符来动态添加参数。
使用参数名称的键定义哈希。然后将其传递给 cmdlet:
$extras = @{}
if (condition) { $extras["ArgumentList"] = whatever }
Start-Process -FilePath = "notepad.exe" @extras
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2296 次 |
| 最近记录: |