Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if OS is x86 or x64 then Windows 7 or XP and run msi

Status
Not open for further replies.

GrimR

IS-IT--Management
Jun 17, 2007
1,149
ZA
Hi

anyone know how to find if OS is x86 or x64 then Windows 7 or XP and run msi
each piece of code does one part how do I combine the two

Code:
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\default:StdRegProv") 
   strKeyPath = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
   strValueName = "Identifier"

oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
'===============================================================
'Checking Condition whether the build is 64bit or 32 bit
	If (instr(strValue,"64")) then
		WshShell.Run "Windows 7 64-bit.MSI" /qn
	End If

	elseif (instr(strValue,"x86")) then
		WshShell.Run "Windows 7 32-bit.MSI" /qn
	End If


Code:
Function FindOSType(strComputer)
    'Defining Variables
    Dim objWMI, objItem, colItems
    Dim OSVersion, OSName, ProductType

    'Get the WMI object and query results
    Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)

    'Get the OS version number (first two) and OS product type (server or desktop) 
    For Each objItem in colItems
        OSVersion = Left(objItem.Version,3)
        ProductType = objItem.ProductType
    Next

    'Time to convert numbers into names
    Select Case OSVersion
		Case "6.1"
			OSName = "Windows 7"
        Case "6.0" 
            OSName = "Windows Vista"
        Case "5.2" 
            OSName = "Windows 2003"
        Case "5.1" 
            OSName = "Windows XP"
        Case "5.0" 
            OSName = "Windows 2000"
        Case "4.0" 
            OSName = "Windows NT 4.0"
        Case Else
            OSName = "Windows 9x"
    End Select

    'Return the OS name
    FindOSType = OSName
End Function

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
Turn the first block of code into a function. Copy the two functions into the same file. Modify the second block of code to return only the version. Move the [tt]select case[/tt] outside the function. Call each function once and use the select to determine the next step.

Code:
'************************************************************************************************************************************************
' VARIABLE DEFINITION
'************************************************************************************************************************************************

CONST HKEY_LOCAL_MACHINE  = &H80000002

'************************************************************************************************************************************************
' FUNCTIONS
'************************************************************************************************************************************************

function getBitWidth(strComputerName)
	set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\default:StdRegProv") 
	strKeyPath = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
	strValueName = "Identifier"
	
	objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue

	if (instr(strValue,"64")) then
		getBitWidth = 64
	else 
		getBitWidth = 32
	end if
end function

function getOSVersion(strComputerName)
	set objWMI = GetObject("winmgmts://" & strComputerName & "/root/cimv2")
	set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)

	for each objItem in colItems
		strOSVersion = left(objItem.Version, 3)
	next

	getOSVersion = strOSVersion
end function

'************************************************************************************************************************************************
' BEGIN
'************************************************************************************************************************************************

strComputerName = "." 'local
intBitWidth = getBitWidth(strComputerName)
strOSVersion = getOSVersion(strComputerName)

select case strOSVersion
	case "6.1" : strOSName = "Windows 7"
	case "6.0" : strOSName = "Windows Vista"
	case "5.2" : strOSName = "Windows 2003"
	case "5.1" : strOSName = "Windows XP"
	case "5.0" : strOSName = "Windows 2000"
	case "4.0" : strOSName = "Windows NT 4.0"
	case else : strOSName = "Windows 9x"
end select

msgbox "I am a " & intBitWidth & "-bit " & strOSName & " machine"

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
Surely that can be simplified (and checking whether the CPU is 32- or 64-bit does not necessarily tell us much about the OS, since we can happily have a 32-bit OS on a 64-bit CPU):

Code:
[blue]Function GetOS(strComputerName)
    Set objWMI = GetObject("winmgmts://" & strComputerName & "/root/cimv2")
    
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        GetOS = objItem.Caption & objItem.OSArchitecture
    Next
End Function[/blue]
 
strongm I do prefer the method of finding OS type x64 or x86 as to whether the processor is x64 capable as we do exactly that install 32bit on x64 machines.

when I do the following below [Part1] I get
"I am a Microsoft Windows Professional 32-bit and Windows 9x machine"
so the first part works 100% now how do I pass it into the select statement?
If the OSversion is correct as in the wscrit.echo OSversion. If I change the Select Case OSversion surely that should now hold the value "Microsoft Windows Professional 32-bit" the question is then how would the select statement know that.

So in the second lot of code [Part2] I changed the Case to reflect the caption, is this now correct or is it still wrong?

Part1
Code:
strComputer = "."
Dim objWMI:Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
    
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        OSVersion = objItem.Caption & objItem.OSArchitecture
		'wscript.echo OSVersion
		'wscript.echo objItem.OSArchitecture
    Next
	
	strOSVersion = OSVersion
	Select Case strOSVersion
	Case "6.1"
	    OSName = "Windows 7"
        Case "6.0" 
            OSName = "Windows Vista"
        Case "5.2" 
            OSName = "Windows 2003"
        Case "5.1" 
            OSName = "Windows XP"
        Case "5.0" 
            OSName = "Windows 2000"
        Case "4.0" 
            OSName = "Windows NT 4.0"
        Case Else
            OSName = "Windows 9x"
    End Select

msgbox "I am a " & strOSVersion & " and " & OSName & " machine"


Part2
Code:
strComputer = "."
Dim objWMI:Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
    
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        OSVersion = objItem.Caption & objItem.OSArchitecture
    Next
	
	
	Select Case OSVersion
	Case "Microsoft Windows 7 Professional 32-bit"
	    OSName = "Microsoft Windows 7 Professional 32-bit"
        Case "Microsoft Windows 7 Professional 64-bit" 
            OSName = "Microsoft Windows 7 Professional 64-bit"
        Case "Microsoft Windows XP Professional 32-bit" 
            OSName = "Microsoft Windows XP Professional 32-bit"
        Case "Microsoft Windows XP Professional 64-bit" 
            OSName = "Microsoft Windows XP Professional 64-bit"
    End Select
	
	

msgbox "I am a " & OSVersion & " and " & OSName & " machine"


MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
I was suggesting that you replace all your code with my somewhat shorter version - you don't need the Select Case at all.

Code:
[blue]
Function GetOS(strComputerName)
    Set objWMI = GetObject("winmgmts://" & strComputerName & "/root/cimv2")
    
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        GetOS = objItem.Caption & objItem.OSArchitecture
    Next
End Function 

Msgbox "I am a " & GetOS(".") & " machine"[/blue]
 
Ok but I have programs that need to run based on OS XP or Win7 and x64 or x86

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
objItem.Caption tells you the full OS name
objItem.OSArchitecture tells you the architecture

What more do you want?
 
I get that but it's useless information as it stands. So I pass it to the select statement, suppose I could use if but I think select looks neater, and install the program.
Hope this makes it clearer. Thanks
Code:
strComputer = "."
Dim objWMI:Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
    
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        OSVersion = objItem.Caption & objItem.OSArchitecture
    Next
	
	
	Select Case OSVersion
	Case "Microsoft Windows 7 Professional 32-bit"
	    WshShell.Run "\\server\share\Windows XP 32-bit.MSI /qn", True
        Case "Microsoft Windows 7 Professional 64-bit" 
            WshShell.Run "\\server\share\Windows XP 62-bit.MSI /qn", True
        Case "Microsoft Windows XP Professional 32-bit" 
            WshShell.Run "\\server\share\Windows XP 32-bit.MSI /qn", True
        'Case "Microsoft Windows XP Professional 64-bit" 
         '   OSName = "Windows XP Professional 64-bit"
    End Select

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
>it's useless information as it stands

No it isn't. It answered your "how to find if OS is x86 or x64 then Windows 7 or XP"

Once that is achieved then running the correct MSI can be done in about a hundred different ways. For example, if the MSIs are named to match the OS and architecture, then it could be as simple as:

Code:
[blue]WshShell.Run "\\server\share\" & GetOS(".") & ".MSI /qn", True[/blue]


 
Thanks, understood, hopefully this is it now. I test on my machine and worked.

Code:
strComputer = "."

strProgPath = "\\server\share\"
Dim objWMI:Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Dim WshShell:Set WshShell = CreateObject("WScript.Shell")

    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)
    
    For Each objItem In colItems
        GetOS = objItem.Caption & objItem.OSArchitecture
		wscript.echo GetOS
    Next

Return = WshShell.Run ("msiexec /qn /i " & chr(34) & strProgPath & GetOS & ".msi" & chr(34),1,True)

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
I am a novice with vbscripting but have a desperate need for a script to do the following (EXTREMELY SIMILAR to the above request)
<which was Find if OS is x86 or x64 then Windows 7 or XP and run msi>

HOWEVER, all I need to do is test if the operating system is XP or NOT...and run a script

if XP I need to run
netswitchXP.vbs
ELSE
run netswitch.vbs

that is it.

I tried to modify the following (Ie...what am I doing wrong with this?)

strComputer = "."
Dim objWMI:Set objWMI = GetObject("winmgmts://" & strComputer & "\root\cimv2")

Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem", , 48)

For Each objItem in colItems
OSVersion = objItem.Caption
Next

Select Case OSVersion
Case "Microsoft Windows XP Professional"
cscript.exe netswitchXP.vbs, True
Case Else
cscript.exe netswitch.vbs
End Select


HELP please ~ and thank you so much...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top