ForEach l oop перезапись вывода в Powershell - PullRequest
0 голосов
/ 28 мая 2020

Я работаю над сценарием PowerShell для l oop через массив запросов API для каждого дня недели.

Я столкнулся с проблемой ... когда я l oop через скрипт вывод для каждого дня перезаписывается, в результате чего значение для каждого дня остается одинаковым.

Где я ошибаюсь?

DATE        PAGE    RANGE   VALUE
18/05/2020, PAGE 1, 0_2000, 12345
18/05/2020, PAGE 1, 2001_4000, 23456
18/05/2020, PAGE 1, 8001_40000, 45678
19/05/2020, PAGE 1, 0_2000, 12345
19/05/2020, PAGE 1, 2001_4000, 23456
19/05/2020, PAGE 1, 8001_40000, 45678
20/05/2020, PAGE 1, 0_2000, 12345
20/05/2020, PAGE 1, 2001_4000, 23456
20/05/2020, PAGE 1, 8001_40000, 45678
…
#Date
$GetDate = [datetime](Get-Date).AddDays((-1 * (Get-Date).DayOfWeek.Value__) - 6).Date
$week = @(0..5)
$WeeklyOutput = @()

#API Query
$REQUESTS = @(
    ("PAGE1","0_2000","QUERY_1",""),
    ("PAGE1","2001_4000","QUERY_2",""),
    ("PAGE1","8001_40000","QUERY_3","")  
)

#Structures the 'OutputClass'
class OutputClass {
    [string]$DATE
    [string]$PAGE
    [string]$RANGE
    [string]$VALUE;
}

#Iterates through the script for the days specified
$week = @(0..6)
Foreach ($dayofweek in $week) {
    $DailyOutput = @()
    $StartDate = $GetDate.AddDays($dayofweek)
    $start = ([int]::Parse((Get-Date $StartDate -UFormat %s)))*1000
    $EndDate = $StartDate.AddDays(1)
    $end = ([int]::Parse((Get-Date $EndDate -UFormat %s)))*1000
    $PrintDate = $StartDate.toString("dd/MM/yyyy")
    #Iterates through all the Requests     
    Foreach ($REQUEST in $REQUESTS) {

        $REQUEST[3] = Invoke-RestMethod -Uri $url -Method POST -Headers $headers -ContentType $contentType -Body $REQUEST[2] | Select -expand results

        #Appends each requests to the Daily Output
        $DailyOutput += [OutputClass]@{DATE=$printdate;PAGE=$REQUEST[0];RANGE=$REQUEST[1];VALUE=$REQUEST[3]}                
    }

    #Appends each Daily Output to the Weekly Output                                
    $WeeklyOutput += $DailyOutput 
}

$WeeklyOutput

Заранее спасибо.

1 Ответ

0 голосов
/ 28 мая 2020

Я не могу воспроизвести ваш вызов API, поэтому вместо этого я просто заполнил его увеличивающимся счетчиком. Нет ничего плохого в том, что у вас там было, но у вас есть некоторые несоответствия, когда вы работаете с датами, и большинство из них не используются ни для чего в коде, который вы вставили.

$WeeklyOutput = @() 

$REQUESTS = @(
             ("PAGE1","0_2000","QUERY_1",""),
             ("PAGE2","2001_4000","QUERY_2",""),
             ("PAGE3","8001_40000","QUERY_3","")  
             )

#Structures the 'OutputClass'
class OutputClass {
    [string]$DATE
    [string]$PAGE
    [string]$RANGE
    [string]$VALUE;
}

#Iterates through the script for the days specified
$week = @(0..6)
$Count = 1
Foreach ($dayofweek in $week) {

        $DailyOutput = @()
        $StartDate = (Get-Date).AddDays($dayofweek).                        # Changed $GetDate to (Get-Date)
        $Start = ([int]::Parse((Get-Date $StartDate -UFormat %s)))*1000     # What is this here for?
        $EndDate = $StartDate.AddDays(1)                                    # Referenced but not used.
        $End = ([int]::Parse((Get-Date $EndDate -UFormat %s)))*1000         # What is this here for?
        $PrintDate = $StartDate.toString("dd/MM/yyyy")

        #Iterates through all the Requests     
        Foreach ($REQUEST in $REQUESTS) {
            #Appends each requests to the Daily Output
            $REQUEST[3] = $Count++
            $DailyOutput += [OutputClass]@{DATE=$printdate;PAGE=$REQUEST[0];RANGE=$REQUEST[1];VALUE=$REQUEST[3]}                
        }

        #Appends each Daily Output to the Weekly Output                                
        $WeeklyOutput += $DailyOutput 
}

$WeeklyOutput

Вывод

DATE       PAGE  RANGE      VALUE
----       ----  -----      -----
28/05/2020 PAGE1 0_2000     1
28/05/2020 PAGE2 2001_4000  2
28/05/2020 PAGE3 8001_40000 3
29/05/2020 PAGE1 0_2000     4
29/05/2020 PAGE2 2001_4000  5
# Shortened sequential output
02/06/2020 PAGE3 8001_40000 18
03/06/2020 PAGE1 0_2000     19
03/06/2020 PAGE2 2001_4000  20
03/06/2020 PAGE3 8001_40000 21

Разве это не то, чего вы ожидаете? Если нет проблемы с выводом API, я не могу понять, в чем еще проблема.

...