简体   繁体   English

循环 X 次

[英]Loop X number of times

I'm working on my first PowerShell script and can't figure the loop out.我正在编写我的第一个 PowerShell 脚本,但无法弄清楚循环。

I have the following, which will repeat $ActiveCampaigns number of times:我有以下内容,它将重复$ActiveCampaigns次数:

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.) $PQCampaign1 (应该变成$PQCampaign2 ,然后是 3 等等)
  • $PQCampaignPath1 (should become $PQCampaignPath2 , then 3, etc.) $PQCampaignPath1 (应该变成$PQCampaignPath2 ,然后是 3 等等)

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 .因此,如果某天$ActiveCampaigns设置为 8,则需要重复 8 次,最后一次必须打开位于 $ $PQCampaign3中的$PQCampaignPath8

How can I fix this?我怎样才能解决这个问题?

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

This may be what you are looking for: 这可能是您正在寻找的:

for ($i=1; $i -le $ActiveCampaigns; $i++)
{
  $PQCampaign     = Get-Variable -Name "PQCampaign$i"     -ValueOnly
  $PQCampaignPath = Get-Variable -Name "PQCampaignPath$i" -ValueOnly

  # Do stuff with $PQCampaign and $PQCampaignPath
}

Here is a simple way to loop any number of times in PowerShell. 这是在PowerShell中循环任意次数的简单方法。

It is the same as the for loop above, but much easier to understand for newer programmers and scripters. 它与上面的for循环相同,但对于较新的程序员和脚本编写者来说更容易理解。 It uses a range, and foreach. 它使用范围和foreach。 A range is defined as: 范围定义为:

range = lower..upper

or 要么

$range = 1..10

A range can be used directly in a for loop as well, although not the most optimal approach, any performance loss or additional instruction to process would be unnoticeable. 范围也可以直接在for循环中使用,尽管不是最优的方法,但是任何性能损失或额外的处理指令都是不明显的。 The solution is below: 解决方案如下:

foreach($i in 1..10){
    Write-Host $i
}

Or in your case: 或者在你的情况下:

$ActiveCampaigns = 10
foreach($i in 1..$ActiveCampaigns)
{
    Write-Host $i
    If($i==$ActiveCampaigns){
        // Do your stuff on the last iteration here
    }
}

See this link . 看到这个链接 It shows you how to dynamically create variables in PowerShell. 它向您展示了如何在PowerShell中动态创建变量。

Here is the basic idea: 这是基本的想法:

Use New-Variable and Get-Variable , 使用New-VariableGet-Variable

for ($i=1; $i -le 5; $i++)
{
    New-Variable -Name "var$i" -Value $i
    Get-Variable -Name "var$i" -ValueOnly
}

(It is taken from the link provided, and I don't take credit for the code.) (它取自提供的链接,我不承认代码。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM