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!
valuefrompipelinebypropertynameparameter attribute.$argsvariable is an automatic $Var. you ought not to use it as a parameter name. [2] have you looked at$PSBoundParamtersyet?