在Powershell中将元素添加到数组

时间:2019-11-22 16:08:14

标签: powershell

我正在尝试将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;

2 个答案:

答案 0 :(得分:0)

正如AdminOfThings所指出的,您误将哈希表添加到现有的自定义对象数组 ( 1}}输出)。

要在PSv3 +中构造自定义对象,只需将Select-Object放在哈希表文字之前,在您的情况下,这意味着:

[pscustomobject]

现在您的$response_table += [pscustomobject] @{Name="Total"; Count=55} 命令应该可以正常工作了。

顺便说一句:在PowerShell 6.x之前,Export-Csv仅(有意义地)支持自定义对象作为输入,而不支持 hashtables 。 v7增加了对哈希表的支持。


通常避免使用Export-Csv附加到数组:

数组是固定大小的数据结构,因此,当您使用+=“附加”到数组时,PowerShell必须执行的操作是每次在幕后创建一个 new 数组。循环效率很低。

使用高效的就地可扩展数据结构(例如+=[System.Collections.ArrayList])是次要的事情,最简单,最快的方法是简单地使用 PowerShell 通过将整个循环作为表达式分配给变量来为您收集数组([System.Collections.Generic.List[object]])中的多个输出对象

[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} 类型约束确保[array]成为数组,即使循环恰好具有 1 迭代。

答案 1 :(得分:-1)

由于声誉限制,我无法发表评论:

请查看https://stackoverflow.com/a/14620446/12338776。 据我所知,您的问题在那里有答案,或者您的代码有另一个问题。