PSTerminalServices – PowerShell module for Terminal Services

February 22, 2010

21 comments

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.

 

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

21 comments

  1. Jon LundfeltJune 9, 2011 ב 19:34

    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.

    Reply
  2. ScriptFanaticJune 12, 2011 ב 11:49

    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\\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?

    Reply
  3. monocoFebruary 17, 2012 ב 08:53

    Hi folks,

    Any way of knowing all of the places I have left disconnected RDP Sessions?

    Fact:

    I connect to many many servers a day, and sometimes I might forget to logoff my RDP Session. It so happens that if I were to change my domain admin password and had disconnected RDP sessions with my old password all over the place, my account gets constantly locked.

    So a side from setting a Terminal Service GPO to kick disconnected sessions that have been idle for a determined period of time I would like to know if anyone knows how I could use SCOM or something to find all disconnected sessions given a determined NT User…

    Is there anything like that? Or will I have to do it myself? 😛

    Thank you very much!

    Reply
  4. ScriptFanaticFebruary 17, 2012 ב 15:00

    @monco

    I don’t know of a way to achieve this but you could execute this command against your servers:

    Get-Content .\Servers.txt | Foreach-Object{
    Get-TSSession -UserName Administrator -State Disconnected -ComputerName $_ | Stop-TSSession -Force
    }

    Reply
  5. LukeJune 12, 2012 ב 13:42

    Hi All,

    I am having the same problem as Jon Lundfelt had. When I try and install it gives the following error…

    Could not access network location
    \\srv01\home\luke\\WindowsPowerShell.

    Any ideas?

    Thanks.

    Reply
  6. ScriptFanaticJune 12, 2012 ב 14:16

    Hi Luke

    Do you have folder redirection enabled for your Documents folder? What’s the value of your $env:USERPROFILE?

    Reply
  7. LukeJune 12, 2012 ב 15:27

    Hi ScriptFanatic,

    Thanks for the quick reply!

    USERPROFILE=C:\Users\Luke.DOMAIN

    We do have folder redirection enabled.

    PS: Running Windows 8

    Reply
  8. ScriptFanaticJune 12, 2012 ב 15:36

    I see that the WindowsPowerShell folder has double forward slashes in front if it. What happens if you remove one?

    Reply
  9. LukeJune 12, 2012 ב 15:44

    I’m not sure how to change that? I can navigate to that location, no double forward slashes…

    \\srv01\home\luke\WindowsPowerShell

    However the msi installer seems to put an extra forward slash?

    In the registry my “HKCR\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal” key has the following value:

    \\srv01\home\Luke

    Reply
  10. ScriptFanaticJune 12, 2012 ב 15:56

    Luke, please download and install the latest version from http://psterminalservices.codeplex.com and let me know if the problem persists, thanks!(I will update the link in the post in a minute).

    Reply
  11. LukeJune 13, 2012 ב 09:35

    Hi ScripFanatic, downloaded that one and tried to install. Got a similar error:

    Could not access network location
    \\srv01\home\luke\\WindowsPowerShell\modules\PSTerminalServices

    Reply
  12. LukeJune 13, 2012 ב 10:51

    I logged on with a local account and installed the msi. Then logged back in with domain account, and copied the “WindowsPowerShell\modules” folder from the local account to my domain account.

    Working now.

    Reply
  13. ScriptFanaticJune 13, 2012 ב 11:07

    Thanks for the updates Luke. The installer let’s you change the installation folder during setup, have you tried it?

    Reply
  14. DamianJuly 30, 2012 ב 10:38

    Hi ScriptFanatic
    I downloaded v1.1 and got the similar folderredirection error. the option to change the installation folder does not shown because the error occours directly after setup lounches. is there an msiexec option to specify the install folder?

    Reply
  15. ScriptFanaticJuly 30, 2012 ב 14:38

    Hi Damian

    Please contact me via the Contact form (see top menu), I’ll send you a custom setup file.

    Reply
  16. TylerOctober 1, 2012 ב 17:13

    Has the download for this been removed or am I just not seeing it?

    Reply
  17. ScriptFanaticOctober 2, 2012 ב 11:16

    Thanks for he heads up @Tyler, I’ve fixed the links.

    Reply
  18. BabeJanuary 30, 2013 ב 23:36

    Hello,
    it is said that you can display the TS client display details but how can i manage this with Get-TSSession command? thanks

    Reply
  19. woejooi@gmail.comJuly 26, 2013 ב 16:28

    Howdy! This really is kind of off topic but I need some guidance from an established weblog. Is it extremely hard to set up your own weblog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to start. Do you have any ideas or suggestions? Appreciate it

    Reply
  20. EasterlingAugust 16, 2013 ב 17:18

    If you need enough performance to try out games and they
    are about the budget, it is possible to always go for this place particularly if you’re an NVIDIA fan. 9 average frames per second 1024 x 768 4x Anti aliasing 26. Video Card and Processor: If you are just like me, you should do some study with regards to the recording card along with the processor.

    Reply