我试图用客户数据创建一个CSV文件多天。
问题是当我的循环正在运行时,它将数据添加到行中,因此我每天有30行。
我想要的是每天将数据放入列中,这样我只有一行包含30天的列。
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
答案 0 :(得分: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