简体   繁体   English

内部功能的必填参数

[英]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:提示了强制性参数serverlistdatelist的提示,但现在看来子功能没有写入主机"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.

相关问题 使函数参数强制的最佳方法是什么? - What's the best way to make a parameter of a function mandatory? Powershell功能-在一种情况下,参数必须是必需的,在另一种情况下,则必须是可选的 - Powershell function - parameter has to be mandatory in one case, optional in another 如何将 function 的二维数组参数作为内部 function 的参数传递 - How to pass a two dimensional array parameter of a function as parameter of an inner function 如何将内部 function 数组作为参数发送? - How to send the inner function an array as a parameter? 在PowerShell函数中,如何仅当缺少另一个可选参数时才使参数成为必需参数? - In a PowerShell function, how can I make a parameter mandatory only if another optional parameter is absent? 函数的必需参数和默认参数 - Mandatory and default parameters of a function PHP强制函数调用 - PHP mandatory function call 如何使用内部 function 的返回结果作为外部 function 的参数? - How to use returned result from inner function as a parameter in the outer function? 如何通过使所有其他强制/可选参数(如果有的话)保持默认值,仅将可选参数传递给PHP函数? - How to only pass an optional parameter to a PHP function by keeping all other mandatory/optional parameters(if any) equal to their default values? 在执行函数指针的typedef时是*必需的? - is * mandatory while doing typedef of a function pointer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM