Tuesday, November 8, 2011

VBS Get the OU distinguishedName of the OU containing a User Account

I had an interesting task which I needed to achieve. I had another peice of code grabbing a bunch of user distinguishedName's in Active Directory. For each distinguishedName I needed to grab the OU/container the user account resides in. For example:

"CN=Clint Boessen,OU=Microsoft MVPs,OU=Engineers,OU=IT Professionals,DC=4Logic,DC=local"

We need to cut the "CN=Clint Boessen" of this string in order to determine the organisational unit so we get:

"OU=Microsoft MVPs,OU=Engineers,OU=IT Professionals,DC=4Logic,DC=local"

However sometimes the user accounts are in a container not an OU so we need code smart enough to deal with this.. for example:

"CN=Clint Boessen,CN=Users,DC=4Logic,DC=local"

To be come:

"CN=Users,DC=4Logic,DC=local"

The VB Script below will take care of this... as long as you store the distinguishedName within the strOU attribute.

For example:

strOU = "CN=Clint Boessen,OU=Microsoft MVPs,OU=Engineers,OU=IT Professionals,DC=4Logic,DC=local"

If instr(lcase(strDN),"cn=users") = 0 then
strOU = right(strDN,len(strDN) - instr(lcase(strDN),",ou="))
Else
strOU = right(strDN,len(strDN) - instr(lcase(strDN),",cn=users"))
End If

1 comment:

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

    ReplyDelete