How Can I Determine Whether Terminal Services is Enabled on a Windows Server 2003 Computer?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine whether Terminal Services is enabled on a Windows Server 2003 computer?

— DD

SpacerHey, Scripting Guy! AnswerScript Center

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 likely to be considered wiser and more intelligent when they get older. If that’s 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!

What that also means, DD, is this: if you’re willing to wait another 30 years or so we could give you a very wise, very intelligent answer to your question. If you aren’t willing to wait, then you’ll have to settle for this:

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_TerminalServiceSetting”)

For Each objItem in colItems If objItem.AllowTSConnections = 0 Then strStatus = “Disabled” Else strStatus = “Enabled” End If Wscript.Echo “Terminal Services status: ” & strStatus Next

As you can see, there really isn’t 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’t 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 – or pretty much any other WMI script you’ll find in the Script Center – 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:

strComputer = “atl-ts-01”

Needless to say, the ability to do remotely pretty much everything you can do locally is one of the cooler features of WMI.

After making the connection we use the following query to retrieve all the instances of the Win32_TerminalServiceSetting class:

Set colItems = objWMIService.ExecQuery _
    (“Select * from Win32_TerminalServiceSetting”)

Note. 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’t be used to determine if Terminal Services is enabled on a Windows 2000 computer. That shouldn’t be a problem for you, DD; you specifically asked about Terminal Services on Windows Server 2003. But it’s something for other people to keep in mind.

After we run the query we’ll 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 is 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 AllowTSConnections property, something we do here:

If objItem.AllowTSConnections = 0 Then

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 Disabled or Enabled to a variable named strStatus. We then use this line of code to echo back the Terminal Services status:

Wscript.Echo “Terminal Services status: ” & strStatus

That’s all there is to it.

Wait, wait: don’t say it. You were just about to ask if it’s possible to use a script to enable or disable Terminal Services, weren’t you? (Hmmm, maybe there is something to this correlation between grouchiness and intelligence.) You bet you can: all you have to do is call the SetAllowTSConnections method, passing the method one of two possible parameters:

•

0, to disable Terminal Services.

•

1, to enable Terminal Services.

For example, this script enables Terminal Services on the local computer:

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery _ (“Select * from Win32_TerminalServiceSetting”)

For Each objItem in colItems objItem.SetAllowTSConnections(1) Next

You know, you’re right: that was pretty nice of us to give you a bonus script, wasn’t it? Especially for as grouchy and cantankerous an old coot as the Scripting Guy who writes this column is. But we’re 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’s just a little misunderstood.

Yes, just like the Roman emperor Caligula; Attila the Hun; Jack the Ripper; and, of course, Rasputin, the Mad Monk.

0 comments

Discussion is closed.

Feedback usabilla icon