Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Windows Scripting Host - VBS

Posted on 2004-04-08
7
Medium Priority
?
351 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

705 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