08 May 2012

VBScript Subroutine to Determine the OS and Architecture

Posted By: Mick Pletcher - 10:48 AM

Share

& Comment

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  

About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered

0 comments:

Post a Comment

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.