DCSIMG
PSTerminalServices – PowerShell module for Terminal Services - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

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:

  1. 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.
  2. Logging off a session.    
  3. Disconnecting a session.   
  4. Displaying a message box in a session.
  5. Enumerating all processes or processes for a specified session.
  6. 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.

 

Comments

Powershell module for Terminal Services « said:

Pingback from  Powershell module for Terminal Services &laquo;

# February 26, 2010 7:08 AM

Episode 104 – Keith Garner on MDT 2010 « PowerScripting Podcast said:

Pingback from  Episode 104 &#8211; Keith Garner on MDT 2010 &laquo;  PowerScripting Podcast

# March 8, 2010 6:35 AM

Arnaud PETITJEAN said:

# May 17, 2010 12:51 AM

Shay Levy said:

My new friends Christa Anderson and Kristin L. Griffin wrote the Microsoft Windows Server 2008 R2 Remote

# November 23, 2010 9:41 PM

Scripting – How to disconnect Remote Desktop sessions? Drija said:

Pingback from  Scripting &#8211; How to disconnect Remote Desktop sessions? Drija

# May 6, 2011 7:13 AM

Jon Lundfelt said:

I think there is a problem with the installer.. It seems to query the shell folder for My Documents

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal

And appends '\\WindowsPowerShell' to that path, which failes-

Product: PSTerminalServices -- Error 1606. Could not access network location \\myserver\mycifsshare\My Documents\\WindowsPowerShell.

# June 9, 2011 7:34 PM

ScriptFanatic said:

Hi Jon,

The installer extract the module files to your profile (user module). The 'Select Installation Folder' dialog should show a path similar to (Windows 7):

C:\Users\<UserName>\Documents\WindowsPowerShell\

Looks like your profile doesn't reside locally on your machine (folder redirection, though there's shouldn't be a problem with that).

What happens if you remove the extra backslash? Does it help?

# June 12, 2011 11:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: