How Can I Tell Whether a Logon Script is Running on a Workstation or on a Server?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell whether a logon script is running on a workstation or on a server? If the script is running on a server, I want to skip a portion of the script.

— RT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RT. You know, a few years ago one of the Scripting Guys helped a fellow Microsoft employee set up a computer lab that this other Microsoft employee needed for testing courseware materials. After getting the lab set up the other Microsoft employee set out to test “computer lockdown,” using a combination of group policy, logon scripts, and command-line tools to see if he could keep anyone from doing anything to the workstation machines. His intention was to determine just how severely you could restrict activities on a computer.

Well, guess what: you can restrict activities so severely that no one can do anything on a computer, not even log on. In other words, the experiment was a complete success. Well, except for one thing. In his zeal to test lockdown tasks this employee didn’t differentiate between workstations and servers; as a result, he ended up locking down all the computers in the lab (including the domain controllers). That meant he couldn’t log on to any of the machines in his test domain. And, yes, that meant we had to reformat all the computers and set the lab up all over again.

Which is simply our way of saying, yes, sometimes it can be very useful to differentiate between workstations and servers.

So how can you tell whether or not a given machine is a workstation or a server? Here’s how:

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

Set colComputers = objWMIService.ExecQuery _ (“Select * from Win32_ComputerSystem”)

For Each objComputer in colComputers If objComputer.DomainRole < 2 Then Wscript.Echo “This is a workstation.” Else Wscript.Echo “This is a server.” End If Next

This script starts off by connecting to the WMI service on the local computer. (We should point out that, although you specifically mentioned running this as a logon script, the script could also easily be modified to run against a remote computer). We then use this line of code to bind to the Win32_ComputerSystem class:

Set colComputers = objWMIService.ExecQuery _
    (“Select * from Win32_ComputerSystem”)

Why the Win32_ComputerSystem class? Well, as it turns out this class has a property named DomainRole that can tell us the “role” played by a computer. DomainRole returns one of the following values:




Standalone Workstation


Member Workstation


Standalone Server


Member Server


Backup Domain Controller


Primary Domain Controller

As you can see, if the DomainRole is either 0 or 1 then we’re dealing with a workstation.

With that in mind, all we have to do is set up a For Each loop to walk through the collection of “computer systems” returned by our query (there will be only one item in the collection). Inside that For Each loop we check to see whether or not the value of DomainRole is 0 or 1 (or, in other words, if it is less than 2) and then echo the appropriate message:

For Each objComputer in colComputers
    If objComputer.DomainRole < 2 Then
        Wscript.Echo “This is a workstation.”
        Wscript.Echo “This is a server.”
    End If

Of course, instead of just echoing back the computer role you’ll probably want to run a specific chunk of code depending on whether the machine is a workstation or server. But we’ll let you take care of that part yourself. Just try not to completely lock everyone out of all your computers, OK?

And, yes, you’re right: that’s something only a Microsoft employee would do. But – for once, at least – we didn’t do it. (For some reason, any time something dumb happens people immediately think of the Scripting Guys. Wonder why ….)


Discussion is closed.

Feedback usabilla icon