简体   繁体   中英

Passing named parameters from one powershell script to another

I have a situation in which I have script_1.ps1 that gets called directly by a user. Script_1.ps1 is really just an entry point (at minimum providing just a hard coded config value) to script_2.ps1, which contains shared, factored out logic. I want the user to be able to pass any or all required arguments to script_1, which in turn must pass to script_2. If not all the required parameters are passed by the user, there's logic in script_2 that will prompt the user for the information.

In my ideal setup, I'd have script_1 accept named parameters from the user, and then script_2 accept the named parameters from script_1. For example, my script_1.ps1 would look something like this:

param (
    [switch] $Help = $false,
    [switch] $Quiet,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    .\script_2.ps1 -ServiceName "Service name here" #also pass any other args that were set by user (may be none, several, or all)
}

Then my script_2.ps1 would look something like this:

param (    
    [switch] $Quiet,
    [Alias("l")]
    [string] $ServiceName,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

# do things with arguments here

Is it possible to achieve this without enumerating all arguments when I call script_2.ps1 from script_1.ps1? My script_1 has about 20 different possible parameters, so something like this would get messy pretty fast:

.\script_2.ps1 -ServiceName "Service name here" -Quiet $Quiet -Param1 $Param1 -Param2 $Param2 

The closest I've gotten to making this work is with the following, but then it cuts off arguments with spaces by the time they get to script_2. Putting escaped quotes around $args results in no arguments getting passed.

param (
    [switch] $Help = $false,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    Start-Process -FilePath powershell.exe -ArgumentList "-file `".\script_2.ps1`" -ServiceName `"Service name here`" $args"
}

I'm sure I'm missing a trick to do this as a relative newcomer to powershell... TIA for any help!

You can use splatting to pass the collection of parameters from the first script into the second script. All this means is that instead of writing out the parameters to script2.ps1 longhand, you pass in a hashtable of key-value pairs.

Fortunately, there's a built-in hashtable which already contains the parameters passed in to a script or a function - $PSBoundParameters - so you can just splat that straight down from script1.ps1 into script2.ps1 ...

Note that you invoke a "splat" with the @ symbol, not $ - ie @PSBoundParameters in this case.

script.ps1

param (
    [switch] $Help = $false,
    [switch] $Quiet,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    write-host "script1.ps1"
    write-host ($PSBoundParameters | ft | out-string);
    . .\script2.ps1 @PSBoundParameters
}

script2.ps1

param (    
    [switch] $Quiet,
    [Alias("l")]
    [string] $ServiceName,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

# do things with arguments here
write-host "script2.ps1"
write-host ($PSBoundParameters | ft * | out-string)

If you then call:

C:\> powershell .\script1.ps1 -Param1 "aaa" -Quiet:$true

you get this output:

script1.ps1

Key    Value
---    -----
Param1 aaa
Quiet  True

script2.ps1

Key    Value
---    -----
Param1 aaa
Quiet  True

and if you don't like the built-in $PSBoundParameters hashtable, you can build your own and splat that instead:

$mySplat = @{
    "Quiet" = $true
    "Param1" = "myParam1"
}
$mySplat.Add("Param2", "myParam2")
$mySplat.Remove("Quiet");
. .\script2.ps1 @mySplat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM