使用PowerShell生成具有列方式数据的CSV而不是行方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PowerShell生成具有列方式数据的CSV而不是行方式相关的知识,希望对你有一定的参考价值。
我正在尝试使用客户端数据创建多天的CSV文件。
问题是当我的循环运行时它将数据添加到行中,所以我每天有30行。
我想要的是每天数据到列,以便我只有一行30天列。
PowerShell代码:
$start_date = "2019-01-10" #YYYY-MM-DD format
$end_date = "2019-02-10" #YYYY-MM-DD format
$path = "C:UsersXXXXDownloads
eport"
$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 $pathUptimeStatus.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:UsersXXXXDownloads
eportUptimeStatus.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:UsersXXXXDownloads
eportfinalReport.csv -NoTypeInformation
问题:System.Object[]
被写入Excel工作表而不是值,因为它在打印时由多个值组成。
ClientNameValue : abc test xyz
答案
基于建议写下面的代码,它工作正常。谢谢@ansgar的帮助。
$path = "C:UsersxxxxDownloads
eport"
$Rows = Import-Csv $pathUptimeStatus.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 $pathfinalReportNew.csv -NoTypeInformation
以上是关于使用PowerShell生成具有列方式数据的CSV而不是行方式的主要内容,如果未能解决你的问题,请参考以下文章
PowerShell - 从 csv 文件读取数据,比较特定列中的数据并将结果写入新文件
PowerShell 和 CSV:阻止 CSV 将文本数据转换为科学记数法