【发布时间】:2019-11-22 16:08:14
【问题描述】:
我正在尝试将总计行添加到数组 $response_table。
例如,该行应为 Totals = 56(数字)。
我试过$response_table += @{Name="Total"; Count=55};,它添加了一行垃圾数据。
你能帮忙看看如何添加这个吗?
代码如下
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$current_month = (Get-Date).month;
$current_year = (Get-Date).year;
$response_table=@();
$what_year=2019
$month_count=12
$current_month, $total_year, $current_date_interval;
$total_monthly=@(); # empty array, will be populated with values from the API
#$numbers += 5, 2, 3; # to add values to array
if ($what_year -eq $current_year)
{
$month_count = $current_month-1;
}
for ($current_month=1; $current_month -le $month_count; $current_month++)
{
$current_date_interval = -join($what_year, "-", $current_month);
$uri="https://data.police.uk/api/crimes-street/bicycle-theft?poly=53.950624,-1.059234:53.951301,-1.049181:53.947361,-1.043420:53.950333,-1.030671:53.952997,-1.016427:53.950189,-1.013653:53.929487,-1.042286:53.942512,-1.054948:53.941936,-1.057172:53.944481,-1.060155s8&date="+$current_date_interval;
$response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
$response_table += $response | Group month |Select -Property name,count
Write-Host $response_table;
$response | Group month |Select -Property name,count
$total_year += $response.count;
}
Write-Host ($response_table|Measure-object -Property count -sum)
$response_table += @{Name="Total"; Count=55};
# does not work
$response_table | export-csv "list.csv" -notypeinformation
#add-content "list.csv" "Total,$total_year"
Write-Host "Yearly total"$total_year;
【问题讨论】:
-
也许你应该试试
$response_table += [pscustomobject]@{Name="Total"; Count=55}。这样您就可以添加一个具有您定义的属性的自定义对象,而不仅仅是一个哈希表。可能有比使用+=更好的方法。这导致每次都构建整个数组并且效率低下。 -
@AdminOfThings 你最后一点的答案是使用
System.Collections.Generic.List[type]
标签: powershell