Detecting Windows System Type in VBS Scripts

Sometimes you need to know what type of system your script is running on to ensure things run smoothly, here are a few tricks I found while looking for a few ways to accomplish this.There may be a better way, but this might be just enough to do the trick for you.
The three empty functions can be called and used within your scripts:
OsArchtecture() – x86 or AMD64 (basically 32bit or 64bit)
OsProductType() – Server or Workstation
OsVersion() – 2000, XP, 7, 2008, 2008 R2, etc…

 
' Detect System Processor Achitecture
' Vaild Values for OsType, x86 or AMD64
Function OsAchitecture ()
	On Error Resume Next
	Dim WshShell
	Set WshShell = CreateObject("WScript.Shell")
	OsAchitecture = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
	On Error Goto 0
End Function
 
' Detect if the system the script is running on is a Server or a workstation
Function OsProductType ()
	On Error Resume Next
	Dim objWMI, objItem, colItems
    	Dim OSVersion, OSName, ProductType
	Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
	Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
	For Each objItem in colItems
		ProductType = objItem.ProductType
	Next
	If ProductType = 1 Then
		ProductType = "workstation"
	Else
		ProductType = "server"
	End If
	OsProductType = ProductType
	On Error Goto 0
End Function
Function OsVersion ()
	On Error Resume Next
    	Dim objWMI, objItem, colItems
    	Dim ProductType
	Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
	Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
	For Each objItem in colItems
		OSVersion = Left(objItem.Version,3)
		ProductType = objItem.ProductType
	Next
	Select Case OSVersion
		Case 5.0
			If ProductType = 1 Then
				OsVersion = "2000"
			Else
				OsVersion = "Server 2000"
			End If
		Case 5.1
			OsVersion = "XP"
		Case 5.2
			If ProductType = 1 Then
				OsVersion = "XP"
			Else
				OsVersion = "Server 2003"
			End If
		Case 6.0
			If ProductType = 1 Then
				OsVersion = "Vista"
			Else
				OsVersion = "Server 2008"
			End If
		Case 6.1
			If ProductType = 1 Then
				OsVersion = "7"
			Else
				OsVersion = "Server 2008 R2"
			End If
		Case Else
			OsVersion = "Unknown"
	End Select
	On Error Goto 0
End Function
Advertisements

About Paul Cardelli, CISSP
Cyber Security Analyst, and computer guru

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: