j2associates
j2associates

Reputation: 1155

How do I find the Task Manager Session Name using WMI

I am using the following VBScript code to run a TaskList to query Task Manager information

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "CMD /C TaskList /FI ""ImageName eq Excel.exe"""

It returns data something like the following:

Image Name     PID Session Name  Session# Mem Usage
============ ===== ============= ======== =========
EXCEL.EXE    23084 Console              1  58,064 K

Is there a WMI equivalent Command / Property that I can use to retrieve the Session name? Using the Win32_Process class, I can get things like Name, SessionID, ProcessID and CreationDate. It would be nice if I could grab the SessionName also.

Upvotes: 0

Views: 36

Answers (1)

Rouhollah Ahmadi
Rouhollah Ahmadi

Reputation: 33

Actually there is a cmd command to get SessionName:

query session

that will return something like this: output of query session

You can see there is a ">" in second line.

So by SessionName if you mean "Console" for this picture you can use this script:

' Create a WScript Shell object
Set objShell = CreateObject("WScript.Shell")

' Execute the "query session" command and capture the output
Set objExec = objShell.Exec("query session")

' Read the output from the command
strOutput = objExec.StdOut.ReadAll()

' Split the output into lines
arrLines = Split(strOutput, vbCrLf)

' Loop through each line to find the current session
For Each strLine in arrLines
    ' Check if the line contains the current session (marked with ">")
    If InStr(strLine, ">") > 0 Then
        ' Split the line into columns (assuming columns are separated by spaces)
        arrColumns = Split(strLine)
        
        ' The session name is the first column (after the ">")
        strSessionName = arrColumns(0)
        
        ' Remove the ">" character from the session name
        strSessionName = Replace(strSessionName, ">", "")
        
        ' Output the session name to the user
        WScript.Echo "Current Session Name: " & strSessionName
        
        ' Exit the loop once the current session is found
        Exit For
    End If
Next

or if you want to get username (Column 2) you can use this: (Extra code is for deleting extra spaces)

' Create a WScript Shell object
Set objShell = CreateObject("WScript.Shell")

' Execute the "query session" command and capture the output
Set objExec = objShell.Exec("query session")

' Read the output from the command
strOutput = objExec.StdOut.ReadAll()

' Split the output into lines
arrLines = Split(strOutput, vbCrLf)

' Loop through each line to find the current session
For Each strLine in arrLines
    ' Trim leading and trailing spaces from the line
    strLine = Trim(strLine)
    
    ' Check if the line contains the current session (marked with ">")
    If InStr(strLine, ">") > 0 Then
        ' Split the line into columns (using spaces as delimiters)
        arrColumns = Split(strLine, " ")
        
        ' Initialize a counter to find the username
        Dim i
        i = 0
        
        ' Loop through the columns to find the username
        For Each strColumn in arrColumns
            ' Skip empty columns (caused by multiple spaces)
            If strColumn <> "" Then
                ' The username is the second non-empty column
                If i = 1 Then
                    strUsername = strColumn
                    Exit For
                End If
                i = i + 1
            End If
        Next
        
        ' Output the username to the user
        WScript.Echo "Current Username: " & strUsername
        
        ' Exit the loop once the current session is found
        Exit For
    End If
Next

Upvotes: 1

Related Questions