Scripting: Check if a W2K3 box is running Terminal Server in Application Mode

I was recently asked (two hours ago) how to tell if a server was running Terminal Services in Application Mode.  The customer wanted to run a different script if users were logged into a Terminal Server.

They had looked through the registry and came across the TSEnabled value in :

HKLM\Software\System\CurrentControlSet\Control\Terminal Server

While this key does indicate whether or not TS is enabled, it does not tell you if the server is in Application Mode.  To compound the issue this key is also set to 1 by default on Windows XP.  So, surely there was a more appropriate way to check?  Indeed there is … the Win32_TerminalServiceSetting WMI class will allow you to check.  See the code below:

Dim strComputer, objWMIService, colClass, objClass, strTSMode

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colClass = objWMIService.ExecQuery("Select * from Win32_TerminalServiceSetting")

For Each objClass in colClass

    strTSMode = objClass.TerminalServerMode

    If strTSMode = 1 Then

        Wscript.Echo "Terminal Server is in Application Server mode."

    Else

        Wscript.Echo "Terminal Server is in Remote Administration mode."

    End If

Next

Note: This will not work under Windows 2000 as the WMI class does not exist.  I have not checked it in Windows 2008.