Edward Sheriff Curtis
Edward Sheriff Curtis

Reputation: 497

Check if users are logged on your workstation using VBScript

I need a solution to execute a script to check if users are logged on your workstation.

I have stored the list of worstation to question on the file serverlist_login.txt

If the user are logged, we write the username in the file results_login.txt.

I have tried without success this script, but for all workstation my username always appears as a user logged into the workstation. This is not possible

The file results_login.txt output:

Login Results
 Date And Time : 24/08/2021 11:59:39

 User is connected  : worstation_01
MyDomain\MyUserName
 User is connected  : worstation_02
MyDomain\MyUserName
 User is connected  : worstation_03
MyDomain\MyUserName

Any suggestion?

Const ForReading = 1, ForWrite = 2, ForAppend = 8

Set Fso = CreateObject("Scripting.FileSystemObject")    
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set systems = wmi.ExecQuery("select * from Win32_ComputerSystem")

If Fso.FileExists(Fso.GetParentFolderName(WScript.ScriptFullName) & "\serverlist_login.txt") Then

Set Ts = Fso.OpenTextFile("serverlist_login.txt", ForReading)
strText = Ts.ReadAll
Ts.Close

For Each i In Split(strText, vbCrLf)
   For Each system In systems
      If system.UserName <> "" Then
         Results1 = Results1 & " User is connected  : " & i & vbCrLf & system.username & vbCrLf 
      Else
         Results2 = Results2 & " No user is connected : " & i & vbCrLf
      End If 
   Next
Next

Set Ts = Fso.CreateTextFile("results_login.txt")
Ts.WriteLine "Login Results" & vbCrLf & " Date And Time : " & Now & Vbcrlf
Ts.WriteLine Results1
Ts.WriteLine Results2
Ts.Close()  

Else
   MsgBox vbTab &_
      "Error" & vbcrlf &_
      "Missing, the server.txt to process. You" & vbCrLf & _
      "must create a servers.txt with an IP or" & vbCrLf & _
      "Computer Name, with one per line",4128,"Error"   
End If

Update #01

For Each i In Split(strText, vbCrLf)
   For Each system In Wmi.ExecQuery("select * from Win32_ComputerSystem where Name = '" & i & "'")
      If system.UserName <> "" Then
         Results1 = Results1 & " User is connected  : " & i & vbCrLf & system.username & vbCrLf 
      Else
         Results2 = Results2 & " No user is connected : " & i & vbCrLf
      End If 
   Next
Next

Upvotes: 0

Views: 272

Answers (1)

leeharvey1
leeharvey1

Reputation: 1436

To illustrate the loop in question, since comments prevent proper formatting:

For Each i In Split(strText, vbCrLf)
   Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & i & "\root\cimv2")
   Set colItems = wmi.ExecQuery("select * from Win32_ComputerSystem")
   For Each Item In colItems
      If Item.UserName <> "" Then
         Results1 = Results1 & " User is connected  : " & i & vbCrLf & Item.username & vbCrLf 
      Else
         Results2 = Results2 & " No user is connected : " & i & vbCrLf
      End If 
   Next
Next

Do you see where variable i replaces the period . above?

Also, your variable systems is misleading you. This is actually just a collection of items returned by WMI for the current system.

So essentially, the main loop above enumerates all systems i in your text file; then connects to those systems via wmi (impersonating your user account context); then pulls a collection of Win32_ComputerSystem items colItems from that system; and finally, enumerates each item in the collection to see which usernames are found.

Upvotes: 1

Related Questions