WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADSI
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Check if a Computer Belongs to a Certain OU

Question:

I need to figure out if computer belong to certain OU. Following is the AD Path. How would I put it in winbatch.

Domain: mydomain.com

"OU1\OU2\Computers\Opsmanager" is the OU and I want to check if certain computer is in this OU. IF it is then I want to execute certain code and if not then other. Thanks

There are probably a bunch of ways to do this but here is one the comes to mind.

; Construct the full domain ADSI path.
ServerDn = dsGetProperty("LDAP://rootDSE", "serverName")
ServerName = ItemExtract(1, ServerDn, ",")
ServerName = ItemExtract(2, ServerName, "=")
ServerDn = dsGetProperty("LDAP://rootDSE", "defaultNamingContext")
ServerPath = "LDAP://%ServerName%/%ServerDN%"

; Find the conputers path
ComputerName = "TestComputer" 
ComputerPath = dsFindPath(ServerPath, "(&(objectCategory=computer)(cn=%ComputerName%))")

; Search the path for the OU
OuName = "TestOuTwo"
If StrIndexNC( ComputerPath, "OU=%OuName%",1, @FWDSCAN)
    Message("Found", "%ComputerName% is in %OuName%")
Else
    Message("Not Found", "%ComputerName% is NOT in %OuName%")
EndIf
Beyond, there be dragons
Article ID:   W16306
File Created: 2005:02:18:12:19:44
Last Updated: 2005:02:18:12:19:44