one of two parameters mandatory
Hm, I think you have too many parameter sets. Also not sure why you have separate user/password parameters and a PSCredential parameter. You should really just use the PSCredential. But I'll assume you need this for some reason (please consider changing it).
4 parameter sets:
- ComputerNameCred
- ComputerNamePlain
- IpAddressCred
- 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.
I also removed [switch]
from your IP address parameter; not sure why that was there. You may want to consider using [System.Net.IPAddress]
for that parameter instead of [String]
. It will still accept strings (they will be cast), but it automatically validates the parameter since the cast will fail if it's not a valid IP.
this is my solution thanks to 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
)