Finding odd and even numbers

I have created a script that will put a collection into a html table and send it in an eamil. I would like to add an alternating color for each table row in the html table. I am building the table and each row by looping through a collection and have added $LoopCount++ count each time the loop is started. I want to use the $LoopCount++ to generate a number and based on the number being odd or even i will update the <tr "bgcolor='7FA96F'"> tag. I am not married to this method but it was the only way i could think of to get alternating colors. I am missing the logic to find out if the loopcount is an odd or even number. Any ideas would help...

	FOREACH($Item in $SortedMBXProps)
 { 
  $LoopCount ++
  ## Change to color 1
  IF($LoopCount -eq "True")
   {
   $list += "<tr bgcolor='7FA96F'>"
   }
   ## Change to color 2
    ELSE
    {
    $list += "<tr bgcolor='FFFFFF'>"
    }
  $list += "<td>" + $Item.DisplayName + "</td>"
  $list += "<td>" + $Item."IssueWarningQuota(MB)" + "</td>"
  $list += "<td>" + $Item."TotalItemSize(MB)" + "</td>"
  $list += "<td>" + $Item."ProhibitSendQuota(MB)" + "</td>"
  $list += "<td>" + $Item.StorageLimitStatus + "</td>"
  $list += "<td>" + $Item.ServerName + "</td>"
  $list += "</tr>"
 }
$list += "</table></font>"
January 8th, 2013 8:41pm

IF($LoopCount % 2 -eq 0)

{even number case}

ELSE

{odd number case}

Free Windows Admin Tool Kit Click here and download it now
January 8th, 2013 8:48pm

IF($LoopCount % 2 -eq 0)

{even number case}

ELSE

{odd number

January 8th, 2013 9:09pm

Alternative to the If/Else construction:

$list = @()
$colors = @{
             0 = '7FA96F'
             1 = 'FFFFFF'
            }
foreach ($loopcounter in (1..10)){
        $list += "<tr bgcolor=<'{0}'>" -f $colors[$loopcounter % 2]
}
$list

Free Windows Admin Tool Kit Click here and download it now
January 8th, 2013 9:38pm

Hello!

I know, this thread is old, but where is this documented?
I wanna understand what this % 2 means.

Thanks!

March 30th, 2015 7:20pm

Hello!

I know, this thread is old, but where is this documented?
I wanna understand what this % 2 means.

Thanks!

See the 'Arithmetic operators' section here:

http://ss64.com/ps/syntax-variables.html

Free Windows Admin Tool Kit Click here and download it now
March 30th, 2015 7:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics