2
1
I am using Powershell 4.0.
I am attempting to write a cmdlet and in this cmdlet I would like to use Advanced Parameter sets to have certain options available based on what parameter(s) is/are chosen. This particular function will essentially and eventually be Get-ADComputer but with the -SearchBase preprogrammed for certain options.
I have 6 parameters total. 2 are strings ($ComputerName or $IpAddress), 1 is an integer ($OULevel) and 3 switches ($ComputerOU, $AllCompany, $List).
I have a parameter set each for ComputerName and IPAddress, I would like the user to be able to input one or the other - I think I have this figured out its pretty simple. However, I would like $OULevel, $ComputerOU and $AllCompany to be exclusive meaning if one is used the other should not be able to be used. $List should remain available in each scenario.
I have tried different variations of the parameter sets to no avail. This is what my script looks like now, with some trimmed back:
function Get-CompanyADComputer{
[CmdletBinding(DefaultParametersetName="ComputerName")]
Param(
[Parameter(Mandatory=$true,
ParameterSetName="ComputerName",
Position=0,
ValueFromPipeline=$false,
HelpMessage='Enter a computer name to search in ActiveDirectory.')]
[Alias('Computer','CN')]
[string]$ComputerName,
[Parameter(Mandatory=$true,
ParameterSetName="IPAddress",
Position=0,
ValueFromPipeline=$false,
HelpMessage='Enter an IP address to search in ActiveDirectory.')]
[Alias('IPv4Address','IPv6Address')]
[string]$IPAddress,
[Parameter(Mandatory=$false,
HelpMessage='Enter a number between 0 and 8. 0 is your current OU Container.')]
[ValidateRange(0,8)]
[int]$OULevel = 0,
[Parameter()]
[Switch]$ComputerOU,
[Parameter()]
[Switch]$AllCompany,
[Parameter()]
[Switch]$List
)
If you are curious our AD is organized by Location, then category (computer, user, groups, contacts, etc) and then it gets more granular in each OU below. This script detects your computer's OU and starts the search there. The purpose of $OULevel is if the user specifies a different number the search will start in a different OU and then search recursively. The purpose of $ComputerOU is to have your search go to the default Computers OU instead of the entire domain or your location. The purpose of $AllCompany is to have the search default to the entire domain instead of any other choice or OU.
Any guidance is appreciated. I can't seem to get the hang of this one without my script getting all convoluted.