Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用正确解析的参数从powershell启动新的powershell窗口_Powershell - Fatal编程技术网

如何使用正确解析的参数从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")

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

谢谢,您的解决方案工作正常:-)