Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

Windows Scripting Host - VBS

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
khampton
Asked:
khampton
  • 4
1 Solution
 
jnelson35Commented:
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
 
jnelson35Commented:
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
 
khamptonAuthor Commented:
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
 
jnelson35Commented:
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
 
jnelson35Commented:
So did this ever work for you?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now