devopsfun
devopsfun

Reputation: 1418

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, e.g. 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.

Upvotes: 0

Views: 1259

Answers (1)

Bruce Payette
Bruce Payette

Reputation: 2639

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 

Upvotes: 3

Related Questions