添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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.
    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?

    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
                    It's quite hard to believe that anything in PowerShell is implemented as easily understandable as that...
                        – Bowi
                    Jul 24 '17 at 14:37
                        

    Here is a simple way to loop any number of times in PowerShell.

    It is the same as the for loop above, but much easier to understand for newer programmers and scripters. It uses a range, and foreach. A range is defined as:

    range = lower..upper
    
    $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. 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.

    Here is the basic idea:

    Use New-Variable and Get-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.)

    improve this answer Thanks. I'll play with that a bit - but I don't think that solves my issue. My variables already exist, I just want to change the variable that is being called in each loop... – user2725402 Aug 28 '13 at 13:43