PSTerminalServices – PowerShell module for Terminal Services
PSTerminalServices is a PowerShell module that helps you manage Terminal Services (including Remote Desktop connections) sessions and processes. No more legacy applications text parsing!
The module is based on an open source project named Cassia (version 2.0.0.60), a .NET library for accessing the native Windows Terminal Services API.
What you can do with it
The following operations are supported on local and remote computers:
- Enumerating terminal sessions and reporting session information including connection state, user name, client name, client display details, client-reported IP address, and client build number.
- Logging off a session.
- Disconnecting a session.
- Displaying a message box in a session.
- Enumerating all processes or processes for a specified session.
- Killing a process.
Supported Platforms
Cassia has been tested on Windows Server 2008 R2 beta (RTM?), Windows Server 2008, Windows Server 2003, Windows XP, and Windows Server 2000. It should work on Windows Vista as well.
Cassia - Source Files
Cassia source files are included in the module's bin directory (compressed file).
Functions
The following functions are added to the current session when you import the module:
Disconnect-TSSession - Disconnects any attached user from the session.
Get-TSCurrentSession - Provides information about the session in which the current process is running.
Get-TSServers - Enumerates all terminal servers in a given domain.
Get-TSProcess - Gets a list of processes running in a specific session or in all sessions.
Get-TSSession - Lists the sessions on a given terminal server.
Send-TSMessage - Displays a message box in the specified session ID.
Stop-TSProcess - Terminates the process running in a specific session or in all sessions.
Stop-TSSession - Logs the session off, disconnecting any user that might be connected.
Getting help
All functions are decorated with comment-based help so you can use Get-Help against any of the functions just like you would do with any other cmdlet. The module also include a help topic file, about_PSTerminalServices_Module, which you can read by typing the following in your current session:
PS > Get-Help about_PSTerminalServices_Module
Some Code Examples
For more code examples use Get-Help <cmdletName> –Examples.
# Logs off all the active sessions from remote computer 'comp1', no confirmations.
PS> Get-TSSession -ComputerName comp1 -State Active | Stop-TSSession –Force
# Gets all Active sessions from remote computer 'comp1', made from IP addresses that starts with '10'.
PS> Get-TSSession -ComputerName comp1 -Filter {$_.ClientIPAddress -like '10*' -AND $_.ConnectionState -eq 'Active'}
# Displays a message box inside all active sessions of computer name 'comp1'.
PS>$Message = "Important`n, the server is going down for maintenance in 10 minutes. Please save your work and logoff."
PS> Get-TSSession -State Active -ComputerName comp1 | Send-TSMessage -Message $Message
#Gets all processes connected to session ID 0 from remote computer 'comp1'.
PS>Get-TSSession -Id 0 -ComputerName comp1 | Get-TSProcess
Bugs/Suggestions
Bugs related to Cassia can be reported directly to its mailing list: cassia-users@googlegroups.com. Module Bugs/Suggestions/Comments can be reported to the module Discussions page.