Skip to content

Instantly share code, notes, and snippets.

@shinjijai
Last active May 11, 2018 14:32
Show Gist options
  • Save shinjijai/eae45c19352fcaa39a3ecaa01adfe31e to your computer and use it in GitHub Desktop.
Save shinjijai/eae45c19352fcaa39a3ecaa01adfe31e to your computer and use it in GitHub Desktop.
Get a list of OU with filter
function Format-DisplayList {
<#
.DESCRIPTION
Display an array larger than certain members into nice column(s) for
easy readability. Added the ability to either use a number system or dashes
to display the list.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
$ArrayList,
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
HelpMessage="Use the name properly of a multi-dimension array")]
[Switch]$DisplayName,
[Parameter(Mandatory=$false,
HelpMessage="Use to allow the usage of '-' instead numbers")]
[boolean]$DashIndex = $false
)
switch($ArrayList.Count) {
{($_ -le 10)} {
$Column = 1
}
{($_ -gt 10) -and ($_ -le 15)} {
$Column = 2
}
default {
$Column = 3
}
}
$TempArr = @()
$TempInx = 1
switch($DisplayName) {
$false {
foreach($Item in $ArrayList) {
if($DashIndex) {
$TempArr += "- $Item"
}
else {
$TempArr += "$TempInx) $Item"
$TempInx = $TempInx + 1
}
}
break
}
$true {
foreach($Item in $ArrayList) {
if($DashIndex) {
$TempArr += "- $Item.Name"
}
else {
$TempArr += "$TempInx) " + $Item.Name
$TempInx = $TempInx + 1
}
}
break
}
}
$TempArr | Format-Wide {$_} -Column $Column -Force | Out-Host
}
function Get-UserInput {
<#
.DESCRIPTION
Will validate input base on what is requested in return.
It does some basic validation for integer and telephone number
and non-null/blank values
.EXAMPLE
To get a string return just use
Get-UserInput -DisplayText "<Enter something>"
To get a formatted telephone number
Get-UserInput -DisplayText "Enter a phone number" -PhoneSearch
To get a integer back (good for choice selectors)
Get-UserInput -DisplayText "Select a choice" -IntSearch -BoundSearch (option to limit choice)
To bound
To get a multiple choice return (for multiple selection)
Get-UserInput -DisplayText "Select a bunch of options" -MultiInt -MultiBound <upper limit of the choice>
#>
[CmdletBinding(DefaultParametersetName='None')]
param(
[Parameter(Mandatory=$true,
Position=0,
HelpMessage="Text for the type of input required")]
[String]$DisplayText,
[Parameter(
ParameterSetName="Int",
Mandatory=$false,
Position=1,
HelpMessage="Confirm input is an integer")]
[Switch]$IntSearch = $false,
[Parameter(
ParameterSetName="Int",
Mandatory=$false)]
[Int]$BoundSearch,
[Parameter(Mandatory=$false,
HelpMessage="Confirm input is a phone number")]
[Switch]$PhoneSearch = $false,
[Parameter(ParameterSetName="Multi",Mandatory=$false,
HelpMessage="Allow multiple integer selection with , or space as delimiter")]
[Switch]$MultiInt = $false,
[Parameter(ParameterSetName="Multi",Mandatory=$true,
HelpMessage="Upper limit of the input, and will discard anything higher")]
[Int]$MultiBound
)
do {
$InputData = Read-Host $DisplayText
if(($InputData -eq $null) -or ($InputData -eq "")) {
Write-Host "Error. Invalid input. Please try again."
$ValidInputData = $false
}
else {
if($IntSearch -eq $true) {
if((Test-Integer -TestObject $InputData) -eq $true) {
if($BoundSearch -eq 0){
$ValidInputData = $true
return $InputData
}
elseif(($BoundSearch -gt 0) -and ([Int]$InputData -le $BoundSearch)) {
$ValidInputData = $true
return $InputData
}
else {
Write-Host "Error. Invalid input. Please try again."
$ValidInputData = $false
}
}
else {
Write-Host "Error. Invalid input. Please try again."
$ValidInputData = $false
}
}
##Expect format of ( 555 ) 555 9999 or 555-555-999 or 5555559999 and cleans it to 613-555-9999
elseif($PhoneSearch -eq $true) {
if($InputData -match "\d{3}-\d{3}-\d{4}") {
$ValidInputData = $true
return $InputData
}
elseif(($InputData -match "\d{10}") -and ($InputData.Length -eq 10)) {
$ValidInputData = $true
return $InputData.ToString().Insert(3,"-").Insert(7,"-")
}
elseif(($InputData -match "\(\d{3}\) \d{3} \d{4}") -and ($InputData.Length -eq 14)) {
$ValidInputData = $true
return $InputData.ToString().Replace("(","").Replace(")","").Replace(" ","").Insert(3,"-").Insert(7,"-")
}
else {
Write-Host "Error. Invalid input. Please try again."
$ValidInputData = $false
}
}
#allows for mulltiple selection of int values, with " " or "," as delimiters
elseif($MultiInt -eq $true) {
$IntArray = @()
$InputData = $InputData.Replace(",", " ").Replace(" ", ";")
$SplitInput = $InputData.Split(";",[System.StringSplitOptions]::RemoveEmptyEntries)
Write-Host "`n`nValidating input."
foreach($Input in $SplitInput) {
try {
$Input = [Int]$Input
if([Int]$Input -le $MultiBound) {
$IntArray += [Int]$Input
}
else {
Write-Host "Discard invalid input of `"$Input`"."
}
}
catch {
Write-Host "Discard invalid input of `"$Input`"."
}
}
$ValidInputData = $true
$IntArray = $IntArray | Select-Object -Unique | Sort-Object
return $IntArray
}
else {
$ValidInputData = $true
return $InputData
}
}
}
while($ValidInputData -eq $false)
}
function Get-BaseOU {
<#
.DESCRIPTION
Searches the SearchBase for all OU and filters any OU that we don't want in the RemoveList.
.EXAMPLE
Use the default RemoveList
Get-BaseOU -SearchBase "OU=contoso,DC=contoso,DC=com"
Don't filter anything
Get-BaseOU -SearchBase "OU=contoso,DC=contoso,DC=com" -RemoveList @()
Change the filter to something else
Get-BaseOU -SearchBase "OU=contoso,DC=contoso,DC=com" -RemoveList @("FirstItem", "SecondItem")
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[String]$SearchBase,
[Parameter(Mandatory=$false)]
[System.Array]$RemoveList = @(
"test",
"Service Mailboxes"
"Computers"
)
)
if($RemoveList) {
[System.Collections.ArrayList]$ChildOUList = Get-ADOrganizationalUnit -Filter * -SearchBase $SearchBase -Properties * | Where-Object {$_.DistinguishedName -notmatch ($RemoveList -join "|") } #regex comparision that expand the array, into a singline line and using "|" as an or
}
else{
[System.Collections.ArrayList]$ChildOUList = Get-ADOrganizationalUnit -Filter * -SearchBase $SearchBase -Properties *
}
Format-DisplayList $ChildOUList -DisplayName
$OUCount = $ChildOUList.Count
$OUInput = Get-UserInput -DisplayText "Please select the location (ie. 1 - $OUCount)" -IntSearch -BoundSearch $OUCount
$ChildOUList[$OUInput-1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment