[英]Invoke one PowerShell script from Another PowerShell script with passing arguments
[英]Pass arguments from one powershell script to another
我有一个powershell脚本,如下所示:
$ErrorActionPreference = "Stop"
$WORKDIR=$args[0]
Write-Host "Num of arguments given: " $args.Count
$AllArguments = ""
for ($i = 1; $i -lt $args.Count; $i += 2) {
$AllArguments = '$AllArguments $args[$i] "$($args[$i+1])"'
}
Write-Host "AllArguments: $($AllArguments)"
Write-Host "Starting .\someotherscript.ps1 in directory $($WORKDIR)"
pushd "$WORKDIR"
& Powershell -File .\someotherscript.ps1 $AllArguments
popd
基本上,此powershell脚本应启动另一个powershell脚本,但没有第一个参数。 因此,例如,当此脚本以.\\firstscript.ps1 C:\\some\\dir -GiveMeParameter param1
,则它应使用以下参数.\\someotherscript.ps1 -GivMeParameter param1
调用其他脚本。
如何实现呢? 目前,我不知道如何解决这个问题。
您可以使用多个分配来提取第一个参数和其余参数
$WORKDIR, $RemainingArgs = $args
我不确定为什么要再次调用PowerShell来运行脚本。 您可以直接运行脚本,然后使用splatting将其余的args传递给子脚本。
.\someotherscript.ps1 @RemainingArgs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.