Powershell Рассчитать итоги из массива из нескольких строк - PullRequest
0 голосов
/ 06 ноября 2018

У меня есть массив со следующими данными. Мне нужно выяснить, как объединить данные и сообщить о них на основе JobName. Если задание имеет статус «только успешное», оно будет иметь в общей сложности 33, если оно имеет три состояния, ему необходимо сосчитать все имена заданий вместе.

JobName  Status  Count
Job #1   Success 33
Job #2   Failed  9
Job #2   Success 32
Job #2   Warning 5

Вот часть кода, который я пробовал.

$arrAllTasksBk = $taskListBk | Sort JobName | Select @{Name="JobName"; Expression = {$_.jobname}}, Status 
$arrAllTasksBk = $arrAllTasksBk | Group-object 'JobName', status |
 Select-Object @{n='JobName';e={$_.Group[0].'JobName'}}, @{n='Status';e={$_.Group[0].Status}}, Count

$arrAllTasksBk = $arrAllTasksBk | Sort JobName |  Select
            @{Name="JobName"; Expression = {$_.jobname}},
            @{Name="Total"; Expression = {$_.Count | Where {($_.status -match "Success") -or ($_.status -match "Warning")-or ($_.status -match "Failed")}}},
            @{Name="Failed"; Expression = {$_.Count | Where {$_.status -match"Failed"}}},
            @{Name="SuccessRate"; Expression = {$_.Count | Where {($_.status -match "Success") -or ($_.status -match "Warning")-or ($_.status -match "Failed")} / {$_.status -match "Success" }}}

1 Ответ

0 голосов
/ 06 ноября 2018

вот один из способов получить эти цифры. удалить или закомментировать элементы, которые вам не нужны, в конечном выводе ... [ ухмылка ]

# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
JobName, Status, Count
Job #1, Success, 33
Job #2, Failed,  9
Job #2, Success, 32
Job #2, Warning, 5
'@ | ConvertFrom-Csv

$GroupedInStuff = $InStuff |
    Group-Object -Property JobName

$Results = foreach ($GIS_Item in $GroupedInStuff)
    {
    $Success = [int]($GIS_Item.Group.Where({$_.Status -eq 'Success'}))[0].Count
    $Failed = [int]($GIS_Item.Group.Where({$_.Status -eq 'Failed'}))[0].Count
    $Warning = [int]($GIS_Item.Group.Where({$_.Status -eq 'Warning'}))[0].Count
    $Total = $Success + $Failed + $Warning
    [PSCustomObject]@{
        JobName = $GIS_Item.Name
        TotalCount = $Total
        Success = $Success
        Failed = $Failed
        # to remove the "Warning" items, comment out the next line
        Warning = $Warning
        # to get an int percent [70 instead of 69.57]
        #    use the following line
        # SuccessPct = [int]('{0:N0}' -f ($Success / $Total * 100))
        SuccessPct = [decimal]('{0:N2}' -f ($Success / $Total * 100))
        }
    }

$Results

вывод ...

JobName    : Job #1
TotalCount : 33
Success    : 33
Failed     : 0
Warning    : 0
SuccessPct : 100.00

JobName    : Job #2
TotalCount : 46
Success    : 32
Failed     : 9
Warning    : 5
SuccessPct : 69.57
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...