如何使用正确解析的参数从powershell启动新的powershell窗口
我有一个powershell脚本,它包装打开十个新的powershell窗口,在每个窗口中,都会使用动态参数调用powershell脚本如何使用正确解析的参数从powershell启动新的powershell窗口,powershell,Powershell,我有一个powershell脚本,它包装打开十个新的powershell窗口,在每个窗口中,都会使用动态参数调用powershell脚本MyScript.ps1。我的C:\temp\SystemWrapper.ps1目前看起来像这样: $system = "1" $version = "1.0.0.0" Write-Host("Triggering system $system / 10 with version $version")
MyScript.ps1
。我的C:\temp\SystemWrapper.ps1目前看起来像这样:
$system = "1"
$version = "1.0.0.0"
Write-Host("Triggering system $system / 10 with version $version")
start powershell { Write-Host('Starting system $system / 10 with version $version'); C:\temp\MyScript.ps1 $system $version; Read-Host}
系统包装器窗口输出:
Triggering system 1 / 10 with version 1.0.0.0
Starting system $system/10 with version $version
C:\temp\myScript.ps1 : Cannot validate argument on parameter 'system'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:1 char:86
+ ... stem/10 with version $version'); C:\temp\myScript.ps1 $system $versio ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [myScript.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,myScript.ps1
第二个powershell窗口输出:
Triggering system 1 / 10 with version 1.0.0.0
Starting system $system/10 with version $version
C:\temp\myScript.ps1 : Cannot validate argument on parameter 'system'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:1 char:86
+ ... stem/10 with version $version'); C:\temp\myScript.ps1 $system $versio ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [myScript.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,myScript.ps1
SystemWrapper.ps1中变量的值在启动第二个powershell窗口时解析不正确,似乎是作为原始字符串$system
和$version
传递的
第二个powershell窗口应如下所示:
Starting system 1 / 10 with version 1.0.0.0
#... and the rest of the script with the arguments $system and $version properly resolved as input parameter
param (
$System,
$Version
)
如何在第二个powershell窗口中实现预期的输出?使用
param()
块开始MyScript脚本,如下所示:
Starting system 1 / 10 with version 1.0.0.0
#... and the rest of the script with the arguments $system and $version properly resolved as input parameter
param (
$System,
$Version
)
然后使用
Start-Process powershell.exe -ArgumentList "-NoLogo -NoExit -File `"C:\temp\MyScript.ps1`" -System $system -Version $version"
谢谢,您的解决方案工作正常:-)