[英]Mandatory parameter for inner function
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.
到目前为止,我已经掌握了以下内容,并且可以正常工作,因为我知道要指定哪些cmdline参数。
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
在代码中,你将如何让ParamSet参数
[string]$killserver
强制两个父功能main
和子功能KillSwitch
因此,如果运行该功能main -nukejobs
PowerShell的提示输入变量$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"
编辑2:提示了强制性参数
serverlist
和datelist
的提示,但现在看来子功能没有写入主机"receive input from $serverlist and $datelist"
Edit 3: corrected the Switch ($PSCmdlet.ParameterSetName){
value for RunMulti
and now things look good. 编辑3:修正后的
Switch ($PSCmdlet.ParameterSetName){
值RunMulti
现在事情看起来不错。
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"
}
I found the mandatory parameters needed separate: 我发现所需的必需参数分开:
[Parameter(Mandatory=$true,ParameterSetName="RunOnce")]
blocks. [Parameter(Mandatory=$true,ParameterSetName="RunOnce")]
块。
For example, this won't work, even if Mandatory=$true
例如,即使
Mandatory=$true
,这也不起作用
My guess is because it only applies to [switch]
我的猜测是因为它仅适用于
[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. 但这将起作用,导致在给出
RunOnce
开关时,Powershell提示输入$server
和$date
。
[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,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.