我有一个Powershell片段,我需要在其中调用带有开关列表的外部可执行文件。
& $pathToExe
--project $project `
--server $server `
--apiKey $key `
现在,我需要执行类似“if
$someVariable -eq $True
然后还添加--optionalSwitch $someValue
”的操作。没有重复的负载怎么办?作为引用,真正的exe调用远大于此,并且可选开关的列表也更大!
最佳答案
包含参数及其值的哈希表如何?像这样
$ht = @{}
$ht.Add('project', 'myProject')
$ht.Add('apikey', $null)
$ht.Add('server', 'myServer')
要构建参数字符串,请通过排除没有值的键来过滤集合:
$pop = $ht.getenumerator() | ? { $_.value -ne $null }
通过迭代过滤的集合来构建命令字符串
$cmdLine = "myExe"
$pop | % { $cmdLine += " --" + $_.name + " " + $_.value }
# Check the result
$cmdLine
myExe --server myServer --project myProject
关于powershell - 如何在Powershell中为可执行文件构建参数列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031231/