08 May 2012

VBScript Subroutine to Determine the OS and Architecture

This is a simplified procedure I wrote to determine both the OS and the architecture and return a single value showing both (i.e. Windows7x64, XPx86).

NOTE: The variable OS is a global variable and is not defined within this subroutine.


 Sub DetermineOS()  

      REM Define Local Constant  
      CONST strComputer = "."  

      REM Define Local Objects  
      DIM objWMIService : Set objWMIService = GetObject("winmgmts:" _  
           & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")  
      DIM colOperatingSystems: Set colOperatingSystems = objWMIService.ExecQuery _  
           ("Select * from Win32_OperatingSystem")  
      DIM objOperatingSystem : Set objOperatingSystem = Nothing  
      DIM WshShell           : Set WshShell = CreateObject("WScript.Shell")  

      REM Define Local Variables  
      DIM OSType : OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")  

      If OSType = "x86" then  
           OSType = "x86"  
      elseif OSType = "AMD64" then  
           OSType = "x64"  
      end if  
      For Each objOperatingSystem in colOperatingSystems  
           OS = objOperatingSystem.Version  
      Next  
      OS = Trim(OS)  
      OS = Left(OS,3)  
      Select Case OS  
      Case 5.0  
           OS = "XP" & OSType  
      Case 6.0  
           OS = "Vista" & OSType  
      Case 6.1  
           OS = "Windows7" & OSType  
      End Select  

      REM Cleanup Local Variables  
      Set objWMIService       = Nothing  
      Set colOperatingSystems = Nothing  
      Set objOperatingSystem  = Nothing  
      Set OSType              = Nothing  
      Set WshShell            = Nothing  

 End Sub  

0 comments:

Post a Comment