Jump to content

vbs / bat to check for reg key and perform if/else


Recommended Posts

I am currently trying to write a batch file to query the registry to determine if internet explorer 7 is installed and echo a message to the user appropriately.

The path in the registry is HKLM\Software\Microsoft\Internet Explorer\Version Vector

The key is: "IE"

IE 7 has a value of "7.0000" and 6 has a value of "6.0000". I am trying to develop a batch (wouldn't be opposed to VBS) to echo a msg to the user regarding what was found.

By simply using

@ECHO OFF

REG QUERY "HKLM\Software\Microsoft\Internet Explorer\Version Vector" /v IE

I can see what the value is but I would like to do something such as piping the key into find and evaluating it like so

@ECHO OFF

REG QUERY "HKLM\Software\Microsoft\Internet Explorer\Version Vector" /v IE |Find /I /C "7.0000"

IF (%ERRORLEVEL% NEQ 0 GOTO noie7) ELSE (goto foundie7)

:noie7

ECHO Internet Explorer 7 Not Found!

:foundie7

ECHO Internet Explorer 7 FOUND!

PAUSE

I have tried this exact batch only to get errors. It doesn't seem to want to recognize the ELSE part of the statement and just always says IE7 found regardless of what is actually present. I also looked at using vbs/wmi but I was unable to find the root\cimv2 or otherwise class for "add/remove" programs.

Help?

Link to comment
Share on other sites


I wouldn't exactly recommend using HKLM\Software\Microsoft\Internet Explorer\Version Vector as it doesn't even exist on my box (not on this one anyhow, haven't checked the others).

You could do something like this:

const HKLM = &H80000002
strComputer = "."
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Internet Explorer"
strValueName = "Version"
oWMI.GetStringValue HKLM,strKeyPath,strValueName,strValue
MsgBox "IE Version: " & strValue

if you prefer a jscript, or powershell version, or whatever else, just ask

Keep in mind IE8 is coming out soon too (beta 1 is already out), so testing for IE 7 only might be shortsighted.

I find .cmd/.bat works best for very simple things, but as soon as it's past basic command lines, a scripting language is far better (more versatile/powerful).

Link to comment
Share on other sites

We have line of business apps that do not have any support for IE 7 let alone IE 8 (or vista for that matter) anywhere in sight. The purpose of this is to make it easier for our helpers who are less technically inclined at an upcoming conference determine if IE7 is installed (which they need to remove).

Thanks for the reply and I will take a look at it.

Link to comment
Share on other sites

With a little change, your script will work:


@ECHO OFF
REG QUERY "HKLM\Software\Microsoft\Internet Explorer\Version Vector" /v IE |Find /I /C "7.0000">nul
IF %ERRORLEVEL% NEQ 0 (
ECHO Internet Explorer 7 Not Found!
) ELSE (
ECHO Internet Explorer 7 FOUND!
)
PAUSE

Link to comment
Share on other sites

Thanks guys! One final question, I've pieced the IE7 detection together with a service pack detection script I had written. I have the two working together however how would you make it so that checks if IE7 and SP3 are installed and THEN gives you a msg box telling you that you need to uninstall SP3/ Then uninstall IE7?

Here is the whole script I'm working with now:

const HKLM = &H80000002
strComputer = "."
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
Set objWMI2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMI2.ExecQuery("Select * from Win32_OperatingSystem")
strKeyPath = "Software\Microsoft\Internet Explorer"
strValueName = "Version"
objWMI.GetStringValue HKLM, strKeyPath,strValueName,strValue
If strValue >= "7" Then
MSGBOX "IE 7 Found! Please Go To Control Panel>Add/Remove Programs and Remove It."
ELSE
If strValue >= "6" Then
MSGBOX "IE 7 Not Found! You are OK!"
End If
End If
For each elem in colItems
If instr(elem.CSDVersion, "2") Then
msgbox "Found Service Pack2"
Else
If instr(elem.CSDVersion, "3") Then
msgbox "Found Service Pack3"
End If
End If
Next

Basically What this does now is says "Hey IE7 is installed, remove it" then it tells you which SP is present. Can it be coded to determine if there is a combination of both SP3 / IE7 and then give them a message box to the effect of "IE7 AND Service Pack 3 was found. You will need to remove Service Pack3 before removing IE7.

I assume its possible, I just seem to have lost my thinking cap today.

Edited by dfnkt
Link to comment
Share on other sites

You can't do If strValue >= "7" Then

strValue is a string, 7 should be an integer, but you wrote it as a string comparison... Doesn't work like that.

How about this?

const HKLM = &H80000002
strComputer = "."
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Internet Explorer"
strValueName = "Version"
objWMI.GetStringValue HKLM, strKeyPath,strValueName,strValue
intIEVers = CInt(Left(strValue, InStr(strValue, ".")-1))
if intIEVers > 6 then blnBadIEVers = true else blnBadIEVers = false
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For each elem in colItems
intSP = elem.ServicePackMajorVersion
Next
if intIEVers < 7 then
msgbox "Evrything OK!"
elseif intSP < 3 then
msgbox "IE " & CStr(intIEVers) & " Found! Please Go To Control Panel>Add/Remove Programs and Remove It."
else
msgbox "IE " & CStr(intIEVers) & " and SP" & CStr(intSP) & " Found! Please Go To Control Panel>Add/Remove Programs and Remove It."
end if

This checks for IE vers > 6 and blindly SP > 2 (regardless of if it's win2k, xp, or vista -- assuming all your boxes run XP here)

SP3 might have been installed before IE7 though, in which case, one shouldn't have to remove SP3.

Link to comment
Share on other sites

Is there not a way to make a vbscript look at multiple if's before having to "then"?

If SP3 and IE7 found then

msgbox "remove sp3, then remove ie7, reinstall sp3"

if sp2 and IE7 found then

msgbox "remove ie7 then install sp3"

if SP3 and IE6 found then

msgbox "no further action needed"

if sp2 and ie6 found then

msgbox "please install sp3"

^^ something like that?

Edited by dfnkt
Link to comment
Share on other sites

Is there not a way to make a vbscript look at multiple if's before having to "then"?

If SP3 and IE7 found then

msgbox "remove sp3, then remove ie7, reinstall sp3"

if sp2 and IE7 found then

msgbox "remove ie7 then install sp3"

if SP3 and IE6 found then

msgbox "no further action needed"

if sp2 and ie6 found then

msgbox "please install sp3"

^^ something like that?

Not what you asked, but this is how I would do that in batch:

...
REM routine to set variable found_something
REM If found SP2 SET /A found_something=20
REM If found SP3 SET /A found_something=30
REM If found IE6 SET /A found_something=%found_something%+6
REM If found IE7 SET /A found_something=%found_something%+7

GOTO :found%found_something%


:found37
ECHO remove sp3, then remove ie7, reinstall sp3
GOTO :EOF

:found27
ECHO remove ie7 then install sp3
GOTO :EOF

:found36
ECHO no further action needed
GOTO :EOF

:found26
ECHO please install sp3
GOTO :EOF

jaclaz

Link to comment
Share on other sites

So you want to force SP3 as well? As in must be BOTH IE < 7 AND SP3? Ok...

const HKLM = &H80000002
strComputer = "."
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strComputer & "rootdefault:StdRegProv")
strKeyPath = "SoftwareMicrosoftInternet Explorer"
strValueName = "Version"
objWMI.GetStringValue HKLM, strKeyPath,strValueName,strValue
intIEVers = CInt(Left(strValue, InStr(strValue, ".")-1))
if intIEVers > 6 then blnBadIEVers = true else blnBadIEVers = false
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For each elem in colItems
intSP = elem.ServicePackMajorVersion
Next
if intIEVers < 7 and intSP = 3 then
msgbox "no further action needed"
elseif intIEVers < 7 and intSP < 3 then
msgbox "please install sp3"
elseif intIEVers > 6 and intSP < 3 then
msgbox "remove ie7 then install sp3"
else
msgbox "remove sp3, then remove ie7, reinstall sp3"
end if

Anything else... just ask. (Normally I use the .wsf format, and I sign them, and add a few comments)

<Edit>

PowerShell version of the same, should anyone care:

$MachineName = "."
$SP = (Get-WmiObject -Class win32_OperatingSystem -namespace "rootCIMV2" -ComputerName $MachineName).ServicePackMajorVersion
$IE = (Get-ItemProperty 'HKLM:SoftwareMicrosoftInternet Explorer').Version.Split(".")[0]
if ($IE -lt 7 -and $SP -ge 3) { write-host "Everything OK!" }
elseif ($IE -lt 7 -and $SP -lt 3) { write-host "Install SP3" }
elseif ($IE -ge 7 -and $SP -lt 3) { write-host "Uninstall IE7" }
elseif ($IE -ge 7 -and $SP -ge 3) { write-host "Uninstall SP3 then uninstall IE7" }
else { write-host "This case shouldn't ever happen" }

It's a lot nicer and more concise, but you do need PowerShell installed (if you want MessageBoxes instead of text, then use the .popup method of the script.shell object)

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

Is there any real need for the if this do this and this type messages?

If you want to make things as simple as possible for your helpers, just give them a single task not a possible list of them

You've only got to check for two conditions:

If Internet Explorer is version 7 then uninstall it!

If Service Pack is not 3 then install SP3

Using the most common language in the thread thus far, I'd suggest an idea similar to this:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
If InStr(objItem.Caption, "Windows XP") Then
strSP = objItem.ServicePackMajorVersion
Else
Wscript.Echo "This computer is not running Windows XP"
Wscript.Quit
End If
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & _
"\root\cimv2\Applications\MicrosoftIE")
Set colIESettings = objWMIService.ExecQuery("Select * from MicrosoftIE_Summary")
For Each strIESetting in colIESettings
IEVer = split(strIESetting.Version, ".")
Next
If IEVer(0) = "7" Then
WScript.Echo "Remove IE7"
Wscript.Quit
ElseIf strSP = "3" Then
Wscript.Echo "No action needed"
Else
WScript.Echo "Install SP3"
End If

Notice the check for XP first!

Link to comment
Share on other sites

Why not use AutoIt for this, the bonus apart from easier conditional programming is that you get a gui, so you dont have to suffer console screens......

AutoIt isn't exactly the best/most universal/most well known scripting kit. It's meant mostly to automate button clicks and such, which a LOT of ppl see as a last resort. And it needs the program installed to run scripts, whereas WSH is already installed on most PCs. With most network admin types (windows world, not linux), vbscript is pretty much the de facto standard, slowly being replaced by PowerShell. There are no console messages with vbscript (using wscript -- as long as you didn't force it to use cscript).

Yzöwl: Likely, he wants a messagebox popup for the end-users, and only one of them, hence all the nested if's. But yeah, my script blindly checked for SP and not OS like I said before (assuming he uses only XP)

Also, opening \root\cimv2\Applications\MicrosoftIE throws an exception here, hence why I went with the other method (reading from registry directly). Screenshot of debug session in VS:

iescripterrordq9.th.png

Link to comment
Share on other sites

Perhaps you need to visit autoItscript.com and you'll see its not as you describe "mostly to automate button clicks" and also not "a LOT of ppl see as a last resort". Ive written entire applications using it, including a Ghost clone thats indistinguishable form the real thing in every detail :)

Its as powerful, if not more, no actually more, than anything you can come up with in a console, its free and is growing all the time due to one of the most active forums on the net.

And it DOES NOT need any program installed to run it, you complie your scripts to an .exe file.

Link to comment
Share on other sites

Okay boys, playtime's over, lets stick to programming/scripting.

All other stuff removed!

My post wasn't intended to correct anything with your scripting method crahak, it was more to example my statement of simplifying the conditional arguments. Ther helper runs the script it tells them what to do next. They run the script after doing that it'll let them know their next move. I think from my experience of helpers that if you just give on task at a time it saves your hair. All they need to learn to do is run script, follow advice, run script, follow advice

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...