bsharath
asked on
Get an error with this script.
Hi,
I got this script from EE.Which has to get all the computers and logged in users.
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio n = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK " _
& "Where objectClass='computer'"
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name" ).Value
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo strcomputer & vbTab & objComputer.UserName
Next
objRecordSet.MoveNext
Loop
Error.
C:\Logged in users.vbs(13, 1) Provider: Table does not exist.
regards
Sharath
I got this script from EE.Which has to get all the computers and logged in users.
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
objCommand.Properties("Sea
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name"
Set objWMIService = GetObject("winmgmts:{imper
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo strcomputer & vbTab & objComputer.UserName
Next
objRecordSet.MoveNext
Loop
Error.
C:\Logged in users.vbs(13, 1) Provider: Table does not exist.
regards
Sharath
ASKER
Yes it is correct...
ASKER
Rob i just changed it to some other OU and i get this...
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
NDC01
ADS-SRV02
C:\Logged in users.vbs(18, 5) Microsoft VBScript runtime error: The remote serve
r machine does not exist or is unavailable: 'GetObject'
There is a OU called Computers .I think thats the default one which has all the computers.For this it does not work.
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
NDC01
ADS-SRV02
C:\Logged in users.vbs(18, 5) Microsoft VBScript runtime error: The remote serve
r machine does not exist or is unavailable: 'GetObject'
There is a OU called Computers .I think thats the default one which has all the computers.For this it does not work.
Looks like it's missing the closing quote, change
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK " _
& "Where objectClass='computer'"
to
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK' " _
& "Where objectClass='computer'"
Regards,
Rob.
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
to
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
Regards,
Rob.
ASKER
The quote is there Rob i think while copying i missed it....
OK, if that's the default object, then it's a container, not an OU, so change the
OU=Computers
to
CN=Computers
Also, for the error with connecting to a PC, this will overcome that:
'==============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio n = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK " _
& "Where objectClass='computer'"
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name" ).Value
If Ping(strcomputer) = True Then
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel l")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'===============
Regards,
Rob.
OU=Computers
to
CN=Computers
Also, for the error with connecting to a PC, this will overcome that:
'==============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
objCommand.Properties("Sea
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name"
If Ping(strcomputer) = True Then
Set objWMIService = GetObject("winmgmts:{imper
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'===============
Regards,
Rob.
ASKER
I think you missed the Quotes.
I get this now...
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
NDC01
ADS-SRV02
C:\Logged in users.vbs(20, 11) Microsoft VBScript runtime error: The remote serv
er machine does not exist or is unavailable: 'GetObject'
I get this now...
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
NDC01
ADS-SRV02
C:\Logged in users.vbs(20, 11) Microsoft VBScript runtime error: The remote serv
er machine does not exist or is unavailable: 'GetObject'
ASKER
Any help with these Q....
https://www.experts-exchange.com/questions/22893275/Add-Administrator-to-the-defalt-shares-in-the-remote-machine.html
https://www.experts-exchange.com/questions/22892700/Add-a-user-to-the-managed-by-for-all-computers-in-the-file.html
https://www.experts-exchange.com/questions/22891060/Script-to-use-as-logon-script-that-will-update-the-last-logged-in-username-in-the-Managed-by-tab-of-the-computer.html
https://www.experts-exchange.com/questions/22891052/How-can-i-update-the-computers-location-tab-from-the-users-office-tab.html
https://www.experts-exchange.com/questions/22864808/Any-way-to-Query-the-Ads-Users-properties-for-Mailbox-Rights.html
https://www.experts-exchange.com/questions/22863160/Need-to-change-the-sessions-tab-for-all-users-in-the-file.html
This one you have answered but just need some changes.
https://www.experts-exchange.com/questions/22886457/Event-retrieve-on-selected-dae-and-time-Pinging-Rob.html
https://www.experts-exchange.com/questions/22893275/Add-Administrator-to-the-defalt-shares-in-the-remote-machine.html
https://www.experts-exchange.com/questions/22892700/Add-a-user-to-the-managed-by-for-all-computers-in-the-file.html
https://www.experts-exchange.com/questions/22891060/Script-to-use-as-logon-script-that-will-update-the-last-logged-in-username-in-the-Managed-by-tab-of-the-computer.html
https://www.experts-exchange.com/questions/22891052/How-can-i-update-the-computers-location-tab-from-the-users-office-tab.html
https://www.experts-exchange.com/questions/22864808/Any-way-to-Query-the-Ads-Users-properties-for-Mailbox-Rights.html
https://www.experts-exchange.com/questions/22863160/Need-to-change-the-sessions-tab-for-all-users-in-the-file.html
This one you have answered but just need some changes.
https://www.experts-exchange.com/questions/22886457/Event-retrieve-on-selected-dae-and-time-Pinging-Rob.html
Can you try this version?
' Start getting a list of all servers from AD
' Determine DNS domain name from RootDSE object.
On Error Resume Next
Set objRootDSE = GetObject("LDAP://RootDSE" )
strDNSDomain = objRootDSE.Get("DefaultNam ingContext ")
strOU = "OU=Test,"
'Start the ADO connection
Set objCommand = CreateObject("ADODB.Comman d")
Set objConnection = CreateObject("ADODB.Connec tion")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnectio n = objConnection
'Set the ADO connection query strings
strBase = "<LDAP://" & strou & strDNSDomain & ">"
strFilter = "(objectCategory=computer) "
strAttributes = "distinguishedName,objectC ategory,na me"
'Create the Query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Pag e Size") = 100
objCommand.Properties("Tim eout") = 30
objCommand.Properties("Cac he Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name" ).Value
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo strcomputer & vbTab & objComputer.UserName
Next
objRecordSet.MoveNext
Loop
' Start getting a list of all servers from AD
' Determine DNS domain name from RootDSE object.
On Error Resume Next
Set objRootDSE = GetObject("LDAP://RootDSE"
strDNSDomain = objRootDSE.Get("DefaultNam
strOU = "OU=Test,"
'Start the ADO connection
Set objCommand = CreateObject("ADODB.Comman
Set objConnection = CreateObject("ADODB.Connec
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnectio
'Set the ADO connection query strings
strBase = "<LDAP://" & strou & strDNSDomain & ">"
strFilter = "(objectCategory=computer)
strAttributes = "distinguishedName,objectC
'Create the Query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Pag
objCommand.Properties("Tim
objCommand.Properties("Cac
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name"
Set objWMIService = GetObject("winmgmts:{imper
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo strcomputer & vbTab & objComputer.UserName
Next
objRecordSet.MoveNext
Loop
Oh yeah, had the error checking in the wrong spot....
'=============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio n = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK' " _
& "Where objectClass='computer'"
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name" ).Value
If Ping(strcomputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" _
& strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
On Error GoTo 0
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " has no user logged in."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel l")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'=============
Regards,
Rob.
'=============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
objCommand.Properties("Sea
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name"
If Ping(strcomputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{imper
& strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
On Error GoTo 0
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " has no user logged in."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'=============
Regards,
Rob.
ASKER
Chandru for your script it goes not reading but no results.
Rob...
I get this.
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
C:\Logged in users.vbs(42, 7) Microsoft VBScript compilation error: Expected 'End'
Guys any help with the excel user creation...Sorry to bug you but i have those 300 user creations on hold... :)
Rob...
I get this.
C:\>cscript "Logged in users.vbs"
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
C:\Logged in users.vbs(42, 7) Microsoft VBScript compilation error: Expected 'End'
Guys any help with the excel user creation...Sorry to bug you but i have those 300 user creations on hold... :)
Oh, I missed an End If
'=============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio n = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De velopment, DC=MYK' " _
& "Where objectClass='computer'"
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name" ).Value
If Ping(strcomputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{imper sonationLe vel=impers onate}!\\" _
& strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
On Error GoTo 0
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " has no user logged in."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
End If
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel l")
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'=============
Regards,
Rob.
'=============
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnectio
objCommand.CommandText = _
"Select Name, Location from 'LDAP://OU=Computers,DC=De
& "Where objectClass='computer'"
objCommand.Properties("Sea
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strcomputer = objRecordSet.Fields("Name"
If Ping(strcomputer) = True Then
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{imper
& strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " could not be contacted."
Err.Clear
On Error GoTo 0
Else
On Error GoTo 0
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objComputer in colComputer
If Err.Number <> 0 Then
Wscript.Echo strcomputer & " has no user logged in."
Err.Clear
On Error GoTo 0
Else
Wscript.Echo strcomputer & vbTab & objComputer.UserName
On Error GoTo 0
End If
Next
End If
Else
'MsgBox strcomputer & " could not be pinged."
End If
objRecordSet.MoveNext
Loop
Function Ping(strComputer)
Dim objShell, boolCode
Set objShell = CreateObject("WScript.Shel
boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
If boolCode = 0 Then
Ping = True
Else
Ping = False
End If
End Function
'=============
Regards,
Rob.
ASKER
Thanks Rob this works..
I get results like this...
-------------------------- -
Windows Script Host
-------------------------- -
CHEN-PC107 DEVELOPMENT\Man
-------------------------- -
OK
-------------------------- -
Can i get the results to a file please....
What way does it get the results Rob .last login?
I get results like this...
--------------------------
Windows Script Host
--------------------------
CHEN-PC107 DEVELOPMENT\Man
--------------------------
OK
--------------------------
Can i get the results to a file please....
What way does it get the results Rob .last login?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot Rob and Chandru...
ASKER
Rob small query..
Does this script query with each machine from the objects in the OU.Or just gets the info from the ADS.
What i mean is does the script go to each machine to get the info?
For some machines i get as "Could not be contacted" what does this mean
Does this script query with each machine from the objects in the OU.Or just gets the info from the ADS.
What i mean is does the script go to each machine to get the info?
For some machines i get as "Could not be contacted" what does this mean
Yes, it gets the info directly from the computer, so if the computer cannot be contacted, it will unable to determine a username.
Regards,
Rob.
Regards,
Rob.
Development.myk\Computers
actually exist?
Regards,
Rob.