How to find operating system name without using WMI in VBScript? - 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

Related

Out of memory error in vbs scripts

I've been facing this dialogue box just after I start my script
Script: C:\konica.vbs
Line: 14
Char: 1
Error: Out of Memory: 'GetObject'
Code: 800A0007
Source: Microsoft VBScript runtime error
This is my script:
Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
'### Konica ###
strIPAddress = "192.168.110.168"
strComputer = "."
strPrinterName = "Konica"
strDriverName = "KONICA MINOLTA C353 Series PS"
strLocation = "Konica"
strInfFile = "\\stp\HHard\Printers\KONICA MINOLTA\XP(x86)\BHC353PSWinx86_6500RU\BHC353PSWinx86_6500RU\KOAZXA__.INF"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found: " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
WSCript.Quit 114001
Next
'MsgBox(strIPAddress)
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_
wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
objPrinter.Location = strLocation
objPrinter.Put_
Next
You can see, that it script for installing in my system printer 'Konica'. In other system, it script works fine. Where mistake? Please help ?
I'm probably on a wrong tangent here but try giving the MsgBox a variable and then using code that doesn't do anything.
For example:
MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found: " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
becomes
x = MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found: " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
Then you could write
If x Then
Else
End If
Which would run and do nothing because x is always x but there's nothing to run inside the If statement.
That error means that your system is low on memory. The line numbering is questionable in your post, but I would venture a guess that the size of the printer driver is pretty large.

"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.

Can't get WMI namespace RSOP to work

I need to create a subroutine to check for certain policies on a system. I'm currently just trying to do that.
strComputerFQDN is defined at the beginning of the program and that is working fine.
Do you see anything wrong?
Here is my code:
'*************************************************************************
' This Subroutine checks Local Policies
'*************************************************************************
Sub CheckPolicies()
Dim objGPOSrvc,colItems,objItem
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP")
Set colItems = objGPOSrvc.ExecQuery("SELECT * FROM RSOP_GPO")
WScript.Echo("Check Policies")
WScript.Echo("------------------------------------")
For Each objItem in colItems
If InStr(UCase(objItem.Name),"WSUS") Then
If InStr(UCase(objItem.Name),"SERVER") Then
WScript.Echo("Policy applied: " & objItem.Name)
Else
WScript.Echo("Wrong WSUS Policy Applied - Check Computer object location")
End If
End If
Next
If strWSUSApplied = "FALSE" Then
WScript.Echo("No WSUS Policy Applied!")
End If
WScript.Echo vbCrLf
End Sub
The namespace should be root\RSOP\Computer
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP\Computer")
or root\RSOP\User
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP\User")
Most typically you would do something like this:
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\RSOP\Computer")
Set colItems = objWMIService.ExecQuery("Select * from RSOP_GPO")
For Each objItem in colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "GUID Name: " & objItem.GUIDName
WScript.Echo "ID: " & objItem.ID
WScript.Echo "Access Denied: " & objItem.AccessDenied
WScript.Echo "Enabled: " & objItem.Enabled
WScript.Echo "File System path: " & objItem.FileSystemPath
WScript.Echo "Filter Allowed: " & objItem.FilterAllowed
WScript.Echo "Filter ID: " & objItem.FilterId
WScript.Echo "Version: " & objItem.Version
WScript.Echo
Next
If you receive Error 0x80041003, you will need to run this script with administrator credentials. For Vista and later, open your start menu and type cmd. When "Command Prompt" appears, right-click and choose Run As Administrator. You can now launch your script from the elevated command prompt without error.

OS architecture detection script

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.

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