[英]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.