SearchObject-ReturnCanonicalName (VBS)

This Script receives a computer name and returns it's canonical name.


Download The Script

 


 '=*=*=*=*=*=*=*=*=*=*=*=*==*=*==*=*==*=

' Created by Assaf Miron

' Http://assaf.miron.googlepages.com

' Date : 16/9/2008

' SearchObjects-ReturnCanonicalName.vbs

'=*=*=*=*=*=*=*=*=*=*=*=*==*=*==*=*==*=

Function FindObject(strObj,ObjClass)

Const ADS_SCOPE_SUBTREE = 2

Dim objRootDSE,objConnection,objCommand,objRecordSet

Dim strDomainLdap


Set objRootDSE = GetObject ("LDAP://rootDSE")

strDomainLdap  = objRootDSE.Get("defaultNamingContext")

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand = CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.CommandText = _

"SELECT AdsPath FROM 'LDAP://" & strDomainLdap & "' WHERE objectClass='" & ObjClass & "' and Name='" &_

strObj & "'"

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Timeout") = 30

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.Properties("Cache Results") = False

Set objRecordSet = objCommand.Execute

If objRecordSet.RecordCount = 0 Then 

FindObject= 0

Else 

objRecordSet.Requery

objRecordSet.MoveFirst

Do Until objRecordSet.EOF

FindObject= objRecordSet.Fields("AdsPath").Value

objRecordSet.MoveNext

Loop


End If

End Function 


Set objFound = GetObject(FindObject(WScript.Arguments(0),WScript.Arguments(1)))


objFound.GetInfoEx Array("canonicalName"), 0

arrCanonicalName = objFound.GetEx("canonicalName")

 

For Each strValue in arrCanonicalName

  WScript.Echo strValue

Next


Comments