Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currently logged on user. 1

Status
Not open for further replies.
Apr 5, 2005
1,484
US
Issue: Same user shows up logged on each workstation. The script I am using should read a list of Workstation names and report who is logged on the PC. Problem is when running the script the first username shows for all PC. I must be missing something in the code.

Example of output:
UserName: domain\JSmith is logged in at computer wkr01
UserName: domain\JSmith is logged in at computer wkr10
UserName: domain\JSmith is logged in at computer wkr35
UserName: domain\JSmith is logged in at computer wkr02
UserName: domain\JSmith is logged in at computer wkr17

Code:
On Error Resume Next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\Scripts\WorkStations.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, VbCrLf)

For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next
Next
 
To better understand what happen you may comment out the On Error Resume Next instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV - I did that and when the script is trying to connect to one of the workstations I am getting the following error: Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: 'GetObject'

This list of workstations contains about 50 entries for a paticular subnet. I am assuming that the 'On Error Resume Next' allows the script to move on, should one of the workstations in the list be off-line.

Regarding the User, my theory is when it enumerates a PC with user jdoe logged on and then moves to another machine, that is online but no one logged on, it is reporting the user from the last objItem.UserName property.

I am going to write some error handling in the script to see if I can debug it.
I saw Marks sample code in another post but I am having the same issue with his code. POST = PC\User Audit.

If I use the code with strCompter = "computername" Everything works great, Just don't want to manualy enter 50 entries.

Hope someone has experience this issue or has an alternative method to try...

 
Question - is there a way to clear the objItem.UserName propery before the script moves to the next workstation?
 
you may try this:
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next
[!]Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing[/!]
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV- Thanks a million, that was what I was looking for. The script runs without without duplicate user names and now show a blank if there is no user logged onto the system...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top