返回数据类似以下内容:
Image Name PID Session Name Session# Mem Usage
============ ===== ============= ======== =========
EXCEL.EXE 23084 Console 1 58,064 K
我可以用来检索会话名的WMI等效命令 /属性吗?使用win32_process类,我可以得到诸如name,sessionID,ProcessID和CreateDate之类的东西。如果我也可以抓住session名称,那就太好了。
实际上,有一个CMD命令可以获取SessionName
:
您可以看到第二行中有一个“>”。 sose sesseconname如果您的意思是此图片的“控制台”,则可以使用此脚本:
' 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
或如果要获取用户名(第2列),则可以使用以下方式:
(额外的代码用于删除额外的空间)
' 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