使用PowerShell生成具有列方式数据的CSV而不是行方式

问题描述 投票:0回答:1

我正在尝试使用客户端数据创建多天的CSV文件。

问题是当我的循环运行时它将数据添加到行中,所以我每天有30行。

enter image description here

我想要的是每天数据到列,以便我只有一行30天列。

enter image description here

PowerShell代码:

$start_date = "2019-01-10" #YYYY-MM-DD format
$end_date = "2019-02-10" #YYYY-MM-DD format
$path = "C:\Users\XXXX\Downloads\report"

$Original_startdate = $start_date
$Original_enddate = $start_date
$start_date_convert = [DateTime]::ParseExact($start_date, 'yyyy-MM-dd', $null)
$end_date_convert = [DateTime]::ParseExact($end_date, 'yyyy-MM-dd', $null)

#Generate the CSV file for each client
foreach ($element in $id) {
    #loop from start date to end date
    while ($start_date_convert -le $end_date_convert) {
        Write-Host "Original Start Date :" $Original_startdate
        Write-Host "Original End Date :" $Original_enddate

        $checkid = $element.pk
        $name = $element.name
        $clienturl = $element.msp_address
        $url3 = "https://uptime.com/api/v1/checks/282760/stats/?start_date=$Original_startdate&end_date=$Original_enddate&full_response_times=false&include_alerts=false&download=false&pdf=false"
        Write-Host "----------Getting Uptime percentage for client : $name with check id : $checkid !!!----------"
        $response = Invoke-RestMethod -Method GET -Uri $url3 -Headers $Headers -UseBasicParsing -ContentType application/json
        Write-Host "original uptime array values :" $response.statistics.uptime

        #Count days
        $days = $response.statistics.uptime.Count
        Write-Host "Uptime days :" $days

        #Sum all Uptime values for selected days
        $sum = 0
        $response.statistics.uptime | Foreach { $sum += $_}
        Write-Host "Sum uptime values :" $sum

        #Calculate Uptime percentage upto 2 decimal place
        $per  = ($sum * 100)/$days
        $uptime_per = [math]::Round($per,2)
        Write-Host "Uptime Percentage :" $uptime_per

        $results = @()
        $data = @{
            CheckID = $checkid
            ClientName = $name
            ClientURL = $clienturl
            UptimePercentage = $uptime_per
            Date = $Original_startdate
        }

        $results += New-Object PSObject -Property $data
        $results |
            Select-Object "CheckID","ClientName","ClientURL","UptimePercentage","Date" |
            Export-Csv -Path $path\UptimeStatus.csv -NoTypeInformation -Append

        $increment_startdate = $start_date_convert.AddDays(1)
        $start_date_convert = $increment_startdate
        Write-Host "New start date :" $start_date_convert
        $Original_startdate = $increment_startdate.ToString('yyyy-MM-dd')
        $Original_enddate = $Original_startdate
        Write-Host "Original Start Date after Increment :" $Original_startdate
        Write-Host "Original End Date after Increment :" $Original_enddate
    }
}

基于建议写下面的代码,当$ ClientNameValue和$ ClientUrlValue中只有一个值获取时它工作正常。

$Rows = Import-Csv C:\Users\XXXX\Downloads\report\UptimeStatus.csv
$DateValue       = $Rows | Select-Object -ExpandProperty Date | Sort -Unique
$ClientNameValue = $Rows | Select-Object -ExpandProperty ClientName | Sort -Unique
$ClientUrlValue  = $Rows | Select-Object -ExpandProperty ClientURL | Sort -Unique
Write-Host "ClientNameValue :" $ClientNameValue
Write-Host "ClientUrlValue :" $ClientUrlValue
$ConsolidatedRows = $Rows | Group-Object CheckID | ForEach-Object {
    $NewRowProperties = @{
        CheckID = $_.Name
        ClientName = $ClientNameValue
        ClientURL = $ClientUrlValue
    }
    foreach ($Row in $_.Group) {
        $NewRowProperties.Add($Row.Date, $Row.UptimePercentage)
    }
    New-Object PSObject -Property $NewRowProperties
}
$ConsolidatedRows |
    Select-Object @("CheckID";"ClientName";"ClientURL";$DateValue) |
    Export-Csv C:\Users\XXXX\Downloads\report\finalReport.csv -NoTypeInformation

问题:System.Object[]被写入Excel工作表而不是值,因为它在打印时由多个值组成。

ClientNameValue : abc test xyz

enter image description here

powershell csv
1个回答
0
投票

基于建议写下面的代码,它工作正常。谢谢@ansgar的帮助。

$path = "C:\Users\xxxx\Downloads\report"

$Rows = Import-Csv $path\UptimeStatus.csv

$DateValue = $Rows |Select-Object -ExpandProperty Date |Sort -Unique

$ConsolidatedRows = $Rows |Group-Object ClientURL |ForEach-Object
{
    $NewRowProperties = 
    @{ 
        CheckID = $_.Group | Select-Object -Expand CheckID -First 1
        ClientName = $_.Group | Select-Object -Expand ClientName -First 1
        ClientURL = $_.Name
    }

    foreach($Row in $_.Group)
    {
        $NewRowProperties.Add($Row.Date,$Row.UptimePercentage)
    }

    New-Object psobject -Property $NewRowProperties
}

$ConsolidatedRows |Select-Object @("CheckID";"ClientName";"ClientURL";$DateValue) |Export-Csv $path\finalReportNew.csv -NoTypeInformation
© www.soinside.com 2019 - 2024. All rights reserved.