Solved

Windows Scripting Host - VBS

Posted on 2004-04-08
7
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.

738 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