Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

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.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,DC=Development,DC=MYK " _
        & "Where objectClass='computer'"  
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strcomputer =  objRecordSet.Fields("Name").Value
    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 & vbTab & objComputer.UserName
Next
    objRecordSet.MoveNext
Loop

Error.

C:\Logged in users.vbs(13, 1) Provider: Table does not exist.

regards
Sharath
Avatar of RobSampson
RobSampson
Flag of Australia image

Sharath, does the OU
Development.myk\Computers
actually exist?

Regards,

Rob.
Avatar of bsharath

ASKER

Yes it is correct...
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.
Looks like it's missing the closing quote, change
objCommand.CommandText = _
    "Select Name, Location from 'LDAP://OU=Computers,DC=Development,DC=MYK " _
        & "Where objectClass='computer'"  

to
objCommand.CommandText = _
    "Select Name, Location from 'LDAP://OU=Computers,DC=Development,DC=MYK' " _
        & "Where objectClass='computer'"

Regards,

Rob.
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.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,DC=Development,DC=MYK " _
        & "Where objectClass='computer'"  
objCommand.Properties("Searchscope") = 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:{impersonationLevel=impersonate}!\\" _
            & 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.Shell")
      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.
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'
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("DefaultNamingContext")
strOU = "OU=Test,"

'Start the ADO connection
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

'Set the ADO connection query strings
strBase = "<LDAP://" & strou & strDNSDomain & ">"
strFilter = "(objectCategory=computer)"
strAttributes = "distinguishedName,objectCategory,name"

'Create the Query
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strcomputer =  objRecordSet.Fields("Name").Value
    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 & 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.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,DC=Development,DC=MYK' " _
        & "Where objectClass='computer'"  
objCommand.Properties("Searchscope") = 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:{impersonationLevel=impersonate}!\\" _
            & 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.Shell")
      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.
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... :)
Oh, I missed an End If

'=============
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.CommandText = _
    "Select Name, Location from 'LDAP://OU=Computers,DC=Development,DC=MYK' " _
        & "Where objectClass='computer'"  
objCommand.Properties("Searchscope") = 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:{impersonationLevel=impersonate}!\\" _
            & 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.Shell")
      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.
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?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot Rob and Chandru...
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
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.