• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 768
  • Last Modified:

Find all Mstsc sessions a user in at the moment.

Hi,

I have some machine names in the file. I want a script that can query with the machines and get me if any machine the user is logged in through Mstsc.If any get the machine names and usernames if possible they have logged in with.

Regards
Sharath
0
bsharath
Asked:
bsharath
  • 4
1 Solution
 
jahboiteCommented:
Try this.

Create a text file with the following vbscript and save it as something.vbs
In the same place, have your file of machine names - each machine name on a new line. (You'll need to change MY_LIST_OF_MACINE_NAMES.txt in the script)

Run the .vbs script by double-clicking it. ;)

I've basically modified this script from one I found at:
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/00d0436700ace684/2656005404a33b5d

I've tested the script, but you may have issues with permissions on remote machines - I'm not an expert in WMI - on line:
Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
but have a go and see what happens.

Heres the script:

const ForReading = 1
const ForWriting = 2

myfile="MY_LIST_OF_MACINE_NAMES.txt"
logname="mstsc_log.txt"

set fso = createobject("scripting.filesystemobject")
set srcFile = fso.opentextfile(myfile,ForReading)

strAll = srcFile.ReadAll
srcFile.Close
arrComputers = Split(strAll, vbCrLf)

set logfile = fso.CreateTextFile(logname, ForWriting)

For Each strComputer in arrComputers
      if strComputer <> "" then
            Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colSessions = objWMI.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 10")
            logfile.writeline colSessions.Count & " users found logged in to " & strComputer

            If colSessions.Count <> 0 Then
               logfile.writeline "RDP Sessions:"
               For Each objSession in colSessions    
                 Set colList = objWMI.ExecQuery("Associators of " & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
                 For Each objItem in colList
                   logfile.writeline "Username: " & objItem.Name & " FullName: " & objItem.FullName
                 Next
               Next
            End If
      end if
Next

logfile.close

Wscript.Echo "Finished!"
0
 
jahboiteCommented:
and i should have added, just for the sake of clarity, that the results will be found in the same folder in a file named:
mstsc_log.txt
0
 
bsharathAuthor Commented:
Hi,

Will this script find machines that the user has connected from his machine.Or how many different machines has been connected to the machine name in the list.

Machine name > different machine
Machinename < From different machine
If the machine is Switched off i get an error.
0
 
jahboiteCommented:
Hi bsharath,

I cannot find a way to find the machine name - I've tried adding domain, but I can't test this as I'm not joined to a domain - On my machine it returns the machine name - On yours I expect it will return the domain so not much help.

I'm not exactly sure of what you're now asking - lost in translation!

To check the local machine, put "localhost" (without the quotes) into your list of machines.

I've modified the script to ping machines before trying to query them:

const ForReading = 1
const ForWriting = 2

myfile="list.txt"
logname="mstsc_log.txt"

set fso = createobject("scripting.filesystemobject")
set srcFile = fso.opentextfile(myfile,ForReading)

strAll = srcFile.ReadAll
srcFile.Close
arrComputers = Split(strAll, vbCrLf)

set logfile = fso.CreateTextFile(logname, ForWriting)

Set objShell = CreateObject("WScript.Shell")
      
For Each strComputer in arrComputers
      if strComputer <> "" then
            Set objExecObject = objShell.Exec ("%comspec% /c ping -n 3 -w 1000 " & strComputer)
            Do While Not objExecObject.StdOut.AtEndOfStream
                  strText = objExecObject.StdOut.ReadAll()
                  If Instr(strText, "Reply") > 0 Then
                        Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                        Set colSessions = objWMI.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 10")
                        logfile.writeline strComputer & ": " & colSessions.Count & " logged in user(s)."
                        If colSessions.Count <> 0 Then
                              For Each objSession in colSessions    
                                    Set colList = objWMI.ExecQuery("Associators of " & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
                                    For Each objItem in colList
                                          logfile.writeline "   Username: " & objItem.Name & " FullName: " & objItem.FullName & " Domain(Not sure about this bit!!): " & objItem.domain
                                    Next
                              Next
                        End If
                  Else
                        logfile.writeline strComputer & ": did not respond to a ping."
                  End If
            Loop
      end if
      logfile.writeline
Next

logfile.close

Wscript.Echo "Finished!"
0
 
jahboiteCommented:
Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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