两个参数之一为必填项

两个参数之一为必填项

我正在尝试创建一个带有参数的可靠 PowerShell 函数。现在的问题是我无法让 ParameterSetNames 工作。

我想要得到的是

  1. computername 或 ipaddress 始终是必填项
  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
)

答案1

嗯,我觉得你的参数集太多了。另外,我不确定你为什么有单独的用户/密码参数和 PSCredential 参数。你真的应该只使用 PSCredential。但我假设你出于某种原因需要它(请考虑更改它)。

4 个参数集:

  1. 计算机名称信用
  2. 计算机名称纯文本
  3. ip地址凭证
  4. 普通地址
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.

我还[switch]从您的 IP 地址参数中删除了 ;不确定为什么会这样。您可能需要考虑使用[System.Net.IPAddress]该参数而不是[String]。它仍将接受字符串(它们将被强制转换),但它会自动验证该参数,因为如果它不是有效的 IP,强制转换将失败。

答案2

这是我的解决方案,感谢 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
)

相关内容