Created
April 23, 2014 19:37
-
-
Save AdilHindistan/11229454 to your computer and use it in GitHub Desktop.
Sample for a complex PowerShell ParameterSet implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##AH - AdilHindistan | |
## Sample for a complex Parameter Set | |
## The best way to get it right is to write them down as shown in .NOTES | |
<# | |
.NOTES | |
Parameter Sets | |
P1: SourceDN DestinationDN SourcePercent Recurse | |
P2: SourceOU DestinationOU SourcePercent Recurse | |
P3: SourceDN DestinationDN DestinationPercent Recurse (Default) | |
P4: SourceOU DestinationOU DestinationPercent Recurse | |
Recurse is Optional in each | |
Move-ComputerFromOneOUToAnotherByPercent [-SourceDN] <string> [-DestinationDN] <string> [-SourcePercent] <int> [[-Recurse]] [<CommonParameters>] | |
Move-ComputerFromOneOUToAnotherByPercent [-DestinationPercent] <int> [[-Recurse]] [-SourceDN <string>] [-DestinationDN <string>] [<CommonParameters>] | |
Move-ComputerFromOneOUToAnotherByPercent [-SourceOU <ADOrganizationalUnit>] [-DestinationOU <ADOrganizationalUnit>] [-DestinationPercent <int>] [[-Recurse]] [<CommonParameters>] | |
Move-ComputerFromOneOUToAnotherByPercent [-SourceOU] <ADOrganizationalUnit> [-DestinationOU] <ADOrganizationalUnit> [-SourcePercent <int>] [[-Recurse]] [<CommonParameters>] | |
#> | |
Function Move-ComputerFromOneOUToAnotherByPercent { | |
#Requires -Modules ActiveDirectory | |
[CMDLETBINDING( | |
DefaultParameterSetName='p3' ## SourceDN,DestinationDN,DestinationPercent | |
)] | |
Param( | |
# DistingishedName of Source OU | |
[Parameter( | |
Mandatory=$true, | |
Position=0, | |
ParameterSetName='p1' | |
)] | |
[Parameter(ParameterSetName='p3')] | |
[ValidateScript({$_ -contains ',dc='})] | |
[string]$SourceDN, | |
# Source OU Object | |
[Parameter( | |
Mandatory=$true, | |
Position=0, | |
ParameterSetName='p2' | |
)] | |
[Parameter(ParameterSetName='p4')] | |
[Microsoft.ActiveDirectory.Management.ADOrganizationalUnit]$SourceOU, | |
# DistinguishedName of Destination OU | |
[Parameter( | |
Mandatory=$true, | |
Position=1, | |
ParameterSetName='p1' | |
)] | |
[Parameter(ParameterSetName='p3')] | |
[ValidateScript({$_ -contains ',dc='})] | |
[string]$DestinationDN, | |
# Destination OU as ADOrganizationalUnit | |
[Parameter( | |
Mandatory=$true, | |
Position=1, | |
ParameterSetName='p2' | |
)] | |
[Parameter(ParameterSetName='p4')] | |
[ValidateScript({$_ -contains ',dc='})] | |
[Microsoft.ActiveDirectory.Management.ADOrganizationalUnit]$DestinationOU, | |
# % of computers that will be moved from Source OU to Destination OU | |
[Parameter( | |
Mandatory=$true, | |
Position=2, | |
ParameterSetName='p1' | |
)] | |
[Parameter(ParameterSetName='p2')] | |
[ValidateRange(1,100)] | |
[int]$SourcePercent, | |
# % of Source of the destination OU should be | |
[Parameter( | |
Mandatory=$true, | |
Position=2, | |
ParameterSetName='p3' | |
)] | |
[Parameter(ParameterSetName='p4')] | |
[ValidateRange(1,100)] | |
[int]$DestinationPercent, | |
# Include Sub OUs of Source OU | |
[Parameter(Position=3)] | |
[switch]$Recurse | |
) | |
Begin | |
{ | |
If ($PSCmdlet.ParameterSetName -eq 'p1') { | |
If ($Recurse) { | |
$SourceComputers = Get-ADComputer -SearchBase $SourceDN -Filter * -SearchScope Subtree | |
} else { | |
$SourceComputers = Get-ADComputer -SearchBase $SourceDN -Filter * -SearchScope OneLevel | |
} | |
} | |
} | |
Process | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment