0

I have two scripts:

Script1.ps1:

param(
[string]$WhoCares = "",
[string]$PassThrough = ""
)

#do a whole bunch of random stuff here...
.\Script2.ps1 $PassThrough

Script2.ps1:

param(
[string]$FirstParameter = "",
[string]$SecondParameter = ""
)

Write-Host "First Parameter is: " $FirstParameter "Second Parmeter is: " $SecondParameter

What I envision doing is something like:

Script1.ps1 -WhoCares one -Passthrough "-FirstParameter Test -SecondParameter Test1"

And then seeing:

First Paramter is Test Second Parameter is Test1

But What I am seeing is

First Parameter is -FirstParameter Test -SecondParameter Test1 Second Parameter is

which is, parameters I want to send to script2 are coming through as a string. How to I pass parameters through an intermediary script

I don't want to modify Script1.ps1 to include every possible parameter, because im using Script1 to setup an environment, logging, etc.

1
  • Just use a -PassThru switch on Script2, it will inherit any variables defined in Script1 if you call it from Script1. Commented Mar 7, 2014 at 15:25

1 Answer 1

1

I'd change Script1 to splat the Passthrough parameters to Script2:

param(
[string]$WhoCares = "",
[hashtable]$PassThrough = @{}
)

#do a whole bunch of random stuff here...
.\Script2.ps1 @PassThrough

Then pass the Passthrough parameters to Script1 as a hashtable:

$Passthru =
 @{
   FirstParameter = 'Test'
   SecondParameter = 'Test1'
  }

  Script1.ps1 -WhoCares One -Passthrough $Passthru
Sign up to request clarification or add additional context in comments.

1 Comment

Just to be clear, I think splatting requires v3+ iirc. But it is definitely the way to accomplish this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.