Monday, April 4, 2011

VB Script to list the current logged in user

The Scipt below reads computers from c:\temp\computers.txt and lists the user who is currently logged in.

'List user currently logged on to a desktop

WScript.Echo "List Currently logged in user for Computers in c:\temp\computers.txt"
On Error Resume Next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\temp\computers.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
 Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
Next
WScript.Echo "Script Finished"

No comments:

Post a Comment