所以我认为这对于 powershell 来说是一件简单的事情,但我似乎不知道如何使我的数组输出为列。下面是我的脚本。
$Table = @()
$Employees = Get-ADUser -SearchBase "OU=Emplyoees,DC=mydomain,DC=com" -Filter * -Properties * | ?{$_.telephoneNumber -ne $Null}
ForEach($User in $Employees){$Table += @($User.Name,$user.telephoneNumber)}
($Table)
输出看起来类似于:
用户1
电话号码1
用户2
电话号码2
我希望它看起来像这样:
姓名 电话号码
用户1 电话号码1
用户2 电话号码2
任何帮助都会很棒
最佳答案
试试这个:
ForEach($User in $Employees){$Table += ,@($User.Name,$user.telephoneNumber)} #NOTE THE COMMA HERE
$table | % { $_ -join '`t' } # the `t is a tab, but you can use whatever you want
评论后编辑:
如果只需要一个布局,您可以尝试:
$table | % { "{0,-20}{1,20}" -f $_[0],$_[1] } #the 2nd value in {} is the relative cursor position the sign left o right alignment
但为什么不只使用 $Employees
变量来显示结果:
$Employees = Get-ADUser -SearchBase "OU=Emplyoees,DC=mydomain,DC=com" -Filter * -Properties * |
?{$_.telephoneNumber -ne $Null} | select name, telephonenumber | ft
关于Powershell - 格式化锯齿状数组输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766374/