Link to home
Start Free TrialLog in
Avatar of Alan Varga
Alan VargaFlag for United States of America

asked on

How do I pass a 2-part parameter from one PS script to another?

Given that I have defined these variables in Switchboard-network adapters.ps1

[string] $PsFolder = "C:\Computer\Hardware\Network adapters"
[string] $ScriptToRun = "$PsFolder\EnableDisableNetworkAdapters.ps1"
[string] $ParmToUse = "-wired query"

Open in new window

and my parameter declaration is at the top of EnableDisableNetworkAdapters.ps1

      [string] $wireless,
      [string] $wired,
      [switch] $help

Open in new window

why does this work from the console command line

&'C:\Computer\Hardware\Network adapters\EnableDisableNetworkAdapters.ps1' -wired query

Open in new window

but neither of these work from within Switchboard-network adapters.ps1?

&"$ScriptToRun" "$ParmToUse"

&($ScriptToRun) ($ParmToUse)

Open in new window

My error trapping in EnableDisableNetworkAdapters.ps1 produces

[-wired query] is an unrecognized parameter.

Open in new window

Avatar of oBdA

The best solution in this case is probably to define the parameters to use as a hashtable, then "splat" ( the hashtable when calling the script:
[string] $PsFolder = "C:\Computer\Hardware\Network adapters"
[string] $ScriptToRun = "$PsFolder\EnableDisableNetworkAdapters.ps1"
[hashtable] $ParmToUse = @{'wired' = 'query'}

Open in new window

& $ScriptToRun @ParmToUse

Open in new window

You can pass positional parameters from an array as well. What you cannot do is load an array like this:
$ParmToUse = "-wired query"

Open in new window

"-wired" is the name of the parameter, it cannot be passed like a string value. This string value ("-wired query") will be passed as an argument into the (positional) "wireless" parameter.

"wired" would have to be passed by name as it's the second parameter. Splatting is a good solution for this because you can dynamically build a list of parameters and arguments.
I see you also have a "help" parameter. I guess that's to get help for your script?

If that is so, you would be better developing some comment based help for your script ( Get-Help (or "help" if you prefer) will read that.

PowerShell's help system is far more pervasive than in many / most other languages, it includes a good amount of self-documentation. Spinning your own formatted help is a waste of energy.
Avatar of Alan Varga



I understand now how splatting would work if I were hardcoding the values, as in line 3 of the first code snippet.  The next thing I need to do, based on an EE question Chris answered yesterday, is how do I put several parameters into an array and select one to assign to $ParmToUse?

I've attached my calling and target scripts so you can see where I'm going with this exercise.

Thanks for your ideas.

As to command-based help, it is impressive now that I have it working, but I must have read a dozen articles before figuring out why I was only getting a single line showing the syntax.  According to

Incorrect placement of comment-based help keyword values.
e.g. The incorrect
vs. the correct
.PARAMETER <parameter_name>
The parsing logic should verify the syntax for each keyword.

I will be updating the few scripts I have so far and removing the GetHelp function found in another article.

Thanks for this tip!
The help for individual parameters can be placed on top of the parameter (in the param block) in newer versions of PowerShell.

I prefer this approach and have used in my most recent work.
function test {
    #   This function has help
    #   Not much though.

        # This is the help text for param1.

        # This is the help text for param2.

Open in new window

Then Get-Help just works.
PS> get-help test -param param1

-param1 <Object>
    This is the help text for param1.

    Required?                    false
    Position?                    1
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false

Open in new window

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am extremely satisfied with this solution.  It has been a great learning experience, as this subject area is new to me.  The Experts have come through once again!