简体   繁体   中英

Pass arguments from one powershell script to another

I have one powershell script, like this one:

$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. 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 .

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. You can just run the script directly and use splatting to pass the remaining args to the child script.

.\someotherscript.ps1 @RemainingArgs 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM