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
    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

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

Open in new window

0
 
slightwv (䄆 Netminder) 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

0
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.


  gpo1.png
gpo2.png
1
 
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.
0
 
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.😃
0
 
yo_beeDirector of Information TechnologyCommented:
@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.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@yo bee,

  Please see above.

Thanks.❤
1
 
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.
1
 
Mohamed AbdelmawlaAuthor Commented:
@David Johnson

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

And pretty much what was posted 4 hours before?
0
 
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
0
 
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.
0
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.