OS architecture detection script - vbscript

I have just downloaded the two .msi installers for 7zip (x86 and x64). Does anyone have a simple script I can use to detect the OS architecture and launch the appropriate .msi file?

You said simple, but didn't specify a language ...
Batch (.cmd):
IF /I %PROCESSOR_ARCHITECTURE% EQU x86 (
msiexec /qn /i 7zip_x86.msi
) ELSE (
msiexec /qn /i 7zip_x64.msi
)
VBScript (.vbs):
Set oShell = WScript.CreateObject("WScript.Shell")
proc_arch = oShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
If proc_arch = 'x86' Then
oShell.Run("msiexec /qn /i 7zip_x86.msi"), 0, true
Else
oShell.Run("msiexec /qn /i 7zip_x64.msi"), 0, true
End If
PowerShell (.ps1):
if ($env:PROCESSOR_ARCHITECTURE -eq "x86") {
& msiexec /qn /i 7zip_x86.msi
} else {
& msiexec /qn /i 7zip_x64.msi
}
# or ...
if ([Environment]::Is64BitOperatingSystem) {
& msiexec /qn /i 7zip_x64.msi
} else {
& msiexec /qn /i 7zip_x86.msi
}

this might help
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessors = objWMIService.ExecQuery _
("Select * From Win32_Processor")
For Each objProcessor in colProcessors
If objProcessor.Architecture = 0 Then
Wscript.Echo "This is an x86 computer."
ElseIf objProcessor.Architecture = 1 Then
Wscript.Echo "This is a MIPS computer."
ElseIf objProcessor.Architecture = 2 Then
Wscript.Echo "This is an Alpha computer."
ElseIf objProcessor.Architecture = 3 Then
Wscript.Echo "This is a PowerPC computer."
ElseIf objProcessor.Architecture = 5 Then
Wscript.Echo "This is a ARM computer."
ElseIf objProcessor.Architecture = 6 Then
Wscript.Echo "This is an ia64 computer."
ElseIf objProcessor.Architecture = 9 Then
Wscript.Echo "This is an x64 computer."
Else
Wscript.Echo "The computer type could not be determined."
End If
Next

I found a great vbscript, written by Monimoy Sanyal, posted on Microsoft Technet.
It also gives information about system architecture, if 32-bit system run on the 64-bit hardware.
https://gallery.technet.microsoft.com/scriptcenter/Determine-If-Your-System-969670e3
Option Explicit
Dim ObjWMI, ColSettings, ObjProcessor
Dim StrComputer, ObjNetwork
Set ObjNetwork = WScript.CreateObject("WScript.Network")
StrComputer = Trim(ObjNetwork.ComputerName)
Set ObjNetwork = Nothing
WScript.Echo VbCrLf & "Computer Name: " & StrComputer
WScript.Echo vbNullString
Set ObjWMI = GetObject("WINMGMTS:" & "{ImpersonationLevel=Impersonate,AuthenticationLevel=Pkt}!\\" & StrComputer & "\Root\CIMV2")
Set ColSettings = ObjWMI.ExecQuery ("SELECT * FROM Win32_Processor")
For Each ObjProcessor In ColSettings
Select Case ObjProcessor.Architecture
Case 0
WScript.Echo "Processor Architecture Used by the Platform: x86"
Case 6
WScript.Echo "Processor Architecture Used by the Platform: Itanium-Based System"
Case 9
WScript.Echo "Processor Architecture Used by the Platform: x64"
End Select
Select Case ObjProcessor.ProcessorType
Case 1
WScript.Echo "Processor Type: Other. Not in the Known List"
Case 2
WScript.Echo "Processor Type: Unknown Type"
Case 3
WScript.Echo "Processor Type: Central Processor (CPU)"
Case 4
WScript.Echo "Processor Type: Math Processor"
Case 5
WScript.Echo "Processor Type: DSP Processor"
Case 6
WScript.Echo "Processor Type: Video Processor"
End Select
WScript.Echo "Processor: " & ObjProcessor.DataWidth & "-Bit"
WScript.Echo "Operating System: " & ObjProcessor.AddressWidth & "-Bit"
WScript.Echo vbNullString
If ObjProcessor.Architecture = 0 AND ObjProcessor.AddressWidth = 32 Then
WScript.Echo "This Machine has 32 Bit Processor and Running 32 Bit OS"
End If
If (ObjProcessor.Architecture = 6 OR ObjProcessor.Architecture = 9) AND ObjProcessor.DataWidth = 64 AND ObjProcessor.AddressWidth = 32 Then
WScript.Echo "This Machine has 64-Bit Processor and Running 32-Bit OS"
End If
If (ObjProcessor.Architecture = 6 OR ObjProcessor.Architecture = 9) AND ObjProcessor.DataWidth = 64 AND ObjProcessor.AddressWidth = 64 Then
WScript.Echo "This Machine has 64-Bit Processor and Running 64-Bit OS"
End If
Next
Set ObjProcessor = Nothing: Set ColSettings = Nothing: Set ObjWMI = Nothing: StrComputer = vbNullstring

Probably crude and not very ... "safe" ...
Just check for the existence of C:\Program Files (x86) which is only present on a 64-bit system.

Related

Batch file auto copy files from USB/DVD to HDD - wait for USB/DVD to be inserted (detect it)

This is my first .bat file. I want to make a script to copy all my files from USB or DVD/CD when volume is detected to hardisk.
I did not find something good. I think I need to implement something like this:
while(!volume:e /*if is DVD*/ || !volume:g /*if is USB*/)
keep seeking
if(volume:e is detected)
xcopy e:\* d:\Backup\ /s /q
if(volume:g is detected)
xcopy g:\* d:\Backup /s /q
My final result:
:while
if ("wmic logicaldisk where drivetype= '2' get volumename") NEQ "No Instance(s) Available."::HERE IS THE PROBLEM
(GOTO :Syntax) else (GOTO :while)
:Syntax
xcopy e:\* d:\Backup\ /s /q
//Use DriveType=5 for DVDs
I don't know how to write after NEQ, because if no USB is connected cmd print No Instance(s) Available., but don't work correct for what I want.
Please help me... what do I miss?
This waits for a VolumeChangeEvent then copies c:\test onto the new volume.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set evtDevice = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_VolumeChangeEvent")
Wscript.Echo "Waiting for events ..."
Do
Set objReceivedEvent = evtDevice.NextEvent
'report an event
Wscript.Echo " Win32_Device Changed event occurred" & VBNewLine
If objReceivedEvent.EventType = 1 Then
Wscript.Echo "Type = Config Changed"
ElseIf objReceivedEvent.EventType = 2 Then
Wscript.Echo "Type = Device Arrived"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Volume")
For Each objItem in colItems
If objitem.DriveType = 2 then
Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(objitem.driveletter)
Set DestFldr=objShell.NameSpace("c:\test\")
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Wscript.Echo "Finished Copying"
End If
Next
ElseIf objReceivedEvent.EventType = 3 Then
Wscript.Echo "Type = Device Left"
ElseIf objReceivedEvent.EventType = 4 Then
Wscript.Echo "Type = Computer Docked"
End If
Loop

"Less than" statements not evaluating

Running into this problem. Trying to uninstall all previous version of a program using the less than statement and installing new version. It doesn't recognize the less than and will keep uninstalling and reinstalling the newest version everytime.
Option Explicit
Const HKEY_LOCAL_MACHINE = &H80000002
Dim Msg, MsgBoxStyle, RegKey, NAMProductKey, NAMProductName, NAMVersion
'=== START Check for Cisco AnyConnect Network Access Manager < 3.1.05170
Sub GetNAMKey()
Dim oReg, sPath, aKeys, sName, sKey, sVersion
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
sPath = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, aKeys
For Each sKey in aKeys
oReg.GetStringValue HKEY_LOCAL_MACHINE, sPath & "\" & sKey, "DisplayName", sName, "DisplayVersion", sVersion
If Not IsNull(sName) Then
If (sName = "Cisco AnyConnect Network Access Manager") Then
NAMProductKey = sKey
NAMProductName = sName
NAMVersion = sVersion
End If
End If
Next
End Sub
'=Start Uninstall Reference==
Sub UninstallUNI(key, name)
Dim cmd, objShell, iReturn, oshell
cmd = "%SystemRoot%\System32\msiexec.exe /q/x " & key
Set objShell = wscript.createObject("wscript.shell")
objShell.LogEvent 0, "Removing the program [" & name & "] under Product Key [" & key & "]" & vbCrLf & "Executing command: " & vbCrLf & cmd
iReturn=objShell.Run(cmd,1,TRUE)
If (iReturn = 0) Then
objShell.LogEvent 0, "Program [" & name & "] was successfully removed"
Else
objShell.LogEvent 0, "Failed to remove the program [" & name & "]."
End If
Set objShell = Nothing
End Sub
'=== START CALLs (This is the script's logic.)
Dim objWSH
Set objWSH = CreateObject("WScript.Shell")
NAMProductKey = ""
NAMProductName = ""
NAMVersion = ""
Call GetNAMKey()
If Not (NAMProductKey = "") Then
If (NAMVersion < "3.1.05170") Then
Call UninstallUNI
NAMProductKey = ""
NAMProductName = ""
NAMVersion = ""
Call GetNAMKey()
If (NAMProductKey = "") Then
'Now we need to produce "msiexec.exe /a "Msi file.msi" /quiet /norestart" for a silent MSI install
objWSH.Run "msiexec.exe /i " + Chr(34) + "C:\Users\sek\Music\Cisco ISE\AnyConnect Network Access Manager\anyconnect-nam-win-3.1.05170-k9.msi" + Chr(34) + " /quiet /norestart"
End If
End If
Else
'Now we need to produce "msiexec.exe /a "Msi file.msi" /quiet /norestart" for a silent MSI install
objWSH.Run "msiexec.exe /i " + Chr(34) + "C:\Users\sek\Music\Cisco ISE\AnyConnect Network Access Manager\anyconnect-nam-win-3.1.05170-k9.msi" + Chr(34) + " /quiet /norestart"
End If
Unless you have very specific knowledge about how Cisco names it's versions you can not compare them like this.
The method you are using is a string compare which follows some lexicographic standard rules.
Because of that a version umber like 3.2 will be considered greater than 3.10.
In order to fix this you will have to split the string with '.' as delimiter and compare the subversion numbers independently.
However this is more a general observation and not the direct reason for the wrong evaluation.
I think the reason for that lies within your GetStringValue call.
According to the API this method can not return 2 values at once, so I am a bit puzzled how this is even executed without error. It explains however why the version number is not returned correctly. You would need a second GetStringValue call for that.

How to find operating system name without using WMI in VBScript?

I need a way to find the operating system version without using winmgmts. I need a platform independent way of find the name of the OS, vista, win7, etc.
We have to parse paths in user accounts that are different on 2K/XP then they are on Vista / Win7. The comspec return looks like this: Microsoft Windows [Version 6.1.7600]. 2k/XP is Version 5.x and Vista/Win7 is Version 6.x.
Set shell = CreateObject("WScript.Shell")
Set getOSVersion = shell.exec("%comspec% /c ver")
version = getOSVersion.stdout.readall
wscript.echo version
Select Case True
Case InStr(version, "n 5.") > 1 : GetOS = "XP"
Case InStr(version, "n 6.") > 1 : GetOS = "Vista"
Case Else : GetOS = "Unknown"
End Select
wscript.echo GetOS`
VBscript:
Set oShell = CreateObject( "WScript.Shell" )
os_name=oShell.ExpandEnvironmentStrings("%OS%")
WScript.Echo os_name
This page provides several wrapper routines for obtaining general Windows operation system information, all using a single call to the GetVersionEx API.
From VisualBasicScript.com:
Option Explicit
Dim oShell
Dim oShellExec, oStdOutputText, sText, iElement, aOS, sOS
Set oShell = CreateObject("Wscript.Shell")
Set oShellExec = oShell.Exec("%comspec% /c ver")
Set oStdOutputText = oShellExec.StdOut
Do While Not oStdOutputText.AtEndOfStream
sText = oStdOutputText.ReadLine
aOS = Array("Windows 95", "Windows 98", "Windows NT", "Windows 2000", "Windows XP", "Microsoft Windows [Version")
For iElement = LBound(aOS) To UBound(aOS)
If InStr(sText, aOS(iElement)) <> 0 Then
If aOS(iElement) = "Microsoft Windows [Version" Then
If InStr(sText, "Version6.0") <> 0 Then
sOS = "Windows Vista"
ElseIf InStr(sText, "Version 6.1")<>0 Then
sOS = "Windows 7"
Else
sOS = "Windows 2003"
End If
Else
sOS = aOS(iElement)
End If
End If
Next
Loop
WScript.Echo sOS
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each os in oss
Wscript.Echo "Caption: " & os.Caption
Wscript.Echo "Code Set: " & os.CodeSet
Wscript.Echo "Country Code: " & os.CountryCode
Wscript.Echo "Debug: " & os.Debug
Wscript.Echo "Encryption Level: " & os.EncryptionLevel
dtmConvertedDate.Value = os.InstallDate
dtmInstallDate = dtmConvertedDate.GetVarDate
Wscript.Echo "Install Date: " & dtmInstallDate
Wscript.Echo "Licensed Users: " & os.NumberOfLicensedUsers
Wscript.Echo "Organization: " & os.Organization
Wscript.Echo "OS Language: " & os.OSLanguage
Wscript.Echo "OS Product Suite: " & os.OSProductSuite
Wscript.Echo "OS Type: " & os.OSType
Wscript.Echo "Primary: " & os.Primary
Wscript.Echo "Serial Number: " & os.SerialNumber
Wscript.Echo "Version: " & os.Version
Next

Pulling a remote windows server architecture in VBScript

I have the following code. I am trying to get the information on whether or not the remote computer I am connecting to is 32-bit or 64 bit. I tried doing it at the last snippet of this code but it didn't work. Here is the error (I changed the remote system name):
WshShell.RegRead: Invalid root in registry key
"\*remotesystem*\HKEY_LOCAL_MACHINE\SYSTEM\CurrentCon
trolSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE".
Option Explicit
Dim objWMISrvc,strRemoteComputer,colOSItems,objItem,args,OsType
'String variables
Dim strName,strCaption,strVersion,strCSDVer,strSerial,WshShell
'Adding this in to transfer FQDN variable to this script from ASP.net
Set args = WScript.Arguments
strRemoteComputer = args.Item(0)
Set objWMISrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strRemoteComputer & "\root\cimv2")
CheckOSType objWMISrvc
Sub CheckOSType( objWMISrvc )
Set colOSItems = objWMISrvc.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colOSItems
strName = objItem.CSName
'strDesc = objItem.Description
'strManufac = objItem.Manufacturer
strCaption = objItem.Caption
strVersion = objItem.Version
strCSDVer = objItem.CSDVersion
strSerial = objItem.SerialNumber
Next
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("\\" & strRemoteComputer & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
If OsType = "x86" then
WScript.Echo "Windows 32bit system detected"
elseif OsType = "AMD64" then
WScript.Echo "Windows 64bit system detected"
end if
End Sub
You can do this directly from WMI without reading the registry. Just loop through your computer names with this script. It returns either "32" or "64".
strComputer = "."
Set objWMIService = GetObject("winmgmt:\\" & strComputer & "\root\cimv2")
Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objProcessor in colProcessors
WScript.Echo objProcessor.AddressWidth 'or objProcessor.DataWidth
Exit For
Next
To read the registry of as remote machine you must use the the StdRegProv WMI class
Check this sample
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strValueName = "PROCESSOR_ARCHITECTURE"
oReg.GetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,OsType
WScript.Echo OsType

How can I test whether or not "Microsoft Windows 7 Home Premium" is the operating system using VBScript?

My first attempt is to query Win32_OperatingSystem for the caption, and test whether the caption "equals" the operating system I am testing for:
Dim objWMIService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
msgbox getOperatingSystemCaption()
msgbox meetsOperatingSystemRequirement()
Function getOperatingSystemCaption()
Dim strCaption, colOperatingSystems, objOperatingSystem
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
strCaption = objOperatingSystem.Caption
Exit For
Next
getOperatingSystemCaption = strCaption
End Function
Function meetsOperatingSystemRequirement()
meetsOperatingSystemRequirement = False
If getOperatingSystemCaption() = "Microsoft Windows 7 Home Premium" Then
meetsOperatingSystemRequirement = True
End If
End Function
I suppose I can use InStr, however I still do not understand why the "Caption" and my string are not equal.
Are you sure you have "Microsoft Windows XP" and not "Microsoft Windows XP Professional" ?. If you use "=" sign, then you will not catch it because it expects to match exact string. Use instr() would be better if you want partial match. Otherwise, add in "Professional"
You can put in some debugging after caption is found
....
msgbox strCaption & " " & len(strCaption)
getOperatingSystemCaption = strCaption
....
and try a different way
.....
myCaption = getOperatingSystemCaption()
msgbox myCaption & " " & len(myCaption)
If myCaption = "Microsoft Windows XP Premium Home" Then
......
check the length as well...

Resources