I am writing a script to write the computer name ("CN") attribute of computer accounts in an OU of Active Directory into description field of the computer account but I am just getting blank or no value for CN attribute. I had attached the image of the code execution results (which shows blank value – i have four computers with the different computer name in the specified OU). Please help to check my code (I have hidden the Active Directory path which shows my company details) and advise me where I did I made a mistake. Thanks.
‘Create connection to the Active Directory and to do Deep Search (throughout all child OUs)
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 3000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
‘Select area (OU) where the operation that we wants to operates from
objCommand.CommandText ="SELECT AdsPath FROM ‘LDAP://OU=Temp,OU=MAC,OU=Clients,OU=TEST Device Accounts,DC=TEST,DC=TEST,DC=TEST’ WHERE objectCategory=’computer’"
Set objRecordSet = objCommand.Execute
‘Go to the first record (AD account) in the specified OU
‘continue operation until the last record (AD Account) in the specified OU
Do Until objRecordSet.EOF
‘Declare few string variables to hold time values
‘set string named strUserPath to "AdsPath" value of a AD accounts’ properties, AdsPath = Active Directory Path
strUserPath = objRecordSet.Fields("AdsPath").Value
‘Get the AD object and assign it to objEntry
strmessage = "The computer name is :"&valuename
wscript.Echo("The computer name is :"&valuename)
objEntry.Description = strmessage objEntry.Setinfo objRecordSet.MoveNext
Source: Windows Questions