Solved

Windows Scripting Host - VBS

Posted on 2004-04-08
7
338 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

10 Experts available now in Live!

Get 1:1 Help Now