Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get computer description using ADO Active Directory

Status
Not open for further replies.

Murtasma

Programmer
Jul 21, 2006
3
US
I am having a really hard time getting the computer description from an Active Directory computer account to work. I understand that ADO retruns an array for the description so I took that into account when I created my script. I am having a problem on line 43 of my script which is this line

arrDesc = objRecordSet.Fields("description").Value
The error I receive is
ADODB.Fields: Item cannot be found in the collection corresponding to the requested name or ordinal.

I used ADSI edit and description is a valid field for a computer object.

PLEASE PLEASE PLEASE Help me or Shoot me :)


'On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
dim objShell
dim objScriptExec
dim strPingResults
dim fso
dim CSVFile
dim strFileName

strFileName = "C:\temp\Logged-On-Users.csv"
Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(strFileName) Then
'File is present so delete it
fso.DeleteFile(strFileName)
Wscript.Echo "Creating File: " & strFileName
Set CSVFile = fso.CreateTextFile(strFileName, True)
Else
Wscript.Echo "Creating File: " & strFileName
Set CSVFile = fso.CreateTextFile(strFileName, True)
End If


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 Name, Location from 'LDAP://OU=Computers,OU=Michigan Administrative Information Systems (MA), DC=bf,DC=umich,DC=edu' " & "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000 'make this larger if there is more then 1000 computers in AD
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

dim strMember
dim arrDesc

Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value

arrDesc = objRecordSet.Fields("description").Value
if isArray(objRecordSet.Fields("description")) then
wscript.echo "It's an array"
for each strMember in arrDesc
wscript.echo strMember
Next

else
wscript.echo "No"
end if

strComputer = objRecordSet.Fields("Name").Value
Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)

strPingResults = LCase(objScriptExec.StdOut.ReadAll)

If InStr(strPingResults, "reply from") Then
wscript.echo "Machine: " & strComputer & " responded to ping."
Set objWMIService = GetObject("winmgmts:"&"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
Wscript.Echo strComputer
Wscript.Echo "User: " & objComputer.UserName
CSVFile.WriteLine (strComputer & "," & objComputer.UserName)
Next
wscript.echo ""
wcript.echo ""
Else
'the machine did not respond to ping. Mark the machine as offline in the CSV file
CSVFile.WriteLine (strComputer & "," & "Offline")
wscript.echo "Machine: " & strComputer & " is currently offline."
wscript.echo ""
wcript.echo ""
End If
objRecordSet.MoveNext 'Move to the next record
Loop
CSVFile.Close 'Close the CSV file
 
Well I just went ahead and did a LDAP query instead of fooling around with ADO since I didn't have time for these silly games. hehe

Here is the completed script

It pings all the machines in the OU to make sure they are working then it connectings using WMI to see what user is currently logged on the machine.



On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
dim objShell
dim objScriptExec
dim strPingResults
dim fso
dim CSVFile
dim strFileName
dim objComputer
dim objProperty
dim strDesc

strFileName = "C:\temp\Logged-On-Users.csv" 'File used for output
'Create the shell object that will be used to run ping command to test connection before attempting
'to authenicate with WMI
Set objShell = CreateObject("WScript.Shell")
'Create the file system object to work with CSV file
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(strFileName) Then
'File is present so delete it
fso.DeleteFile(strFileName)
Wscript.Echo "Creating File: " & strFileName
Set CSVFile = fso.CreateTextFile(strFileName, True)
Else
Wscript.Echo "Creating File: " & strFileName
Set CSVFile = fso.CreateTextFile(strFileName, True)
End If

'Setup the ADO object to be used to Querty AD and get a list of machines
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 Name, Location from 'LDAP://OU=Computers,OU=Michigan Administrative Information Systems (MA), DC=bf,DC=umich,DC=edu' " & "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000 'make this larger if there is more then 1000 computers in AD
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute 'Run the query
objRecordSet.MoveFirst 'Move to the first record



Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
'Run a LDAP query on the computer object this is needed to return the computer description
Set objComputer = GetObject("LDAP://CN=" & objRecordSet.Fields("Name").Value & ",OU=Computers,OU=Michigan Administrative Information Systems (MA), DC=bf,DC=umich,DC=edu")
'Get the computer description for the object
objProperty = objComputer.Get("Description")
If IsNull(objProperty) Then
Wscript.Echo "The description has not been set."
strDesc = "" 'make sure strDesc is blank
Else
Wscript.Echo "Description: " & objProperty
strDesc = objProperty 'Set strDesc to the description this will be used when writing to the CSV file
objProperty = Null
End If

strComputer = objRecordSet.Fields("Name").Value 'Store the computer name
'Run the ping command on the machine
Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut.ReadAll) 'Store the results of the ping command

If InStr(strPingResults, "reply from") Then
wscript.echo "Machine: " & strComputer & " responded to ping."
'Connect to WMI and run the Query
Set objWMIService = GetObject("winmgmts:"&"{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
Wscript.Echo strComputer
Wscript.Echo "User: " & objComputer.UserName
CSVFile.WriteLine (strComputer & "," & objComputer.UserName & "," & strDesc)
strDesc = ""
Next
wscript.echo ""
wscript.echo ""
Else
'the machine did not respond to ping. Mark the machine as offline in the CSV file
CSVFile.WriteLine (strComputer & "," & "Offline" & "," & strDesc)
strDesc = ""
wscript.echo "Machine: " & strComputer & " is currently offline."
wscript.echo ""
wscript.echo ""
End If
objRecordSet.MoveNext 'Move to the next record
Loop
CSVFile.Close 'Close the CSV file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top