内部函数的必需参数

时间:2015-08-19 22:35:40

标签: function powershell

我一直试图找出从函数调用函数的最佳方法,并且对两个函数都强制执行参数。到目前为止,我已经得到了以下内容,因为我知道cmdline参数指定了什么,所以工作正常。

我确实找到this post,但我不确定如何使用调用函数的函数。

编辑添加了更短的代码。在代码中,如何为父函数[string]$killserver和子函数main强制使用ParamSet参数KillSwitch,以便在运行函数时main -nukejobs Powershell提示对于变量$killserver

编辑2:计算了强制参数serverlistdatelist的提示,但现在看来子函数没有写入主机"receive input from $serverlist and $datelist"

编辑3:更正了Switch ($PSCmdlet.ParameterSetName){的{​​{1}}值,现在看起来不错了。

RunMulti

1 个答案:

答案 0 :(得分:0)

我发现必须分开强制参数:

[Parameter(Mandatory=$true,ParameterSetName="RunOnce")]阻止。

例如,即使Mandatory=$true

,这也不会奏效

我的猜测是因为它仅适用于[switch]

[Parameter(Mandatory=$false,ParameterSetName="RunOnce",
HelpMessage="Enter ServerName to schedule reboot for.")]
[switch]$RunOnce,
[string]$server,
[string]$date,

但这会有效,导致Powershell在$server切换时提示$dateRunOnce

[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,
相关问题