{"id":226,"date":"2008-12-03T02:50:22","date_gmt":"2008-12-02T18:50:22","guid":{"rendered":"http:\/\/www.jhouseconsulting.com\/?p=226"},"modified":"2010-10-04T02:56:29","modified_gmt":"2010-10-03T18:56:29","slug":"using-wmi-to-detect-if-running-on-a-windows-2003-or-2008-terminal-server","status":"publish","type":"post","link":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/2008\/12\/03\/using-wmi-to-detect-if-running-on-a-windows-2003-or-2008-terminal-server-226","title":{"rendered":"Using WMI to detect if running on a Windows 2003 or 2008 Terminal Server"},"content":{"rendered":"<p>How can I detect if a Windows 2003\/2008 server is configured for Remote Administration or running the Terminal Server role\/service?<!--more--><\/p>\n<p>You can check the mode of the Windows Server 2003\/2008 server using the TS WMI Provider&#8217;s Win32_TerminalServiceSetting LicensingType property:<\/p>\n<p style=\"padding-left: 30px;\">0 = Personal Terminal Server \u2013 For Windows XP only<\/p>\n<p style=\"padding-left: 30px;\">1 = Remote Desktop for Administration<\/p>\n<p style=\"padding-left: 30px;\">2 = Per Device. Valid for application servers (true Terminal Servers)<\/p>\n<p style=\"padding-left: 30px;\">4 = Per User. Valid for application servers (true Terminal Servers)<\/p>\n<p style=\"padding-left: 30px;\">5 = Not configured yet. Valid for application servers (true Terminal Servers)<\/p>\n<p>Alternatively, but not recommended moving forward,\u00a0on a Windows 2003 server you can check the contents of the following registry DWORD value:<\/p>\n<p>HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\TSAppCompat<\/p>\n<p style=\"padding-left: 30px;\">0 = Remote Administration<\/p>\n<p style=\"padding-left: 30px;\">1 = Application Server mode<\/p>\n<p>Note that this key only will let you see what mode the server is in. To determine the Licensing Type, you would also need to query the following regitry DWORD value:<\/p>\n<p>HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\Licensing Core\\PolicyAcOn<\/p>\n<p style=\"padding-left: 30px;\">2 = Per Device configuration<\/p>\n<p style=\"padding-left: 30px;\">4 = Per User configuration<\/p>\n<p>To demontrate this I have\u00a0created two functions:<\/p>\n<ol>\n<li>isTerminalServer<\/li>\n<li>isTerminalServerAlternate<\/li>\n<\/ol>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nOption Explicit\r\n\r\nDim strOS\r\n\r\nstrOS = GetOSFamily()\r\n\r\nIf isTerminalServer(strOS) Then\r\n  wscript.echo &quot;This is a Terminal Server&quot;\r\nElse\r\n  wscript.echo &quot;This is not a Terminal Server&quot;\r\nEnd If\r\n\r\nIf ucase(strOS) = &quot;W2K3&quot; Then\r\n  If isTerminalServerAlternate Then\r\n    wscript.echo &quot;This is a Terminal Server&quot;\r\n  Else\r\n    wscript.echo &quot;This is not a Terminal Server&quot;\r\n  End If\r\nEnd If\r\n\r\nWScript.Quit(0)\r\n\r\nFunction GetOSFamily()\r\n  Dim strComputer, oWMIService, colOSInfo, oOSProperty, strCaption, strOSFamily\r\n  strComputer = &quot;.&quot;\r\n  Set oWMIService = GetObject(&quot;winmgmts:\\\\&quot; &amp; strComputer &amp; &quot;\\root\\cimv2&quot;)\r\n  Set colOSInfo = oWMIService.ExecQuery(&quot;Select * from Win32_OperatingSystem&quot;)\r\n  For Each oOSProperty in colOSInfo\r\n    strCaption = oOSProperty.Caption\r\n  Next\r\n  If InStr(1,strCaption, &quot;2008&quot;, vbTextCompare) Then strOSFamily = &quot;W2K8&quot;\r\n  If InStr(1,strCaption, &quot;2003&quot;, vbTextCompare) Then strOSFamily = &quot;W2K3&quot;\r\n  GetOSFamily = strOSFamily\r\n  Set oWMIService = Nothing\r\n  Set colOSInfo = Nothing\r\nEnd Function\r\n\r\nFunction isTerminalServer(strOS)\r\n  Dim strComputer, strNameSpace, objWMIService, colItems, objItem   \r\n  strComputer = &quot;.&quot;\r\n  If ucase(strOS) = &quot;W2K3&quot; Then\r\n    strNameSpace = &quot;\\root\\cimv2&quot;\r\n  Else\r\n    strNameSpace = &quot;\\root\\cimv2\\TerminalServices&quot;\r\n  End If\r\n  Set objWMIService = GetObject(&quot;winmgmts:&quot; _   \r\n    &amp; &quot;{impersonationLevel=impersonate}!\\\\&quot; &amp; strComputer &amp; strNameSpace)  \r\n  Set colItems = objWMIService.ExecQuery _  \r\n    (&quot;Select * from Win32_TerminalServiceSetting&quot;)  \r\n  For Each objItem in colItems  \r\n    Select Case objItem.LicensingType  \r\n      Case &quot;1&quot; ' Remote Administration\r\n        isTerminalServer = False\r\n      Case &quot;2&quot; ' Per Device\r\n        isTerminalServer = True\r\n      Case &quot;4&quot; ' Per User\r\n        isTerminalServer = True\r\n      Case &quot;5&quot; ' Not configured yet\r\n        isTerminalServer = True\r\n      Case Else\r\n        isTerminalServer = False  \r\n    End Select\r\n  Next  \r\n  Set objWMIService = Nothing  \r\n  Set colItems = Nothing  \r\nEnd Function\r\n\r\nFunction isTerminalServerAlternate\r\n  Dim objShell, strValue, strValueData\r\n  strValue = &quot;HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\TSAppCompat&quot;\r\n  Set objShell = CreateObject(&quot;WScript.Shell&quot;)\r\n  strValueData = objShell.RegRead(strValue)\r\n  If strValueData = &quot;1&quot; Then\r\n    isTerminalServerAlternate = True\r\n  Else\r\n    isTerminalServerAlternate = False\r\n  End If\r\n  Set objShell = Nothing\r\nEnd Function\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How can I detect if a Windows 2003\/2008 server is configured for Remote Administration or running the Terminal Server role\/service?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[5],"tags":[110,109],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-scripting","tag-detect-terminal-server-role","tag-wmi"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":7,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/posts\/226\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jhouseconsulting.com\/jhouseconsulting\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}