[英]Pass arguments from one powershell script to another
I have one powershell script, like this one: 我有一个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
Basically, this powershell script shoud start another powershell script, but without the first argument. 基本上,此powershell脚本应启动另一个powershell脚本,但没有第一个参数。 So, eg when this script is started with
.\\firstscript.ps1 C:\\some\\dir -GiveMeParameter param1
, then it should call the other script with the following parameters .\\someotherscript.ps1 -GivMeParameter param1
. 因此,例如,当此脚本以
.\\firstscript.ps1 C:\\some\\dir -GiveMeParameter param1
,则它应使用以下参数.\\someotherscript.ps1 -GivMeParameter param1
调用其他脚本。
How to achieve that? 如何实现呢? At the moment I do not know how to solve this problem.
目前,我不知道如何解决这个问题。
You can use multiple assignment to extract the first and remaining arguments by doing 您可以使用多个分配来提取第一个参数和其余参数
$WORKDIR, $RemainingArgs = $args
I'm not sure why you're calling PowerShell again to run the script. 我不确定为什么要再次调用PowerShell来运行脚本。 You can just run the script directly and use splatting to pass the remaining args to the child script.
您可以直接运行脚本,然后使用splatting将其余的args传递给子脚本。
.\someotherscript.ps1 @RemainingArgs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.