gpt4 book ai didi

powershell - 如何在Powershell中为可执行文件构建参数列表?

转载 作者:行者123 更新时间:2023-12-03 01:07:47 27 4
gpt4 key购买 nike

我有一个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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com