希望填充两列数组,并将其排序到第二列以显示。有人能帮我吗?
$scopelist = Get-DhcpServerv4Scope | sort name
write-host -foregroundcolor green "Aantal scopes : " $scopelist.count
$allscopes=@(85),@(2)
$teller=0
foreach ($scope in $scopelist)
{
#write-host $scope.name " : " (Get-DhcpServerv4Lease $scope.scopeid).count
$all += (Get-DhcpServerv4Lease $scope.scopeid).count
$allscopes += $scope.name,(Get-DhcpServerv4Lease $scope.scopeid).count
#$allscopes[$teller][0]=$scope.name
#$allscopes[$teller][1]=(Get-DhcpServerv4Lease $scope.scopeid).count
$teller++
}
write-host "Alle toestellen via dhcp : " $all
$allscopes
#$gesorteerd = $allscopes | sort-object @{Expression={$_[1]}; Ascending=$false}
#$gesorteerd
现在输出如下所示:
Tournai
19
Turnhout
40
Users Wired
149
Users Wireless
46
Verviers
41
Veurne
18
WAP
10
Waregem
42
Wavre
25
Wetteren
33
Wevelgem
46
Zaventem
23
Zelzate
69
Zottegem
18
Zwevegem
42
发布于 2017-06-15 17:13:44
你的数组排序很好。问题在于数组初始化和将成员添加到数组的行。这是:
$allscopes=@(85),@(2)
创建具有两个数组成员( {85}和{2} )的one-dimensional数组。然后这一行:
$allscopes += $scope.name,(Get-DhcpServerv4Lease $scope.scopeid).count
使用+=
运算符,该运算符随后将$scope.name
和count
添加到+=
数组中(这是该运算符的默认行为)。
要修复您的代码,请尝试如下:
# Empty array initialization
$allscopes = @()
...
# Notice the comma - means you're adding array as a member, not two members
$allscopes += ,($scope.name,(Get-DhcpServerv4Lease $scope.scopeid).count)
...
# Output every (x,y) member, joined with tab char
$allscopes | foreach {$_ -join "`t"}
https://stackoverflow.com/questions/44569837
复制相似问题