question

ChristopherJack-1763 avatar image
0 Votes"
ChristopherJack-1763 asked RichMatheisen-8856 answered

Looping Question with indexes - Powershell

Hi,

I am trying to get a loop working for one order - I am having issues because the index of the array starts at 0


 $null = For ($i=0; $i -le $OrderNumbers.count-1; $i++) 
 {
        
 }

I have tried using a temp variable to say if order numbers .count = 1 to set to two but then that loops through twice once for 0 and once for 1.

I am only wanting it to loop through once for one record.

Any help appreciated

This test does not seem to work at all


  $q = 0
     $t = 1
     $OrderNumbers = @(1,2)
     $OrderNumbers.count
     $x=1
     for ($y=0; $y -le $OrderInfo.count-1; $y++)
     {
         "yo"
         $x++
         $x
     }





windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OlafHelper-2800 avatar image
1 Vote"
OlafHelper-2800 answered

$OrderNumbers = @(1,2) … $OrderInfo.count-1

In the array declaration you use name $OrderNumbers and in the for loop an other name $OrderInfo ; that don't work =>

 $q = 0
 $t = 1
 $OrderNumbers = @(1,2,3,4);
 Write-Host $OrderNumbers.count;
 $x=1;
 for ($y=0; $y -le $OrderNumbers.count -1; $y++)
 {
     Write-Host "yo";
     $x++;
     Write-Host $x;
 }






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

There's really no need for the For-loop.

 $OrderNumbers = @(1,2)
 $x=1
 $OrderNumbers |
     ForEach-Object{
         Write-Host "OrderNumber: $_" -ForegroundColor Yellow
         "yo"
         $x++
         $x
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.