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