Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

powershell - Loop X number of times

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 "$PQCampaignPath1PQ 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?

question from:https://stackoverflow.com/questions/18488372/loop-x-number-of-times

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

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

Output:

PS D:emp> 1..10 | % { write "loop $_" }
loop 1
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
loop 9
loop 10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...