Prevent a VBS script from running on servers

Hello Everyone,

I have a VBScript that I use as a GPO logon script for users, I need to prevent the script from running on server in the event of one of the users logs in to a server. So I just need help writing an IF statement that determines the OS version and run it on Windows 7 or Windows 10 only.

Thanks in advance.
Mohamed AbdelmawlaAsked:
Who is Participating?
David Johnson, CD, MVPOwnerCommented:
Function FindOSType(ComputerName)
    'Defining Variables
    Dim objWMI, objItem, colItems
    Dim OSVersion, OSName, ProductType
    Set objWMI = GetObject( "winmgmts:\\" & ComputerName & "\root\cimv2" )
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
    For Each objItem in colItems
        ProductType = objItem.ProductType
    FindOSType = ProductType
    Set colItems = Nothing
    Set objWMI = Nothing
End Function

Dim OSType,strComputerName
Set wshShell = CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
OSType = FindOSType(strComputerName)
 If  OSType = 1 then
 'add your script here
 WScript.Echo("This is a Server... Aborting....")
End if

Open in new window

You can use this
systeminfo | findstr /C:"OS Name:"

Open in new window

slightwv (䄆 Netminder) Commented:
Start here:

Then look up additional Members in the Win32_OperatingSystem class:

I think what you want is:

    Data type: uint32
    Access type: Read-only

    Additional system information.

    Work Station (1)
    Domain Controller (2)
    Server (3)

Putting it together:
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") 
For Each objOS in colOSes 
  Wscript.Echo "Prduct Type: " & objOS.ProductType 

Open in new window

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

yo_beeDirector of Information TechnologyCommented:
I would handle this with a new GPO and move the logon script to this new GPO.
From there I would leverage WMI filtering.
Here are two screenshots that highlight what I am talking about.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I would just put a simple exclusion in by using item level targeting, or put a simple WMI filter in place and have the GPO use that WMI filter for where it applies.

You can use server name, or group membership, or OS to makw the exclusion directly in the group policy.

  of course you can put it in your script as well, whichever you prefer.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@ Yo Bee,  I am on mobile, and when I wrote that 4 hours ago, there was no posr by you, it just posted now. phpht.

in any case you saidwhat I came to say, and included screenshots I couldn't while mobile, so I certainly defer to you and would edit my post to note you dorectly if I could but, I'm on mobile.

have good mosh pitting.😃
yo_beeDirector of Information TechnologyCommented:

1: Please take the time to read previous replies before posting any of your suggestion. This will avoid any duplicative suggestion.  
2: GPP will not work here since the asker has a Logon Script and not applying a GPP.
3: The asker never mentioned what his script does. He just asked how to exclude it from running is the user logs onto a server.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@yo bee,

  Please see above.

yo_beeDirector of Information TechnologyCommented:
I was typing my reply before you posted your second comment.
That makes sense now.  Sorry if I offended you in anyway.
Mohamed AbdelmawlaAuthor Commented:
@David Johnson

Thanks a lot, that's exactly what I want :)
slightwv (䄆 Netminder) Commented:
>>Thanks a lot, that's exactly what I want :)

And pretty much what was posted 4 hours before?
David Johnson, CD, MVPOwnerCommented:
@netminder all your script does is display the product type. My answer goes one step further and checks if the product type is 1 which is a non-server OS
slightwv (䄆 Netminder) Commented:
I understand you added the additional IF statement.

I figured the OP had a certain level of knowledge and only needed to figure out how to determine if the script was running on a Workstation or not.

ProductType was the hard part.  IF statements should be simple.

If you want the points for an IF statement, enjoy.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.