woef
woef

Reputation: 57

powershell - working with array, 2 column

Want to fill two column array, and sort it on second column to display. Can someone assist me ?

    $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

now is output something like this :

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 

Upvotes: 0

Views: 2725

Answers (1)

Mikhail Tumashenko
Mikhail Tumashenko

Reputation: 1723

Your array sorting is fine. The problem is with array initialization and the line where you're adding members to the array. This:

$allscopes=@(85),@(2)

creates one-dimensional array with 2 array members, {85} and {2}. Then this line:

$allscopes += $scope.name,(Get-DhcpServerv4Lease $scope.scopeid).count

uses += operator, which subsequently adds $scope.name and count to the one-dimensional array (it's the default behaviour for this operator).

To fix your code try this:

# 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"} 

Upvotes: 1

Related Questions