Windows Vista Forums

Testing for Query results
  1. #1


    William Stokes Guest

    Testing for Query results

    Hello,

    I'm fairly new to scripting so helps appreciated.

    Below is part of a script for enumerating installed software. Otherwise
    everything works but if there's no software product found on target computer
    this script fails. So how could I find out if no tested software is
    installed. In this sample it works if strComputer has IBM based apps but if
    not it fails to WSH run time error on line 13 (For Each objSoftware in
    colSoftware) and does not echo anything.

    Here's the code:

    strComputer = "CliName"

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colSoftware = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")

    For Each objSoftware in colSoftware

    If IsNull(objSoftware.Vendor) Then

    Wscript.Echo "Not installed"

    Else

    Wscript.Echo objSoftware.Vendor

    End If

    Next



    So how can I test whether or not "Set colSoftware = objWMIService.ExecQuery
    ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")" returned anything.

    Thanks!







      My System SpecsSystem Spec

  2. #2


    Tom Lavedas Guest

    Re: Testing for Query results

    On Dec 2, 8:27*am, "William Stokes" <w...@xxxxxx> wrote:

    > Hello,
    >
    > I'm fairly new to scripting so helps appreciated.
    >
    > Below is part of a script for enumerating installed software. Otherwise
    > everything works but if there's no software product found on target computer
    > this script fails. So how could I find out if no tested software is
    > installed. In this sample it works if strComputer has IBM based apps but if
    > not it fails to WSH run time error on line 13 (For Each objSoftware in
    > colSoftware) and does not echo anything.
    >
    > Here's the code:
    >
    > strComputer = "CliName"
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >
    > * Set objWMIService = GetObject("winmgmts:" _
    > * * *& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    >
    > * Set colSoftware = objWMIService.ExecQuery _
    > * * *("SELECT * FROM Win32_Product WHERE Vendor='IBM'")
    >
    > For Each objSoftware in colSoftware
    >
    > *If IsNull(objSoftware.Vendor) Then
    >
    > * * *Wscript.Echo "Not installed"
    >
    > *Else
    >
    > * Wscript.Echo objSoftware.Vendor
    >
    > *End If
    >
    > Next
    >
    > So how can I test whether or not "Set colSoftware = objWMIService.ExecQuery
    > ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")" returned anything.
    >
    > Thanks!
    Your code did not error on my machine in a local search for software
    that was not installed. It didn't reach to "Not Installed" message
    either - as I expected. The logic of the FOR/EACH statement is such
    that that message cannot be reached.

    Though I didn't get the error on a local search, it might be that the
    remote search might fail differently. So, I would propose something
    like the following to handle all possible error conditions ...

    strComputer = "CliName"
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
    \cimv2")

    Set colSoftware = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")

    nCount = 0
    on error resume next
    nCount = colSoftware.count
    on error goto 0

    if nCount > 0 then
    For Each objSoftware in colSoftware
    Wscript.Echo objSoftware.Vendor
    Next
    else
    Wscript.Echo "Not found"
    end if

    Tom Lavedas
    ***********
    http://there.is.no.more/tglbatch/

      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: Testing for Query results


    "Tom Lavedas" <tglbatch@xxxxxx> wrote in message
    news:caab6918-0705-4588-94b8-34b5b2424a7a@xxxxxx
    On Dec 2, 8:27 am, "William Stokes" <w...@xxxxxx> wrote:

    > Hello,
    >
    > I'm fairly new to scripting so helps appreciated.
    >
    > Below is part of a script for enumerating installed software. Otherwise
    > everything works but if there's no software product found on target
    > computer
    > this script fails. So how could I find out if no tested software is
    > installed. In this sample it works if strComputer has IBM based apps but
    > if
    > not it fails to WSH run time error on line 13 (For Each objSoftware in
    > colSoftware) and does not echo anything.
    >
    > Here's the code:
    >
    > strComputer = "CliName"
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >
    > Set objWMIService = GetObject("winmgmts:" _
    > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    >
    > Set colSoftware = objWMIService.ExecQuery _
    > ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")
    >
    > For Each objSoftware in colSoftware
    >
    > If IsNull(objSoftware.Vendor) Then
    >
    > Wscript.Echo "Not installed"
    >
    > Else
    >
    > Wscript.Echo objSoftware.Vendor
    >
    > End If
    >
    > Next
    >
    > So how can I test whether or not "Set colSoftware =
    > objWMIService.ExecQuery
    > ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")" returned anything.
    >
    > Thanks!
    Your code did not error on my machine in a local search for software
    that was not installed. It didn't reach to "Not Installed" message
    either - as I expected. The logic of the FOR/EACH statement is such
    that that message cannot be reached.

    Though I didn't get the error on a local search, it might be that the
    remote search might fail differently. So, I would propose something
    like the following to handle all possible error conditions ...

    strComputer = "CliName"
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
    \cimv2")

    Set colSoftware = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")

    nCount = 0
    on error resume next
    nCount = colSoftware.count
    on error goto 0

    if nCount > 0 then
    For Each objSoftware in colSoftware
    Wscript.Echo objSoftware.Vendor
    Next
    else
    Wscript.Echo "Not found"
    end if

    Tom Lavedas
    ***********
    http://there.is.no.more/tglbatch/

    ----------
    The code should never result in an error, even if Vendor never matches
    "IBM". The collection (colSoftware) will be empty and no statements in the
    For Each loop will execute. I also see no need for
    IsNull(objSoftware.Vendor). I never see a runtime error, unless there is a
    problem on the remote client or I lack permissions.

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

  4. #4


    William Stokes Guest

    Re: Testing for Query results

    Ok, thanks. I'll try this.

    > nCount = 0
    > on error resume next
    > nCount = colSoftware.count
    > on error goto 0
    >
    > if nCount > 0 then
    > For Each objSoftware in colSoftware
    > Wscript.Echo objSoftware.Vendor
    > Next
    > else
    > Wscript.Echo "Not found"
    > end if
    Will


    "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> kirjoitti
    viestissä:e9xOwFLVJHA.4300@xxxxxx

    >
    > "Tom Lavedas" <tglbatch@xxxxxx> wrote in message
    > news:caab6918-0705-4588-94b8-34b5b2424a7a@xxxxxx
    > On Dec 2, 8:27 am, "William Stokes" <w...@xxxxxx> wrote:

    >> Hello,
    >>
    >> I'm fairly new to scripting so helps appreciated.
    >>
    >> Below is part of a script for enumerating installed software. Otherwise
    >> everything works but if there's no software product found on target
    >> computer
    >> this script fails. So how could I find out if no tested software is
    >> installed. In this sample it works if strComputer has IBM based apps but
    >> if
    >> not it fails to WSH run time error on line 13 (For Each objSoftware in
    >> colSoftware) and does not echo anything.
    >>
    >> Here's the code:
    >>
    >> strComputer = "CliName"
    >>
    >> Set objFSO = CreateObject("Scripting.FileSystemObject")
    >>
    >> Set objWMIService = GetObject("winmgmts:" _
    >> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    >>
    >> Set colSoftware = objWMIService.ExecQuery _
    >> ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")
    >>
    >> For Each objSoftware in colSoftware
    >>
    >> If IsNull(objSoftware.Vendor) Then
    >>
    >> Wscript.Echo "Not installed"
    >>
    >> Else
    >>
    >> Wscript.Echo objSoftware.Vendor
    >>
    >> End If
    >>
    >> Next
    >>
    >> So how can I test whether or not "Set colSoftware =
    >> objWMIService.ExecQuery
    >> ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")" returned anything.
    >>
    >> Thanks!
    >
    > Your code did not error on my machine in a local search for software
    > that was not installed. It didn't reach to "Not Installed" message
    > either - as I expected. The logic of the FOR/EACH statement is such
    > that that message cannot be reached.
    >
    > Though I didn't get the error on a local search, it might be that the
    > remote search might fail differently. So, I would propose something
    > like the following to handle all possible error conditions ...
    >
    > strComputer = "CliName"
    > Set objWMIService = GetObject("winmgmts:" _
    > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root
    > \cimv2")
    >
    > Set colSoftware = objWMIService.ExecQuery _
    > ("SELECT * FROM Win32_Product WHERE Vendor='IBM'")
    >
    > nCount = 0
    > on error resume next
    > nCount = colSoftware.count
    > on error goto 0
    >
    > if nCount > 0 then
    > For Each objSoftware in colSoftware
    > Wscript.Echo objSoftware.Vendor
    > Next
    > else
    > Wscript.Echo "Not found"
    > end if
    >
    > Tom Lavedas
    > ***********
    > http://there.is.no.more/tglbatch/
    >
    > ----------
    > The code should never result in an error, even if Vendor never matches
    > "IBM". The collection (colSoftware) will be empty and no statements in the
    > For Each loop will execute. I also see no need for
    > IsNull(objSoftware.Vendor). I never see a runtime error, unless there is a
    > problem on the remote client or I lack permissions.
    >
    > --
    > Richard Mueller
    > MVP Directory Services
    > Hilltop Lab - http://www.rlmueller.net
    > --
    >
    >


      My System SpecsSystem Spec

Testing for Query results problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
WMI query results not giving the expected result Matabra PowerShell 3 08 Jul 2009
LDAP Group Query Results Limitation Bart Perrier VB Script 2 09 Apr 2009
Re: ArrayList of REG QUERY results Tao Ma PowerShell 1 29 May 2008
Re: sql query, how to check for no results Kiron PowerShell 0 06 Dec 2007
testing Ross M. Greenberg Vista General 1 23 Mar 2007