Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Testing for Query results

Reply
 
Old 12-02-2008   #1 (permalink)
William Stokes


 
 

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
Old 12-02-2008   #2 (permalink)
Tom Lavedas


 
 

Re: Testing for Query results

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

> 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
Old 12-02-2008   #3 (permalink)
Richard Mueller [MVP]


 
 

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:
Quote:

> 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
Old 12-03-2008   #4 (permalink)
William Stokes


 
 

Re: Testing for Query results

Ok, thanks. I'll try this.
Quote:

> 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
Quote:

>
> "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:
Quote:

>> 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
Reply

Thread Tools


Similar Threads
Thread Forum
WMI query results not giving the expected result PowerShell
LDAP Group Query Results Limitation VB Script
Re: ArrayList of REG QUERY results PowerShell
Re: sql query, how to check for no results PowerShell
testing dk Vista print fax & scan


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46