Loading...
 

Greg`s Tech blog

IsServerOS.vbs

Thursday 27 of March, 2008
More on the corporate logon script.
We needed a way to prevent to script from running if we were logging onto a server. We developed a vbscript to test the ProductName registry key for the presence of the word "server". This works for at least Windows 2000 & 2003. Probably will work for 2008, probably not for NT 4. Neither have been tested yet.

'IsServerOS.vbs GjM  3/26/08
'Looks to see if ProductName reg key contains "Server"
'Returns 1 if yes and 2 if no

Option explicit
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002


dim strComputer, oReg, objargs
dim strValueName, strValue, strKeyPath

strComputer = "."

' Bind to WMI registry provider
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
 strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
strValueName = "ProductName"
' query the path and key.  results returned in strValue
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

'Check for "server" in returned value
If instr(strValue,"Server") > 0 then
	'wscript.echo "Running on a server: " & strValue
	wscript.quit 0
else
	'wscript.echo "Not running on a server: " & strValue
	wscript.quit 1
end if


We then made use of it by adding this to the logon script

:: Test to see if we are on a server and shouldn't run
cscript /nologo IsServerOS.vbs
if %errorlevel% EQU 0 (
   echo Running on a server; skipping logon script!
   Goto :EOF
)


Hope it's clear how this is working. Feel free to borrow

\\Greg