Jump to content

WMI Application Installation Check Failing if Not Existing


Recommended Posts

Greetings,

I am using the below WMI code in AutoIt to check if the MS SQL Server 2008 R2 client is installed.  If it is, I can get information such as the actual name or version to confirm it it exists, however, if it doesn't exist, it doesn't do anything and just exists.

My goal is if it exists to bring up a message box saying it does, but if it doesn't, so then bring up a message box saying it doesn't exist.  My problem is if it is installed, it will come back that it exists, however, if it is not, the else statement doesn't do anything and ignores the message box saying it doesn't exist.

I assume this is happening since $objSoftware.Name isn't actually equal to anything since it is not installed and that breaks the check, but I wasn't sure how to get around this.

I know I could check against specific registry entries instead, but I prefer to go the WMI route as that also allows me to use wildcards when doing the "Caption LIKE" check.

Any ideas? 

#NoTrayIcon


$strComputer = "."
$objShell = ObjCreate("WScript.Shell")
$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colSoftware = $objWMIService.ExecQuery ("Select * from Win32_Product where Caption LIKE '%Microsoft SQL Server 2008 R2 Native Client%'")


For $objSoftware In $colSoftware
$String = "Description: " & $objSoftware.Description & @LF & _
"IdentifyingNumber: " & $objSoftware.IdentifyingNumber & @LF & _
"InstallDate: " & $objSoftware.InstallDate & @LF & _
"InstallDate2: " & $objSoftware.InstallDate2 & @LF & _
"InstallLocation: " & $objSoftware.InstallLocation & @LF & _
"InstallState: " & $objSoftware.InstallState & @LF & _
"Name: " & $objSoftware.Name & @LF & _
"PackageCache: " & $objSoftware.PackageCache & @LF & _
"SKUNumber: " & $objSoftware.SKUNumber & @LF & _
"Vendor: " & $objSoftware.Vendor & @LF & _
"Version: " & $objSoftware.Version


If $objSoftware.Name = "Microsoft SQL Server 2008 R2 Native Client" Then
   MsgBox(262144,"Application Status","It exists.")
   Exit
      Else
   MsgBox(262144,"Application Status","It does not exist.")
EndIf


Next

 

Link to comment
Share on other sites

If there are no Win32_Product Where Caption Like...  Then For nothing in nothing means you get nothing.  Return that 'does not exist' based off of $colSoftware

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

What I usually do with WMI is declare all my vars in global. Then I will do the WMI work first, then do the other stuff later. For example, I wouldn't have my If/Endif stuff with MsgBox insdie the For/Next portion that sets the variables. Another reason for this is then this information will be available outside of that. Small example:

#include <File.au3>
#include <array.au3>

Global $objWMIService, $sWMIService, $colItems, $oItem, $sName, $sModel, $db

$sWMIService = "winmgmts:\\" & @ComputerName & "\root\CIMV2"
$objWMIService = ObjGet($sWMIService)
IF IsObj($objWMIService) Then
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%Fingerprint%'")
    If IsObj($colItems) Then
        For $oItem In $colItems
            $sName = $oItem.Name
        Next
    EndIf
    If StringStripWS($sName, 2) = "TouchStrip Fingerprint Sensor" Then
        MsgBox (4096, "Match", "TouchStrip Fingerprint Sensor")
    ElseIf StringStripWS($sName, 2) = "Fingerprint Sensor" Then
        MsgBox (4096, "Match", "Fingerprint Sensor")
    Else
        MsgBox (4096, "Mismatch", $sName)
    EndIf
EndIf

Also in all cases where you are dealing with WMI, you will want some way to output what data is present if the data you are looking for is not there.

Link to comment
Share on other sites

 

I tried a variant of what you had (the code below), but it says it doesn't exist.  Any ideas?  Ultimately, it is querying against the key of HKEY_CLASSES_ROOT\Installer\Products\F33B08125223E324BB1C7789FC3DDCF1, but that will be random, so I want to use the WMI query as opposed to a static reg path to find the information.

 

#include <File.au3>
#include <array.au3>

Global $objWMIService, $sWMIService, $colItems, $oItem, $sName, $sModel, $db

$sWMIService = "winmgmts:\\" & @ComputerName & "\root\CIMV2"
$objWMIService = ObjGet($sWMIService)
IF IsObj($objWMIService) Then
 $colSoftware = $objWMIService.ExecQuery ("Select * from Win32_Product where Caption LIKE '%Microsoft SQL Server 2008 R2 Native Client%'")
   If IsObj($colItems) Then
        For $oItem In $colItems
            $sName = $oItem.Name
        Next
    EndIf
    If $sName = "Microsoft SQL Server 2008 R2 Native Client" Then
        MsgBox (4096, "Match", "It exists.")
    Else
        MsgBox (4096, "Mismatch","It doesn't exist.")
    EndIf
EndIf

 

Link to comment
Share on other sites

You set $colSoftware then check $colItems.   Give them meaningful names to you, prevents that sort of stuff.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Set $colSoftware to what since it is currently set to $objWMIService.ExecQuery ("Select * from Win32_Product where Caption LIKE '%Microsoft SQL Server 2008 R2 Native Client%'")
 

And for $colItems, do you mean like this?:

If $colItems = "Microsoft SQL Server 2008 R2 Native Client" Then
        MsgBox (4096, "Match", "It exists.")
    Else
        MsgBox (4096, "Mismatch","It doesn't exist.")
    EndIf

 

Link to comment
Share on other sites

no, here;

$colSoftware = $objWMIService.ExecQuery ("Select * from Win32_Product where Caption LIKE '%Microsoft SQL Server 2008 R2 Native Client%'")
   If IsObj($colItems) Then

you fill an object name $colSoftware and then check to see if $colItems isObj

Look at Trip's again

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%Fingerprint%'")
    If IsObj($colItems) Then

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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
 Share

  • Recently Browsing   0 members

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