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

Terminal Services vbscript to enumerate sessions?

I am looking for a vbscript / wsh / wmi script to enumerate RDP and ICA sessions - terminal services connections to a Windows 2003 based Citrix  Metaframe server.

If I open up Terminal Services Manager, I get to see the username, session number and if the session is active or disconnected.

I am looking to gather the same info via vbscript or wmi?

Thanks,
Mark
0
markpalinux
Asked:
markpalinux
  • 3
  • 2
2 Solutions
 
RobSampsonCommented:
Hi, I think the Win32_SessionResource or Win32_SessionProcess classes will help you here....I'll have a look at it soon.

Regards,

Rob.
0
 
thegordoCommented:
Try this...

<package>
<job id="Sessions">
<comment>
File: Sessions.wsf
Description: List sessions in the farm.
Requirements: WSH 5.5 or higher.
Copyright (c) 2002 Citrix Systems, Inc.
</comment>
<runtime>
<description>
List sessions in the farm.
</description>
<example>
CScript //nologo Sessions.wsf
</example>

</runtime>
<reference object="MetaFrameCOM.MetaFrameFarm"/>
<script language="VBScript">

Dim theFarm, aSession, SessionState
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "c:\MFCOMreport"
strFile = "\session.txt"
strText = "Session Report"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "creating...... " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "creating........ " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8

SessionState = Array("Unknown", _
"Active", _
"Connected", _
"Connecting", _
"Shadowing", _
"Disconnected", _
"Idle", _
"Listening", _
"Resetting", _
"Down", _
"Init")

'
' Create MetaFrameFarm object
'

Set theFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
if Err.Number <> 0 Then
WScript.Echo "Can't create MetaFrameFarm object"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

'
' Initialize the farm object.
'

theFarm.Initialize(MetaFrameWinFarmObject)
if Err.Number <> 0 Then
WScript.Echo "Can't Initialize MetaFrameFarm object"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

'
' Are you Citrix Administrator?
'

if theFarm.WinFarmObject.IsCitrixAdministrator = 0 then
WScript.Echo "You must be a Citrix admin to run this script"
WScript.Echo ""
WScript.Quit 0
End If

'
' Print out the farm name.
'

WScript.Echo "MetaFrame Farm Name: " & theFarm.FarmName
WScript.Echo ""

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

' Writes strText every time you run this VBScript

' Display all sessions in the farm.
'

objTextFile.WriteLine("All sessions in the farm (" & Now & ")")
objTextFile.WriteLine ("------------------------------------------------")

For Each aSession In theFarm.Sessions

if aSession.ServerName = "Youservername" then

if Err.Number <> 0 Then
WScript.Echo "Can't enumerate sessions"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

objTextFile.WriteLine ("ServerName :" & aSession.ServerName)
objTextFile.WriteLine ("SessionName:" & aSession.SessionName)
objTextFile.WriteLine ("SessionID :" & CStr(aSession.SessionID))
objTextFile.WriteLine ("User :" & aSession.UserName)
objTextFile.WriteLine ("Client Name:" & aSession.ClientName)
objTextFile.WriteLine ("Client IP :" & aSession.ClientAddress)
objTextFile.WriteLine ("ClientBuild:" & aSession.ClientBuild)
objTextFile.WriteLine ("State :" & SessionState(aSession.SessionState))
objTextFile.WriteLine ("============================")

objTextFile.WriteLine ("")

end if

Next

objTextFile.WriteLine( "Total Sessions : " & theFarm.Sessions.count)

objTextFile.Close

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
WScript.Quit
'
</script>
</job>
</package>
0
 
RobSampsonCommented:
@thegordo, that's a great script....works well, except it was missing a constant for MetaFrameWinFarmObject

I have added that, and also taken the script out of the WSF file you provided, just to make it a stand-along VBS file.  I have also added a block at the top that re-runs the script in CScript if it is running in WScript by default.

'============
Dim strPath, strCommand
Dim theFarm, aSession, SessionState, strMFServer
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
Const MetaFrameWinFarmObject = 1

' Run the script via CScript if it is running in WScript
' Use %comspec% /k to leave the prompt open, Or
' use %comspec% /c to close it when finished.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strDirectory = "U:\Temp"
strFile = "\session.txt"
strText = "Session Report"

strMFServer = InputBox("Enter the name of the Metaframe server you wish to enumerate:", "Metraframe Server", "MCCPS04")

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "creating...... " & strDirectory
End If

If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "creating........ " & strDirectory & strFile
End If

set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8

SessionState = Array("Unknown", _
"Active", _
"Connected", _
"Connecting", _
"Shadowing", _
"Disconnected", _
"Idle", _
"Listening", _
"Resetting", _
"Down", _
"Init")

'
' Create MetaFrameFarm object
'

Set theFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
if Err.Number <> 0 Then
WScript.Echo "Can't create MetaFrameFarm object"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

'
' Initialize the farm object.
'
theFarm.Initialize(MetaFrameWinFarmObject)
if Err.Number <> 0 Then
WScript.Echo "Can't Initialize MetaFrameFarm object"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

'
' Are you Citrix Administrator?
'

if theFarm.WinFarmObject.IsCitrixAdministrator = 0 then
WScript.Echo "You must be a Citrix admin to run this script"
WScript.Echo ""
WScript.Quit 0
End If

'
' Print out the farm name.
'

WScript.Echo "MetaFrame Farm Name: " & theFarm.FarmName
WScript.Echo ""

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

' Writes strText every time you run this VBScript

' Display all sessions in the farm.
'

objTextFile.WriteLine("All sessions in the farm (" & Now & ")")
objTextFile.WriteLine ("------------------------------------------------")

For Each aSession In theFarm.Sessions

if aSession.ServerName = strMFServer then

if Err.Number <> 0 Then
WScript.Echo "Can't enumerate sessions"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

objTextFile.WriteLine ("ServerName :" & aSession.ServerName)
objTextFile.WriteLine ("SessionName:" & aSession.SessionName)
objTextFile.WriteLine ("SessionID :" & CStr(aSession.SessionID))
objTextFile.WriteLine ("User :" & aSession.UserName)
objTextFile.WriteLine ("Client Name:" & aSession.ClientName)
objTextFile.WriteLine ("Client IP :" & aSession.ClientAddress)
objTextFile.WriteLine ("ClientBuild:" & aSession.ClientBuild)
objTextFile.WriteLine ("State :" & SessionState(aSession.SessionState))
objTextFile.WriteLine ("============================")

objTextFile.WriteLine ("")

end if

Next

objTextFile.WriteLine( "Total Sessions : " & theFarm.Sessions.count)

objTextFile.Close

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
WScript.Quit
'============

Regards,

Rob.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thegordoCommented:
Excellent - makes it a better script - Thanks Rob!

Think this should now do the trick!
0
 
markpalinuxAuthor Commented:

Guys,

Thanks, hope I split the points ok.

Mark
0
 
RobSampsonCommented:
No problem with me.  Thanks.

Regards,

Rob.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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