Challenge of Day

Thursday, January 22, 2009

How to find out your computer's DN in your Active Directory

Hello,
From time to time you need to know your DN (Distingueshed Name) for one of the objects (for example a computer) in your Network Directory or simply Active Directory in Microsoft terminology.
Usually here is DN for any given computer name (e.g. ALI) in your domain (e.g. BARIDSOFT.CA)
CN=ALI, CN=COMPUTERS, DN=BARIDSOFT, DN=CA
Here is a very simple VBScript that can help you find out exact DN for the computer name. To run this script, save it as SCRIPT.VBS and double click on it in Windows Explorer:

'------------------------------------------------------------
Dim sDNSDomain, oTrans, sNetBiosDomain, sAdsPath, sComputer, oUser
Dim WshNetwork : Set WshNetwork = WScript.CreateObject("WScript.Network")

' Determine DNS domain name from RootDSE object.
Dim oRoot : Set oRoot = GetObject("LDAP://RootDSE")
sDNSDomain = oRoot.Get("DefaultNamingContext")
' Use the NameTranslate object to find the NetBIOS
' domain name from the DNS domain name.
Set oTrans = CreateObject("NameTranslate")
oTrans.Init 3, sDNSDomain
oTrans.Set 1, sDNSDomain
sNetBIOSDomain = oTrans.Get(3)

' Use NameTranslate to convert the NT user name to the
' DistinguishedName required for the LDAP provider.
oTrans.Init 1, Left(sNetBIOSDomain, _
Len(sNetBIOSDomain) - 1)
oTrans.Set 3, sNetBIOSDomain & sNTName
sAdsPath = oTrans.Get(1)

' Bind to the user object in AD with the LDAP provider.
Set oUser = GetObject("LDAP://" & sAdsPath)

' Use NameTranslate to convert the NT name of the computer
' to the DistinguishedName required by LDAP.
' Computer names must end with "$".
sComputer = InputBox("Please enter Computer Name, hit enter if Local PC.","Enter PC Name", "Local")'
If ucase(sComputer) = "LOCAL" then
sComputer = WshNetwork.ComputerName
ElseIf IsEmpty(sComputer) Then
Msgbox "No Name, Exit"
wscript.Quit
End If

oTrans.Set 3, sNetBIOSDomain & sComputer & "$"
sAdsPath = oTrans.Get(1)

' Bind to the computer object in AD with the LDAP provider.
Set oComputer = GetObject("LDAP://" & sAdsPath)
'Should be able to add objComputer.DeleteObject (0) here to delete the PC.

msgbox sAdsPath


'-------------------------------------------------------------
Also there is another useful tool, name is ADSIEdit.msc which you can find it in "c:\program files\support tools" folder. This MMC will give you a complete list of DN of your directory.

Be a winner in your daily challenges!
Ali Khademi
P.S.: Do not forget to read and follow Happy Family weblogs at http://happyfamily.persianblog.ir

2 Comments:

At October 4, 2010 at 6:22 PM , Blogger Unknown said...

Nice Post about PowerBuilder Development Thanks for sharing this.

 
At September 20, 2017 at 11:10 AM , Anonymous Anonymous said...

Thank you!

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home