Powershell: Parse AD DistinguishedName: Difference between revisions
From Rabbi Blog
(Created page with "=Purpose= Using Powershell, break down the elements of the AD DistinguisedName value return. Ideally, use this has a launching point to handle CN, OU, DC, etc values from the...") |
m (→Code) |
||
Line 7: | Line 7: | ||
=Code= | =Code= | ||
<pre> | <pre> | ||
######################################################################################## | |||
# User Variables | |||
######################################################################################## | |||
$DN_ARRAY_FLIP=1; #Set to 1 to flip $DN_ARRAY before parsing | |||
######################################################################################## | |||
$AD_distinguishedName = 'CN=Some B. User,OU=Accounting,OU=Part Time,OU=Staff,OU=Users,DC=Rabbibob,DC=com' | $AD_distinguishedName = 'CN=Some B. User,OU=Accounting,OU=Part Time,OU=Staff,OU=Users,DC=Rabbibob,DC=com' | ||
write-host $AD_distinguishedName | write-host $AD_distinguishedName | ||
Line 16: | Line 24: | ||
$DN_Counter=0 | $DN_Counter=0 | ||
write-host "Size of Array:" $DN_array.Length | write-host "Size of Array:" $DN_array.Length "Flip Bit: $DN_ARRAY_FLIP" | ||
######################################################################################## | |||
# Check to see if the array should be flipped ($DN_ARRAY_FLIP) | |||
######################################################################################## | |||
if ($DN_ARRAY_FLIP -eq 1){ | |||
[array]::Reverse($DN_array) | |||
#Flipping the Array (for report from a heirarchy perspective) | |||
} | |||
foreach($DN_array_value in $DN_array) | foreach($DN_array_value in $DN_array) | ||
{ | { | ||
$DN_Counter++ | $DN_Counter++ | ||
if ($DN_array_value -match '(CN=)(.*)') { | |||
write-host "Matched CN at Position" $DN_Counter":" $Matches[2] } | |||
elseif ($DN_array_value -match '(DC=)(.*)') { | |||
write-host "Matched DC at Position" $DN_Counter":" $Matches[2] } | |||
elseif ($DN_array_value -match '(OU=)(.*)') { | |||
write-host "Matched OU at Position" $DN_Counter":" $Matches[2]} | |||
else { | |||
write-host "Unmatched Item: $DN_array_value " | |||
} | |||
} | } | ||
</pre> | </pre> | ||
Revision as of 14:19, 18 April 2019
Purpose
Using Powershell, break down the elements of the AD DistinguisedName value return. Ideally, use this has a launching point to handle CN, OU, DC, etc values from the resulting array.
Background
At first I wanted to do this via regex however at my level of knowledge it seemed to be a higher hurdle to break down the variable length input DN string. Luckily I realized that it's a comma separated value and it is nicely split by commas.
Code
######################################################################################## # User Variables ######################################################################################## $DN_ARRAY_FLIP=1; #Set to 1 to flip $DN_ARRAY before parsing ######################################################################################## $AD_distinguishedName = 'CN=Some B. User,OU=Accounting,OU=Part Time,OU=Staff,OU=Users,DC=Rabbibob,DC=com' write-host $AD_distinguishedName $DN_array=@() $AD_distinguishedName | Foreach{ $DN_array=$_.split(",") } $DN_Counter=0 write-host "Size of Array:" $DN_array.Length "Flip Bit: $DN_ARRAY_FLIP" ######################################################################################## # Check to see if the array should be flipped ($DN_ARRAY_FLIP) ######################################################################################## if ($DN_ARRAY_FLIP -eq 1){ [array]::Reverse($DN_array) #Flipping the Array (for report from a heirarchy perspective) } foreach($DN_array_value in $DN_array) { $DN_Counter++ if ($DN_array_value -match '(CN=)(.*)') { write-host "Matched CN at Position" $DN_Counter":" $Matches[2] } elseif ($DN_array_value -match '(DC=)(.*)') { write-host "Matched DC at Position" $DN_Counter":" $Matches[2] } elseif ($DN_array_value -match '(OU=)(.*)') { write-host "Matched OU at Position" $DN_Counter":" $Matches[2]} else { write-host "Unmatched Item: $DN_array_value " } }
Output
CN=Some B. User,OU=Accounting,OU=Part Time,OU=Staff,OU=Users,DC=Rabbibob,DC=com Size of Array: 7 Position 0 CN=Some B. User Position 1 OU=Accounting Position 2 OU=Part Time Position 3 OU=Staff Position 4 OU=Users Position 5 DC=Rabbibob Position 6 DC=com