两个参数之一是强制性的

我正在尝试使用参数创build一个可靠的PowerShell函数。 现在的问题是我没有得到ParameterSetNames工作。

我想得到的是那个

  1. 计算机名或IP地址始终是强制性的
  2. 计算机名或IP地址必须设置,但不能同时设置(如果可能的话)
  3. 如果给予adminuser或adminpassword另一个是强制性的

我尝试了很多不同的设置,但一直失败,这是我的最后一次尝试

CmdletBinding(DefaultParameterSetName="IpAddress")] [CmdletBinding()] Param ( # computername: Name of the host you want to connect to. [Parameter(Mandatory=$true,ParameterSetName="ComputerName", Position=0)] [Parameter(Mandatory=$false,ParameterSetName="IpAddress")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCredentials", Position=0)] [Parameter(Mandatory=$false,ParameterSetName="IpAddressCredentials")] [Parameter(Mandatory=$false,ParameterSetName="IpAddressUser")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNameUser", Position=0)] [String]$computername, # ipAddress: Ip Address of the host you want to connect to. [Parameter(Mandatory=$false,ParameterSetName="ComputerName")] [Parameter(Mandatory=$true,ParameterSetName="IpAddress", Position=0)] [Parameter(Mandatory=$false,ParameterSetName="ComputerNameCredentials")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressCredentials", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="IpAddressUser", Position=0)] [Parameter(Mandatory=$false,ParameterSetName="ComputerNameUser")] [switch] [String]$ipAddress, # credentials: PowerShell credentials use to connect to the host. [Parameter(Mandatory=$false,ParameterSetName="ComputerName")] [Parameter(Mandatory=$false,ParameterSetName="IpAddress")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCredentials")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressCredentials")] [Parameter(Mandatory=$false,ParameterSetName="IpAddressUser")] [Parameter(Mandatory=$false,ParameterSetName="ComputerNameUser")] [PSCredential]$credentials, # adminUser: Username to use to connect to the host. [Parameter(Mandatory=$false,ParameterSetName="ComputerName")] [Parameter(Mandatory=$false,ParameterSetName="IpAddress")] [Parameter(Mandatory=$false,ParameterSetName="ComputerNameCredentials")] [Parameter(Mandatory=$false,ParameterSetName="IpAddressCredentials")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressUser")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNameUser")] [String]$adminUser, # adminPassword: Password to use to connect to the host. [Parameter(Mandatory=$false,ParameterSetName="ComputerName")] [Parameter(Mandatory=$false,ParameterSetName="IpAddress")] [Parameter(Mandatory=$false,ParameterSetName="ComputerNameCredentials")] [Parameter(Mandatory=$false,ParameterSetName="IpAddressCredentials")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressUser")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNameUser")] [String]$adminPassword ) 

嗯,我想你有太多的参数集。 也不知道为什么你有单独的用户/密码参数和PSCredential参数。 你应该只使用PSCredential。 但是我会假设你出于某种原因需要这个(请考虑改变它)。

4个参数集:

  1. ComputerNameCred
  2. ComputerNamePlain
  3. IpAddressCred
  4. IpAddressPlain
 function Thing { [CmdletBinding(DefaultParameterSetName="ComputerNameCred")] Param ( # computername: Name of the host you want to connect to. [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCred", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain", Position=0)] [String]$computername, # ipAddress: Ip Address of the host you want to connect to. [Parameter(Mandatory=$true,ParameterSetName="IpAddressCred", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain", Position=0)] [String]$ipAddress, # credentials: PowerShell credentials use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCred")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressCred")] [PSCredential]$credentials, # adminUser: Username to use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain")] [String]$adminUser, # adminPassword: Password to use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain")] [String]$adminPassword ) } Get-Help Thing # use this to make sure your parameter sets are as they should be. 

我也从你的IP地址参数中删除了[switch] ; 不知道为什么那里。 您可能需要考虑使用[System.Net.IPAddress]作为该参数而不是[String] 。 它仍然会接受string(它们会被转换),但是它会自动validation参数,因为如果它不是有效的IP,转换将会失败。

这是我的解决scheme,感谢briantist

 [CmdletBinding(DefaultParameterSetName="ComputerName")] Param ( # computername: Name of the host you want to connect to. [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCred", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="ComputerName", Position=0)] [String]$computername, # ipAddress: Ip Address of the host you want to connect to. [Parameter(Mandatory=$true,ParameterSetName="IpAddressCred", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain", Position=0)] [Parameter(Mandatory=$true,ParameterSetName="IpAddress", Position=0)] [String]$ipAddress, # credentials: PowerShell credentials use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="ComputerNameCred")] [Parameter(Mandatory=$true,ParameterSetName="IpAddressCred")] [PSCredential]$credentials, # adminUser: Username to use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain")] [String]$adminUser, # adminPassword: Password to use to connect to the host. [Parameter(Mandatory=$true,ParameterSetName="IpAddressPlain")] [Parameter(Mandatory=$true,ParameterSetName="ComputerNamePlain")] [String]$adminPassword, # writeLog: Boolean that enables or disables log-writing. [Parameter(Mandatory=$false)] [Boolean]$writeLog=$true )