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
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