![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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! 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 Specs![]() |
| | #3 (permalink) |
| | 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! 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 Specs![]() |
| | #4 (permalink) |
| | 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 "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 Specs![]() |
![]() |
| 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 | |||