21 May 2011

RDS Session / Server Management via Powershell

Once upon a time there was a cool wrapper dll for accessing TS session info from scripts and .NET programs without accessing the complex and poorly documented WTS API. The dll was called WTSadmin.dll. Unfortunately this assembly does not work any more on 2008 R2 RDS. Googling and binging sent me to a project named Cassia, which is in fact another wrapper for the WTS API. And theeeeeere.. is a Powershell module using the Cassia wrapper. It can be downloaded here.

I prefer to use the zip file and copy the module folder into %systemroot%\system32\WindowsPowerShell\V1.0\Modules

After that you can import the module manually or using a profile.

import-module  PSTerminalServices

And here is a little example which extracts the client IP address of the current user’s session (also works for ICA sessions of course)

$username = $env:USERNAME
$session = Get-TSSession | ? { $_.UserName -match $username} | select *
If ($session)
   $clientip = $session.ClientIPAddress.ToString()
   write-host "Client IP is: $clientip"

If you want to manage the RDS Configuration you have to import the RemoteDesktopServices module (built in after RDS role install)

import-module RemoteDesktopServices

After importing the module you can browse the RDS configuration as a file system.

Getting config items:

ls RDS:\RDSConfiguration

Adding a license server

cd RDS:\RDSConfiguration\LicensingSettings\SpecifiedLicenseServers
New-Item -name licensehost.domain.com

More info about RDS:\ provider here