Page 1 of 1 1
Topic Options
#208686 - 2014-03-06 03:33 PM How to get a for each statement nested within a for each statement
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I am not getting this one right. I have tried different variations, but still no joy!

My Function is as follows:

 Code:
Function GetW7AVinfo(optional $machine)
dim $oSWbem,$colItems,$av[4],$colItem
 if not $machine $machine = "." endif
 
$Product= "AntiVirusProduct"
$Product= "AntiSpywareProduct"
$Product= "FirewallProduct"

 $oSWbem = GetObject("winmgmts:\\" + $machine + "\root\SecurityCenter2")
 If @Error exit @error EndIf
 For each $SelectStatement in $Product
 $colItems = $oSWbem.ExecQuery("Select * From " + $SelectStatement)
 If @Error exit @error EndIf

 For Each $colItem In $colItems
  $av[0] = $colItem.productState
  $av[1] = $colItem.displayName
  $av[2] = $colItem.instanceGuid
  $av[3] = $colItem.pathToSignedProductExe
  $av[4] = $colItem.pathToSignedReportingExe
 Next
? $av[1]
? $av[2]
? $av[3]
? $av[4]
? $av[0]
if  $av[0] = 0 ? $Product + "not installed"
else
endif
EndFunction


If I replace $SelectStament with either AntiVirusProduct, AntiSpywareProduct and get rid of the for each $selectstatement, then code works perfectly. But I don't want to have three Functions GetW7AVinfo and Function GetW7ASinfo and Function GetW7FWinfo. In other words this works $colItems = $oSWbem.ExecQuery("Select * From AntiVirusProduct"), but I want the script to loop and replace AntiVirusProduct with AntiSpywareProduct and then FirewallProduct if that makes sense

I know that it can be done, but I cannot work out how to call AntivirusProduct, run the rest of the UDF and then return back to the top and call Antispyware Product. What the script is doing is not calling anything as its obviously not using the For each $SelectStatement in $Product.

lastly, what would be the best option to alert the administrator to a problem. My intention is to complete the coding for the script and run it as a scheduled task on each computer once every fortnight. If there is problem, the script will write to a file on the server giving out name of computer and what is not installed or up to date. But not sure whether there is a better way.

Top
#208687 - 2014-03-06 03:38 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
 Code:
$Product= "AntiVirusProduct" 
$Product= "AntiSpywareProduct" 
$Product= "FirewallProduct"


does not array make.
 Code:
$Product="AntiVirusProduct", "AntiSpywareProduct", "FirewallProduct"
_________________________
!

download KiXnet

Top
#208688 - 2014-03-06 03:50 PM Re: How to get a for each statement nested within a for each statement [Re: Lonkero]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I tried that option and it didn't work. Oh well I will test it again.
Top
#208690 - 2014-03-06 03:55 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
How bizarre. It worked this time. Maybe I had something different in the script when I was trying an hour ago. its working now so ignore that.

I don't know if the problem with the Code highlighting should be default issue is causing my current issues with kixtart, but I am getting double posts and it hangs when posting anything. I have to click try again or type in http://www.kixtart.org to get back to the website. May be unrelated to the code highlighting change, but I thought that I would mention it.

Top
#208692 - 2014-03-06 04:06 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm... I have had that happen to me too. anyhow, the error we are seeing is a database timeout, which is unrelated and is most likely caused by surge in crawler traffic or an active attack.
_________________________
!

download KiXnet

Top
#208693 - 2014-03-06 05:29 PM Re: How to get a for each statement nested within a for each statement [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I deleted the dup posts - you should be able to delete your own - Edit / Delete Post..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208694 - 2014-03-06 05:41 PM Re: How to get a for each statement nested within a for each statement [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Just a comment from the perspective of an instructor...
 Code:
Function GetW7AVinfo(optional $machine)
dim $oSWbem,$colItems,$av[4],$colItem
 if not $machine $machine = "." endif
 
$Product= "AntiVirusProduct"
$Product= "AntiSpywareProduct"
$Product= "FirewallProduct"

 $oSWbem = GetObject("winmgmts:\\" + $machine + "\root\SecurityCenter2")
 If @Error exit @error EndIf
 For each $SelectStatement in $Product
contains errors as Lonk pointed out, but also is not as clear as it could be..

You have multiple products in an array, so declare (DIM!) the array of products (plural!)and define the array
 Code:
Dim $aProducts, $Product     ; array of products, enumerator var
$aProducts = 'prod 1', 'prod 2', 'prod 3'
Now you have an array that clearly indicates its content - a list of products, and a variable used to step through (enumerate) that list with a meaningful name - $Product.
 Code:
For Each $Product in $Products
The code is the same, but the clarity is improved.

Your loop
 Code:
For Each $ColItem in $colItems
is also unclear. The "col" in front of the "real" variable name Items ($colItems) implies that it is a collection of items from an object reference. $colItem isn't a collection, so the "col" prefix is misleading. A better form would be
 Code:
For Each $Item in $colItems
Again, you follow a singular/plural format for the enumeration, but don't imply that the enumerated object is a collection.

Simple syntax changes that make your code more readable and understandable, both to others and to your "future" self.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208695 - 2014-03-06 05:54 PM Re: How to get a for each statement nested within a for each statement [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I think we need to close Korg down and hire Glenn. He comes up with all the answers! I never noticed that there was a delete option when editing the post. I just didn't see a delete option where one sees the reply, quote, quick reply etc. Thanks Glenn, I will remember that.

Does anyone know how I work out where in this script the loop function starts. I have tried to reset the variable to be blank after each loop but cannot get it to work. The reason being is that the script is returning the details of the anti virus, then the details of the anti spyware, but when it is supposed to return the details of the firewall it should be returning blanks for the firewall, but instead its returning the information received in the last loop from the anti spyware!

I have tried to put in an exit function so that the coding exits if say a third party firewall is not installed, but this does not work and I think its because its still finding a class called firewallproduct in WMI even if nothing is installed and using it. The working script that I have at the moment is:

 Code:
Function GetW7AVinfo(optional $machine)
dim $oSWbem,$colItems,$av[4],$colItem
 if not $machine $machine = "." endif
 
$Product = "AntiVirusProduct", "AntiSpywareProduct", "FirewallProduct"

 $oSWbem = GetObject("winmgmts:\\" + $machine + "\root\SecurityCenter2")
 If @Error exit @error EndIf
 For each $SelectStatement in $Product

 $colItems = $oSWbem.ExecQuery("Select * From " + $SelectStatement)
 If @Error exit @error EndIf

 For Each $colItem In $colItems
  $av[0] = $colItem.productState
  $av[1] = $colItem.displayName
  $av[2] = $colItem.instanceGuid
  $av[3] = $colItem.pathToSignedProductExe
  $av[4] = $colItem.pathToSignedReportingExe
 Next
? $av[1]
? $av[2]
? $av[3]
? $av[4]
? $av[0]

;    Select $iProductState = Hex($colItem.productState, 8)
;	Case ($iProductState, 5, 2) = "00" or "01"
;	 $aReturn[5] = "Disabled"
;	 Case "10", "11"
;	 $aReturn[5] = "Enabled"
;	 EndSelect
if  $av[0] = 0 ? "Something not installed"
else
endif
EndFunction

Exit


What I am trying to get it to do go through each loop and ultimately it will send to a file anything that is not installed/updated/enabled etc. I will get to the file bit later, but I cannot work out where to dim the variable so that $ColItems is set to zero after loop. I think part of the problem is that you cannot dim an array - $product which is why its probably not working. But reading the documentation the variable is retained only within the if else endif statement therefore I assumed that the variable would only be remembered for one loop within the For each statement! Or does remembering the value of the variables only get eliminated within if else endif statements and not in for net and while .. do until statements?

Top
#208696 - 2014-03-06 05:55 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
oh just noticed your updated post now Glenn. You were obviously typing that out while I was typing my post out!
Top
#208697 - 2014-03-06 06:27 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
You probably want something more like this:
 Code:
Function GetW7AVinfo(optional $_Machine)

  Dim $_oSWbem			; COM Object pointer for query
  Dim $_colItems,  $_Item	; collection of items, enumerator var
  Dim $_Product			; Product type to retrieve
  Dim $_Av[4]			; 5-element array to return

  ; Define localhost if Machine Name not provided
  If Not $_Machine $_Machine = '.' EndIf
 
  ; Define product to find
  $_Product = 'AntiVirusProduct'

  $_oSWbem = GetObject('winmgmts:\\' + $_Machine + '\root\SecurityCenter2')
  If @ERROR Exit @ERROR EndIf

  
  ; Get collection of items for product type
  $_colItems = $_oSWbem.ExecQuery('Select * From ' + $_Product)
   If @ERROR Exit @ERROR EndIf

  ; process the collection and assign the desired object values 
  For Each $Item In $colItems
    $_Av[0] = $Item.productState
    $_Av[1] = $Item.displayName
    $_Av[2] = $Item.instanceGuid
    $_Av[3] = $Item.pathToSignedProductExe
    $_Av[4] = $Item.pathToSignedReportingExe
  Next

  ; DEBUGGING
  $_Product @CRLF		; show current product type
  Join($_Av, @CRLF) @CRLF	; show array, 1 element per line

;    Select $iProductState = Hex($colItem.productState, 8)
;	Case ($iProductState, 5, 2) = '00' or '01'
;	 $aReturn[5] = 'Disabled'
;	 Case '10', '11'
;	 $aReturn[5] = 'Enabled'
;	 EndSelect

  ; Prepare to return the data
  $GetW7AVinfo = $_Av

  ; Validate results of query and return exit status accordingly
  If  $_Av[0] = 0
    ; ? 'Something not installed'	; BAD! Do not generate output from UDFs
    Exit 2				; not found, optionally use "87" for invalid value
  EndIf

  Exit 0				; success!

EndFunction
You probably want to move the commented code that deals with element 5 of the AV array to your core code, and use the correct index value, as "5" is beyond the 5th element of the array (Dim $_Av[4]).

Also - the two lines after the ; DEBUGGING comment should be removed after testing.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208753 - 2014-03-21 07:19 PM Re: How to get a for each statement nested within a for each statement [Re: Glenn Barnas]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Hi Glenn, sorry I never got back to you. Been one thing after another. I had a good go at the anti virus script this week, but in the end I just gave up as I couldn't get it to work properly. I will get my sister who is a computer programmer to show me what I am doing wrong as programming is simply not my forte! And I don't have time!

But thank you especially for the debugging line. Very useful to know how to return the results using two lines of code instead of what I was doing!

On a completely different topic, I have been researching UPS's as I know almost nothing about them. After several weeks research I have decided to buy Eaton UPS's. One thing that stood out is how well Eaton works with generators which may be of interest to those in the US where hurricanes frequently prevail as apparently there are a lot of issues with APC and Tripplite line interactive UPS and generators down to the very narrow frequency range - or something to that effect. So if anyone is looking at UPS's then based on my humble research I think I would recommend Eaton. When I actually buy one, I will let you know what I think of them although as I don't really work with UPS's it will be difficult to compare them to anything else! A great forum for learning about UPS is the website spiceworks.

Top
#208754 - 2014-03-21 07:56 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
talk about advertisement.

I have had no issue with APC battery backups and gas powered generator. they are way more reliable than the generator they are hooked into \:\)
_________________________
!

download KiXnet

Top
#208755 - 2014-03-22 12:06 PM Re: How to get a for each statement nested within a for each statement [Re: Lonkero]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Actually the comment was meant for people like Glenn who live in areas where hurricanes prevail as there is a need for a generator and UPS that work with a generator. I have no need for a generator, but I thought that I would pass on that little titbit that I picked up in my research. However, the issue with the APC and Tripplite if I have understood the documentation correctly is to do with Line interactive UPS and not Online UPS which is what you may be using.

Actually the thing that put me off the APC was the lifespan of the batteries and no justifiable reason for the extra cost. More than one post said that they got better battery lifespan from the Eaton and when you consider the batteries are like 80% of the cost of the UPS, for the battery renewal schedule is of great importance. I am not saying that APC are bad, but I can't help wondering if you are paying a premium price for the logo mor than anything else to be honest. Anyway.

Top
#208756 - 2014-03-22 11:35 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
I actually use an APC 7.5KVA unit that runs my environment for about 20 minutes. I have run it on a generator without issue. The larger units like this allow you to define the frequency range for input, outside of which it will switch to its inverter in an AC-DC-AC flow. The smaller units, under 2.2K, don't have the same capabilities and might be affected by generators. My current gen is a 7.5KVA unit as well.

FYI - you can see my network environment here - scroll about 3/4 down the page.

I've been using this unit since 2010, purchased refurbished with fresh batteries.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208757 - 2014-03-23 04:35 AM Re: How to get a for each statement nested within a for each statement [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I have had small (currently smallest is 500W) and big ones hooked up to the generator and like I said, I have had no issues whatsoever. what I have noticed is the batteries dying a bit too fast on some units. it's like the units constantly overcharge them or something.

and Rob... the utility sucks in some places here so bad that anyone with a computer needs an UPS. in worst times electricity goes out once a day.
_________________________
!

download KiXnet

Top
#208758 - 2014-03-23 10:50 AM Re: How to get a for each statement nested within a for each statement [Re: Lonkero]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Whoa guys, let me say this again. I have no issues with APC - never used them. All I am saying is that based on my research I believe that Eaton is better than APC and I was just passing it on for anyone that is interested. Lonk, your point about the batteries dying a bit too fast is the main concern that I have with APC, this is the reason why I am looking at Eaton as quite a few people made that exact same complaint!

To be fair to me, my interest in UPS's is more to protect from brown surges and to keep servers online when electrician's shut the power down as my clients never seem to be able to switch the servers on without having some sort of problem. Compared to you guys, the electricity here is rock solid stable. If the electricity goes out once every two years its a lot!

Top
#208759 - 2014-03-23 04:05 PM Re: How to get a for each statement nested within a for each statement [Re: Robdutoit]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yea. European electricity utilities are actually reliable. and in Finland elec power is so abundant that huge portion of the country heats with electricity.
why I said something about advertising is that you came out out of the blue about UPS product in an unrelated thread. and now we effectively hijacked it. I like Eaton but based on my buying experience, they are always way more expensive than APC and APC gets the job done. My network equipment backing Belkin UPS at home just decided to quit working right (randomly shuts down after few days) which is something you would not want, but for a rough $50 investment and 4 years of reliable internet when everyone else keeps going dark, it served me good. would a better product serve me longer? sure. will the difference in price be captured by longer service? I doubt it. Thinking my next setup is going to be homegrown one which utilizes solar and wind backed battery bank. but until I get all the paperwork sorted out, I bet the remedy is going to remain cheap APC or noname units.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.08 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org