Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert array of distinguished names to canonical names

2,247 views
Skip to first unread message

Zeunasc

unread,
Apr 11, 2012, 11:42:28 AM4/11/12
to
I have an array that is full of user's and computer's distinguished
names from Active Directory. How can I convert each element in the
array to the user's or computer's CN?

For example, if my array looks like this:

"CN=TestUser1,OU=TestOU,DC=abc,DC=com"
"CN=TestUser2,OU=TestOU,DC=abc,DC=com"
"CN=TestUser3,OU=TestOU,DC=abc,DC=com"
"CN=TestComputer1,OU=TestOU,DC=abc,DC=com"
"CN=TestComputer2,OU=TestOU,DC=abc,DC=com"
"CN=TestComputer3,OU=TestOU,DC=abc,DC=com"

I would like the resulting array to look like this:

TestUser1
TestUser2
TestUser3
TestComputer1
TestComputer2
TestComputer3

Thanks in advance!

David Trimboli

unread,
Apr 11, 2012, 5:05:09 PM4/11/12
to
On 4/11/2012 11:42 AM, Zeunasc wrote:
> "CN=TestUser1,OU=TestOU,DC=abc,DC=com"
> "CN=TestUser2,OU=TestOU,DC=abc,DC=com"
> "CN=TestUser3,OU=TestOU,DC=abc,DC=com"
> "CN=TestComputer1,OU=TestOU,DC=abc,DC=com"
> "CN=TestComputer2,OU=TestOU,DC=abc,DC=com"
> "CN=TestComputer3,OU=TestOU,DC=abc,DC=com"

If none of your canonical names have commas in them (e.g., "CN=Smith\,
John,OU=TestOU,DC=abc,DC=com"), this is easy:

$result = $myarray | foreach {$_ -split "," -match "^CN"} | foreach
{$_.substring(3)}

It's a little more complicated with possible commas. I don't quite have
time for it right now. Try matching "," but not "\\,".

--
David Trimboli
Windows Systems Analyst
Cold Spring Harbor Laboratory

Normandc

unread,
Apr 13, 2012, 3:11:06 PM4/13/12
to
I forget where i got this code from, it works great. the authors name is
in the code.

#Author: Glenn Sizemore gln...@get-admin.com
#
#Purpose: Convert a DN to a Canonical name, and back again.
#
#Example: PS > ConvertFrom-Canonical
'get-admin.local/test/test1/Sizemore, Glenn E'
# CN=Sizemore\, Glenn E,OU=test1,OU=test,DC=getadmin,DC=local
# PS > ConvertFrom-DN 'CN=Sizemore\, Glenn
E,OU=test1,OU=test,DC=getadmin,DC=local'
# get-admin.local/test/test1/Sizemore, Glenn E


function ConvertFrom-DN
{
param([string]$DN=(Throw '$DN is required!'))
foreach ( $item in ($DN.replace('\,','~').split(",")))
{
switch -regex ($item.TrimStart().Substring(0,3))
{
"CN=" {$CN = '/' + $item.replace("CN=","");continue}
"OU=" {$ou += ,$item.replace("OU=","");$ou += '/';continue}
"DC=" {$DC += $item.replace("DC=","");$DC += '.';continue}
}
}
$canoincal = $dc.Substring(0,$dc.length - 1)
for ($i = $ou.count;$i -ge 0;$i -- ){$canoincal += $ou[$i]}
$canoincal += $cn.ToString().replace('~',',')
return $canoincal
}

function ConvertFrom-Canonical
{
param([string]$canoincal=(trow '$Canonical is required!'))
$obj = $canoincal.Replace(',','\,').Split('/')
[string]$DN = "CN=" + $obj[$obj.count - 1]
for ($i = $obj.count - 2;$i -ge 1;$i--){$DN += ",OU=" + $obj[$i]}
$obj[0].split(".") | ForEach-Object { $DN += ",DC=" + $_}
return $dn
0 new messages