Prevent a VBS script from running on servers

Mohamed Abdelmawla
Mohamed Abdelmawla used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

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

Open in new window

Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
Start here:
https://gallery.technet.microsoft.com/scriptcenter/df14b0d5-d8d3-4502-8250-dfff7e2f66fe

Then look up additional Members in the Win32_OperatingSystem class:
https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx

I think what you want is:
ProductType

    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 
Next 

Open in new window

yo_beeDirector of Information Technology

Commented:
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.


  gpo1.png
gpo2.png
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
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 Engineer

Commented:
@ 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 Technology

Commented:
@Ben

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 Engineer

Commented:
@yo bee,

  Please see above.

Thanks.❤
yo_beeDirector of Information Technology

Commented:
I was typing my reply before you posted your second comment.
That makes sense now.  Sorry if I offended you in anyway.
Top Expert 2016
Commented:
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
    Next
    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
  '
 Else
 WScript.Echo("This is a Server... Aborting....")
End if

Open in new window

Author

Commented:
@David Johnson

Thanks a lot, that's exactly what I want :)
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
>>Thanks a lot, that's exactly what I want :)

And pretty much what was posted 4 hours before?
Top Expert 2016

Commented:
@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
Most Valuable Expert 2012
Distinguished Expert 2018

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial