Powershell:将数组转换为参数序列,以通过 Octopus 仪表板将它们传递给 exe 应用程序
Powershell: Convert an array to sequence of arguments to pass them to an exe application through Octopus dashboard
我使用 Octopush 仪表板:
屏幕截图上的代码为我提供了 TFS 更改集列表。
所以我已经有了我的 ID 列表。
我需要 运行 一些 exe 文件并将参数传递给这个 exe
通过 C# 的以下方式:
string cParams = "\"Test proj name\" " + "3.22.652.965863 " + "QA " + "false " + "463841" + " 464268" + " 463450" + " 463841" + " 463167" + " 458908" + " 462917" + " 462780" + " 462429" + " 461225" + " 460414";
var proc = System.Diagnostics.Process.Start(@"\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe", cParams);
或者在 PowerShell 中它是这样工作的:
start "" "\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe" "Test proj name" 3.22.652.965863 QA false 463841 464268 463450 463841 463167 458908 462917
pause
我需要结合我的硬编码参数 "proj name"、"version num"、"string1"、"false" 和我收到的 ID 列表。
如何让它在 Powershell 中工作?
$Exe = '\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe'
$Params = "Test proj name " + "3.22.652.965863 " + "QA " + "false " + "463841" + " 464268" + " 463450" + " 463841" + " 463167" + " 458908" + " 462917" + " 462780" + " 462429" + " 461225" + " 460414"
&$Exe $Params
取决于您的本机命令,您可能需要先将参数作为数组加入
$Params = "Test proj name","3.22.652.965863","QA ","false" [etc.]
&$Exe $Params
使用您的示例,您仍然可以继续使用 System.Diagnostics.Process:
[System.Diagnostics.Process]::Start($Exe,$Params)
另一个选项正在使用 WMI:
([WMICLASS]"ROOT\CIMV2:Win32_Process").Create("$Exe $Params")
当然还有带有 -ArgumentList
参数的 PowerShell Start-Process
编辑
将常量和动态变量分开,
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","QA" etc.
$Dynamic = "462917","462780","462429","461225","460414"
&$Exe $Constants $Dynamic
或使用其他执行选项之一
$argumentList = "`"Test proj name`" 3.22.652.965863 QA false 463841 464268 463450 463841 463167 458908 462917"
start-process -FilePath "\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe" -ArgumentList $argumentList
我的答案基于 Avshalom 建议的代码,已修改:
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio
2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","Dev","false"
$Dynamic = "462917","462780","462429","461225","460414"
$all = $Constants + $Dynamic | select -uniq
&$Exe $all
问题是无法通过这种方式将两个参数传递给 .NET concole 应用程序:
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","QA" etc.
$Dynamic = "462917","462780","462429","461225","460414"
&$Exe $Constants $Dynamic
因为应用程序从未收到最后一个参数。我想 powershell 只是忽略了第二个。
我使用 Octopush 仪表板:
屏幕截图上的代码为我提供了 TFS 更改集列表。 所以我已经有了我的 ID 列表。 我需要 运行 一些 exe 文件并将参数传递给这个 exe 通过 C# 的以下方式:
string cParams = "\"Test proj name\" " + "3.22.652.965863 " + "QA " + "false " + "463841" + " 464268" + " 463450" + " 463841" + " 463167" + " 458908" + " 462917" + " 462780" + " 462429" + " 461225" + " 460414";
var proc = System.Diagnostics.Process.Start(@"\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe", cParams);
或者在 PowerShell 中它是这样工作的:
start "" "\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe" "Test proj name" 3.22.652.965863 QA false 463841 464268 463450 463841 463167 458908 462917
pause
我需要结合我的硬编码参数 "proj name"、"version num"、"string1"、"false" 和我收到的 ID 列表。 如何让它在 Powershell 中工作?
$Exe = '\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe'
$Params = "Test proj name " + "3.22.652.965863 " + "QA " + "false " + "463841" + " 464268" + " 463450" + " 463841" + " 463167" + " 458908" + " 462917" + " 462780" + " 462429" + " 461225" + " 460414"
&$Exe $Params
取决于您的本机命令,您可能需要先将参数作为数组加入
$Params = "Test proj name","3.22.652.965863","QA ","false" [etc.]
&$Exe $Params
使用您的示例,您仍然可以继续使用 System.Diagnostics.Process:
[System.Diagnostics.Process]::Start($Exe,$Params)
另一个选项正在使用 WMI:
([WMICLASS]"ROOT\CIMV2:Win32_Process").Create("$Exe $Params")
当然还有带有 -ArgumentList
参数的 PowerShell Start-Process
编辑
将常量和动态变量分开,
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","QA" etc.
$Dynamic = "462917","462780","462429","461225","460414"
&$Exe $Constants $Dynamic
或使用其他执行选项之一
$argumentList = "`"Test proj name`" 3.22.652.965863 QA false 463841 464268 463450 463841 463167 458908 462917"
start-process -FilePath "\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe" -ArgumentList $argumentList
我的答案基于 Avshalom 建议的代码,已修改:
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio
2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","Dev","false"
$Dynamic = "462917","462780","462429","461225","460414"
$all = $Constants + $Dynamic | select -uniq
&$Exe $all
问题是无法通过这种方式将两个参数传递给 .NET concole 应用程序:
$Exe = `\ptnas1\home_dirs\michaelb\Documents\Visual Studio 2015\Projects\Changesetes\Changesetes\bin\Debug\Changesetes.exe`
$Constants = "Test proj name","3.22.652.965863","QA" etc.
$Dynamic = "462917","462780","462429","461225","460414"
&$Exe $Constants $Dynamic
因为应用程序从未收到最后一个参数。我想 powershell 只是忽略了第二个。