0

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.

1 Answer 1

3

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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.