{"id":66363,"date":"2006-09-29T14:51:00","date_gmt":"2006-09-29T14:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/29\/how-can-i-determine-whether-terminal-services-is-enabled-on-a-windows-server-2003-computer\/"},"modified":"2006-09-29T14:51:00","modified_gmt":"2006-09-29T14:51:00","slug":"how-can-i-determine-whether-terminal-services-is-enabled-on-a-windows-server-2003-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-whether-terminal-services-is-enabled-on-a-windows-server-2003-computer\/","title":{"rendered":"How Can I Determine Whether Terminal Services is Enabled on a Windows Server 2003 Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I determine whether Terminal Services is enabled on a Windows Server 2003 computer?<BR><BR>&#8212; DD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DD. You know, it turns out to be true: good things <I>will<\/I> come to those who wait. According to the front page of <I>The Seattle Times<\/I>, people who are grouchy and cantankerous today are more likely to be considered wiser and more intelligent when they get older. If that\u2019s true, and if we project his current level of grouchiness forward, in another 30 years or the Scripting Guy who writes this column should be working on his third or fourth Nobel Prize. Vindication at last!<\/P>\n<P>What that also means, DD, is this: if you\u2019re willing to wait another 30 years or so we could give you a very wise, very intelligent answer to your question. If you <I>aren\u2019t<\/I> willing to wait, then you\u2019ll have to settle for this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_TerminalServiceSetting&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If objItem.AllowTSConnections = 0 Then\n        strStatus = &#8220;Disabled&#8221;\n    Else\n        strStatus = &#8220;Enabled&#8221;\n    End If\n    Wscript.Echo &#8220;Terminal Services status: &#8221; &amp; strStatus\nNext\n<\/PRE>\n<P>As you can see, there really isn\u2019t much to this script. We start out, as we usually do, by connecting to the WMI service on the local computer. Fortunately, this script can be easily modified to connect to the WMI service on a remote computer; after all, you probably don\u2019t need to go to all the trouble of writing a script in order to determine whether Terminal Services is enabled on your local machine. To connect to a remote computer using this script \u2013 or pretty much any other WMI script you\u2019ll find in the Script Center \u2013 just assign the name of that remote machine to the variable strComputer. For example, this line of code results in the script connecting to the WMI service on the computer atl-ts-01:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ts-01&#8221;\n<\/PRE>\n<P>Needless to say, the ability to do remotely pretty much everything you can do locally is one of the cooler features of WMI.<\/P>\n<P>After making the connection we use the following query to retrieve all the instances of the <B>Win32_TerminalServiceSetting<\/B> class:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_TerminalServiceSetting&#8221;)\n<\/PRE>\n<TABLE id=\"ERD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. This is probably a good time to mention that the Win32_TerminalServiceSetting class is only available on computers running Windows Server 2003 or Windows XP. Among other things, that means this script can\u2019t be used to determine if Terminal Services is enabled on a Windows 2000 computer. That shouldn\u2019t be a problem for you, DD; you specifically asked about Terminal Services on Windows Server 2003. But it\u2019s something for other people to keep in mind.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we run the query we\u2019ll get back a collection of all the Terminal Services settings for the computer in question. Although there will be only one item in the collection (that is, one group of Terminal Services settings), this <I>is<\/I> a collection; that means we still need to set up a For Each loop in order to get at the individual items and property values. In particular, we want to examine the value of the <B>AllowTSConnections<\/B> property, something we do here:<\/P><PRE class=\"codeSample\">If objItem.AllowTSConnections = 0 Then\n<\/PRE>\n<P>If AllowTSConnections is equal to 0 that means Terminal Services is disabled; if AllowTSConnections is equal to 1 that means Terminal Services in enabled. All we do in the remainder of the script is check the value of AllowTSConnections and then, based on that value, assign the string <I>Disabled<\/I> or <I>Enabled<\/I> to a variable named strStatus. We then use this line of code to echo back the Terminal Services status:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Terminal Services status: &#8221; &amp; strStatus\n<\/PRE>\n<P>That\u2019s all there is to it.<\/P>\n<P>Wait, wait: don\u2019t say it. You were just about to ask if it\u2019s possible to use a script to enable or disable Terminal Services, weren\u2019t you? (Hmmm, maybe there <I>is<\/I> something to this correlation between grouchiness and intelligence.) You bet you can: all you have to do is call the <B>SetAllowTSConnections<\/B> method, passing the method one of two possible parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>0<\/B>, to disable Terminal Services.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>1<\/B>, to enable Terminal Services.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>For example, this script enables Terminal Services on the local computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_TerminalServiceSetting&#8221;)<\/p>\n<p>For Each objItem in colItems\n    objItem.SetAllowTSConnections(1)\nNext\n<\/PRE>\n<P>You know, you\u2019re right: that <I>was<\/I> pretty nice of us to give you a bonus script, wasn\u2019t it? Especially for as grouchy and cantankerous an old coot as the Scripting Guy who writes this column is. But we\u2019re going to let you in on a little secret: the Scripting Guy who writes this column is nowhere near as grouchy and hard-to-deal-with as his reputation would suggest. Instead, like many great figures in history, he\u2019s just a little misunderstood. <\/P>\n<P>Yes, just like the Roman emperor Caligula; Attila the Hun; Jack the Ripper; and, of course, Rasputin, the Mad Monk.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine whether Terminal Services is enabled on a Windows Server 2003 computer?&#8212; DD Hey, DD. You know, it turns out to be true: good things will come to those who wait. According to the front page of The Seattle Times, people who are grouchy and cantankerous today are more [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,130,722,5],"class_list":["post-66363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-servers","tag-terminal-server","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine whether Terminal Services is enabled on a Windows Server 2003 computer?&#8212; DD Hey, DD. You know, it turns out to be true: good things will come to those who wait. According to the front page of The Seattle Times, people who are grouchy and cantankerous today are more [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66363"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}