Solved

Windows Scripting Host - VBS

Posted on 2004-04-08
7
339 Views
Last Modified: 2012-06-21
Using WSH - VBS, I am trying to list all computers attached to a domain as well as the username logged on to that computer.

I have been successful in listing the computers:

Sub GetDomainComputers(sDomName)
      Dim objDomain, i, objComputer
      i = 1
      'Create handle to ADSI object for specified domain
      Set objDomain = GetObject("WinNT://" & sDomName)
      'Remove all entities except computers
      objDomain.filter = Array("Computer")
      'Populate global array with computer names
      For Each objComputer In objDomain
            DomainComputers(i) = objComputer.Name
            i=i+1
      Next
      set objDomain = nothing
      set objComputer = nothing
End Sub

BUT - how do I list the user who is logged onto that machine and onto the domain (if one is indeed logged on).

In other words, I want to make a snapshot report of a domain, all of its computers and who is logged in thru each computer.

Thanks!
0
Comment
Question by:khampton
  • 4
7 Comments
 

Expert Comment

by:jnelson35
ID: 10786909
This may be kind of slow on a large network, but should work.
-------------------------------------------------------------

Sub GetDomainComputers(sDomName)
     Dim objDomain, i, objComputer
     i = 1
     'Create handle to ADSI object for specified domain
     Set objDomain = GetObject("WinNT://" & sDomName)
     'Remove all entities except computers
     objDomain.filter = Array("Computer")
     'Populate global array with computer names
     For Each objComputer In objDomain
          DomainComputers(i) = objComputer.Name
          WScript.Echo GetLoggedInUser(objComputer.Name, sDomName)
          i=i+1
     Next
     set objDomain = nothing
     set objComputer = nothing
End Sub

'-------------------------------------------------
Function GetLoggedInUser(strCompName,strDomName)
'-------------------------------------------------
'make remote WMI connection to machine
Set objWMIService = GetObject("winmgmts:\\" & strCompName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkLoginProfile",,48)
'loop through each profile to see if the specified domain name can be found in the "Name" attribute (So that NT AUTHORITY\SYSTEM and the like aren't flagged)
For Each objItem in colItems
     If Instr(objItem.Name, strDomName) Then
          GetLoggedInUser = objItem.Name
          Exit For
     Else
          GetLoggedInUser = "No user logged in"
     End If
Next
0
 

Expert Comment

by:jnelson35
ID: 10786924
Oops...just saw something in the function I wrote...use this revised version

'-------------------------------------------------
Function GetLoggedInUser(strCompName,strDomName)
'-------------------------------------------------
'make remote WMI connection to machine
Set objWMIService = GetObject("winmgmts:\\" & strCompName & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkLoginProfile",,48)
'loop through each profile to see if the specified domain name can be found in the "Name" attribute (So that NT AUTHORITY\SYSTEM and the like aren't flagged)
For Each objItem in colItems
     If Instr(objItem.Name, strDomName) Then
          strUserName = objItem.Name
          Exit For
     Else
          strUserName = "No user logged in"
     End If
Next
GetLoggedInUser = strUserName
0
 
LVL 1

Author Comment

by:khampton
ID: 10807962
For some reason, I can't connect to the local computer via:

Set objWMIService = GetObject("winmgmts:\\" & strCompName & "\root\cimv2")

I get a long delay (5 seconds or so) and then a non discript error message.
0
 

Accepted Solution

by:
jnelson35 earned 250 total points
ID: 10808226
Sorry, make sure you have an "End Function" on the line following GetLoggedInUser = strUserName...I seem to have left that out.

If it is still not working, try using this line instead of what we had above:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strCompName & "\root\cimv2")

If that still does not work, make sure the account you're running the script under has local admin priveleges on the remote machine.
0
 

Expert Comment

by:jnelson35
ID: 10891103
So did this ever work for you?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now