We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

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

Alan Varga
Alan Varga asked
on
83 Views
Last Modified: 2017-03-30
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

Param(
      [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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
The best solution in this case is probably to define the parameters to use as a hashtable, then "splat" (https://blogs.technet.microsoft.com/heyscriptingguy/2010/10/18/use-splatting-to-simplify-your-powershell-scripts/) 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

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 (https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_comment_based_help). 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.
Alan VargaAccess Developer

Author

Commented:
oBdA/Chris,

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.
==========
Chris,

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 https://github.com/PowerShell/PSScriptAnalyzer/issues/500:

Incorrect placement of comment-based help keyword values.
e.g. The incorrect
.PARAMETER
<parameter_name>
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!
Switchboard-network-adapters.txt
EnableDisableNetworkAdapters.txt
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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 {
    # .SYNOPSIS
    #   This function has help
    # .DESCRIPTION
    #   Not much though.

    param(
        # This is the help text for param1.
        $param1,

        # This is the help text for param2.
        $param
    )
}

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

PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Alan VargaAccess Developer

Author

Commented:
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!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.