[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get an error with this script.

Posted on 2007-10-15
17
Medium Priority
?
276 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:bsharath
  • 9
  • 7
17 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 20082858
Sharath, does the OU
Development.myk\Computers
actually exist?

Regards,

Rob.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20082872
Yes it is correct...
0
 
LVL 11

Author Comment

by:bsharath
ID: 20082886
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 65

Expert Comment

by:RobSampson
ID: 20082888
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20082891
The quote is there Rob i think while copying i missed it....
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20082915
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20082930
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'
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20082953
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20082970
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20082987
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... :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20083012
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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20083040
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?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 20083060
Yes, it gets the results from the last user to register on the computer name.  I believe that this persists even at a reboot if no user has logged in yet, it would just display the previous person to log in.

Try this to a file speicified by strFile:

'=============
Const ADS_SCOPE_SUBTREE = 2

strFile = "Last_User_Logged_On.csv"

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

strResults = "Computer,User"

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
                strResults = strResults & VbCrLf & strcomputer & ",INVALID"
                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
                        strResults = strResults & VbCrLf & strcomputer & ",INVALID"
                        Wscript.Echo strcomputer & " has no user logged in."
                        Err.Clear
                        On Error GoTo 0
                  Else
                        strResults = strResults & VbCrLf & strcomputer & "," & objComputer.UserName
                        Wscript.Echo strcomputer & vbTab & objComputer.UserName
                        On Error GoTo 0
                  End If
            Next
          End If
      Else
                  strResults = strResults & VbCrLf & strcomputer & ",INVALID"
            'MsgBox strcomputer & " could not be pinged."
      End If
      objRecordSet.MoveNext
Loop

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile(strFile, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing
Set objFSO = Nothing

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.
0
 
LVL 11

Author Comment

by:bsharath
ID: 20083075
Thanks a lot Rob and Chandru...
0
 
LVL 11

Author Comment

by:bsharath
ID: 20083112
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20083119
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question