I’m having a problem making parameter sets work the way I want.
My main function:
function Get-ComputerName {
[CmdletBinding(DefaultParameterSetName = 'AllComputers')]
[OutputType([String])]
Param(
# Gets all computers: workstations, member servers, and domain controllers. This is the default behavior when no switch is specified.
[Parameter(ParameterSetName="AllComputers")]
[switch]$AllComputers,
# Lists all workstations.
[Parameter(ParameterSetName="SomeComputers")]
[switch]$Workstations,
# Lists all member servers.
[Parameter(ParameterSetName="SomeComputers")]
[Alias('Servers')]
[switch]$MemberServers,
# Lists all domain controllers.
[Parameter(ParameterSetName="SomeComputers")]
[Alias('DCs')]
[switch]$DomainControllers,
# Filters the list of computers to include only those currently online. May not be used with -Offline.
[Parameter()]
[switch]$Online,
# Filters the list of computers to include only those currently offline. May not be used with -Online.
[Parameter()]
[switch]$Offline
)
BEGIN {}
PROCESS{
# Query AD. Clean up @PSBoundParameters and call Get-OnlineComputerName if needed.
}
END {}
} #function Get-ComputerName
This function returns a list of computer names from Active Directory queries. If the -Online or -Offline parameters are supplied then this function calls Get-OnlineComputerName to test network connectivity:
function Get-OnlineComputerName {
[CmdletBinding(DefaultParameterSetName = 'Online')]
[OutputType([String])]
param (
# Used to specify the computers of interest.
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[string[]]$ComputerName,
# Returns a list of online computers.
[Parameter(ParameterSetName="Online")]
[switch]$Online,
# Returns a list of offline computers.
[Parameter(ParameterSetName="Offline")]
[switch]$Offline
)
BEGIN {}
PROCESS{
# Return list of computers that passed or failed Test-Connection
}
END {}
Help for Get-OnlineComputerName shows the desired syntax:
Get-OnlineComputerName [-ComputerName] {string} [-Online] [{CommonParameters}]
Get-OnlineComputerName [-ComputerName] {string} [-Offline] [{CommonParameters}]
Help for Get-ComputerName shows this syntax:
Get-ComputerName [-AllComputers] [-Online] [-Offline] [{CommonParameters}]
Get-ComputerName [-Workstations] [-MemberServers] [DomainControllers] [-Online] [-Offline] [{CommonParameters}]
I’d like to have Get-ComputerName accept -Online or -Offline, but not both, and to also have the option of using neither to get a computer name list without calling Get-OnlineComputerName to test connectivity. The closest I could get using parameter sets was to make -Online and -Offline mutually exclusive but the function then won’t let me not use one of them.
My options without parameters sets seem to be let Get-ComputerName pass both -Online and -Offline, at which point Get-OnlineComputername writes an error (not ideal since the user didn’t call that function and doesn’t know why it’s giving an error) or use a Throw in Get-ComputerName when both -Online and -Offline have been used.
Is there a cleaner approach?