weloytty
weloytty

Reputation: 6098

Passing Named Parameters via an intermediary script

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.

Upvotes: 0

Views: 101

Answers (1)

mjolinor
mjolinor

Reputation: 68243

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

Upvotes: 1

Related Questions