首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >powershell -使用数组,2列

powershell -使用数组,2列
EN

Stack Overflow用户
提问于 2017-06-15 14:13:42
回答 1查看 2K关注 0票数 0

希望填充两列数组,并将其排序到第二列以显示。有人能帮我吗?

代码语言:javascript
运行
复制
    $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

现在输出如下所示:

代码语言:javascript
运行
复制
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 
EN

回答 1

Stack Overflow用户

发布于 2017-06-15 17:13:44

你的数组排序很好。问题在于数组初始化和将成员添加到数组的行。这是:

代码语言:javascript
运行
复制
$allscopes=@(85),@(2)

创建具有两个数组成员( {85}和{2} )的one-dimensional数组。然后这一行:

代码语言:javascript
运行
复制
$allscopes += $scope.name,(Get-DhcpServerv4Lease $scope.scopeid).count

使用+=运算符,该运算符随后将$scope.namecount添加到+=数组中(这是该运算符的默认行为)。

要修复您的代码,请尝试如下:

代码语言:javascript
运行
复制
# 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"} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44569837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档