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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
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

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.

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure 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 SaaS Infrastructure 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 SaaS Infrastructure 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.
David Johnson, CDRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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, CDRetiredCommented:
@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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.