Reputation: 2448
I'm stuck trying to figure out the best method for calling a function from a function and making parameters mandatory for both functions. I've got the below so far, and things work because I know what cmdline params to specify.
I did find this post but I'm not sure how to use that with a function that calls a function.
Edit: added shorter code. In the code, How would you make the ParamSet parameter [string]$killserver
mandatory for both the parent function main
and the child function KillSwitch
so that if the function is run main -nukejobs
Powershell prompts for the variable $killserver
Edit 2: worked out the prompting for the mandatory param serverlist
and datelist
but it appears now the child function doesn't write to host "receive input from $serverlist and $datelist"
Edit 3: corrected the Switch ($PSCmdlet.ParameterSetName){
value for RunMulti
and now things look good.
Function Main{
[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="ViewOnly")]
Param(
[Parameter(Mandatory=$false,ParameterSetName="KillSwitch")]
[Switch]$NukeJobs,
[Parameter(Mandatory=$true,ParameterSetName="KillSwitch",
HelpMessage="Enter ServerName to remove the scheduled reboot for, Check using main -viewonly")]
[String]$killserver,
[Parameter(Mandatory=$false,ParameterSetName="RunMulti")]
[switch]$RunMultiple,
[Parameter(Mandatory=$true,ParameterSetName="RunMulti")]
[String]$serverlist,
[Parameter(Mandatory=$true,ParameterSetName="RunMulti")]
[String]$datelist
)
Switch ($PSCmdlet.ParameterSetName) {
"KillSwitch" {
KillSwitch -server $killserver
} # end killswitch
"RunMulti" {
RunMulti -serverlist $serverlist -datelist $datelist
} # end run multi
} # end switch block
} # end main function
Function KillSwitch{
Param(
[Parameter(Mandatory=$true)]
[String]$server
)
"Removing previous scheduled reboot for $server"
} # end killswitch function
Function RunMulti {
Param(
[Parameter(Mandatory=$true,
HelpMessage="Text file with server names to reboot, one per line!")]
[string]$serverlist,
[Parameter(Mandatory=$true,
HelpMessage="Text file with date/times, one per line!")]
[String]$datelist
)
"receive input from $serverlist and $datelist"
}
Upvotes: 0
Views: 124
Reputation: 2448
I found the mandatory parameters needed separate:
[Parameter(Mandatory=$true,ParameterSetName="RunOnce")]
blocks.
For example, this won't work, even if Mandatory=$true
My guess is because it only applies to [switch]
[Parameter(Mandatory=$false,ParameterSetName="RunOnce",
HelpMessage="Enter ServerName to schedule reboot for.")]
[switch]$RunOnce,
[string]$server,
[string]$date,
But this will work, causing Powershell to prompt for $server
and $date
when the RunOnce
switch is given.
[Parameter(Mandatory=$false,ParameterSetName="RunOnce",
HelpMessage="Enter ServerName to schedule reboot for.")]
[switch]$RunOnce,
[Parameter(Mandatory=$true,ParameterSetName="RunOnce")]
[string]$server,
[Parameter(Mandatory=$true,ParameterSetName="RunOnce")]
[string]$date,
Upvotes: 0