0

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 
1
  • You could use a dictionary instead of array and sort it like $dictionary.GetEnumerator() | sort -Property value Commented Jun 15, 2017 at 14:53

1 Answer 1

1

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"} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.