Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop X number of times

Tags:

powershell

I'm working on my first PowerShell script and can't figure the loop out.

I have the following, which will repeat $ActiveCampaigns number of times:

Write-Host "Creating $PQCampaign1 Pre-Qualified Report" Invoke-Item "$PQCampaignPath1\PQ REPORT $PQCampaign1.qvw" Write-Host "Waiting 1 minute for QlikView to update" sleep -seconds 60 # Wait 1 minute for QlikView to Reload, create Report and Save.  DO{     Write-Host "Daily Qlikview Reports"     Write-Host "Wating for QlikView to create the $PQCampaign1 PQ Report"     Get-Date     Write-Host "Checking...."     sleep -seconds 1     Write-Host ""     Write-Host "Not Done Yet"     Write-Host "Will try again in 5 seconds."     Write-Host ""     sleep -seconds 5 }  Until (Test-Path "$PQCampaignPath1\$PQCampaign1 $PQReportName $ReportDate.xlsx" -pathType leaf)  Get-Date Write-Host "Done with $PQCampaign1 PQ Report. Wait 10 seconds." sleep -seconds 10 

These parameters need to increase with one for each loop:

  • $PQCampaign1 (should become $PQCampaign2, then 3, etc.)
  • $PQCampaignPath1 (should become $PQCampaignPath2, then 3, etc.)

So if $ActiveCampaigns is set to 8 on a certain day, then this needs to repeat 8 times and the last time it must open $PQCampaign3 which lies in $PQCampaignPath8.

How can I fix this?

like image 938
user2725402 Avatar asked Aug 28 '13 12:08

user2725402


People also ask

Which loop loops a set number of times?

A For Loop executes the section of code inside the loop, called a subdiagram, a set number of times. The count (N) terminal sets the number of times to repeat the subdiagram. One subdiagram repetition is called an iteration.

What is X in a for loop?

x itself has no special meaning, it simply (as a part of the for loop) provides a way to repeat print random.randint(1,101) 10 times, regardless of the variable name (i.e., x could be, say, n ). In each iteration the value of x keeps increasing, but we don't use it.


1 Answers

Use:

1..10 | % { write "loop $_" } 

Output:

PS D:\temp> 1..10 | % { write "loop $_" } loop 1 loop 2 loop 3 loop 4 loop 5 loop 6 loop 7 loop 8 loop 9 loop 10 
like image 152
cmcginty Avatar answered Oct 10 '22 03:10

cmcginty