我正在尝试将Totals行添加到数组$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;
正如AdminOfThings指出的那样,您错误地将哈希表添加到现有的自定义对象数组(Select-Object
输出)中。
要在PSv3 +中构造自定义对象,只需将[pscustomobject]
其放在哈希表文字之前,这对您而言意味着:
$response_table += [pscustomobject] @{Name="Total"; Count=55}
现在您的Export-Csv
命令应该可以正常工作了。
顺便说一句:在PowerShell 6.x之前,Export-Csv
仅(有意义地)支持将自定义对象作为输入,不支持hashtables作为输入。v7添加了对哈希表的支持。
通常避免使用+=
追加到数组:
数组是固定大小的数据结构,因此,当您“附加到”数组时,PowerShell必须执行的操作+=
是每次在幕后创建一个新数组,这在循环中效率很低。
在使用高效的就地可扩展数据结构(例如[System.Collections.ArrayList]
或[System.Collections.Generic.List[object]]
)时,最简单,最快的方法是通过将整个循环分配为表达式,让PowerShell轻松地[object[]]
为您收集数组()中的多个输出对象。变量:
[array] $response_table = 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
# *Output* the result of this pipeline, and PowerShell will
# collect all outputs for you
$response | Group month | Select -Property name,count
$total_year += $response.count;
}
# It's fine to use += to add the total, because you're only using it *once*.
$response_table += [pscustomobject] @{Name="Total"; Count=55}
注意:即使循环恰好只有1次迭代,[array]
类型约束也可确保该$response_table
数组成为数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句