我的目标是编写一个支持将任何数组写为表的函数,其中给出的args是数组,以及表中所需的列数。
我有以下代码......
Function PrintArrayAsTable
{
Param ([String[]]$array ,[Int]$numOfItemsPerRow)
$elementCounter = 1
[String[]]$row = @()
[String[]]$tableArray = @()
ForEach ($element in $array)
{
$row += $element
if ($elementCounter % $numOfItemsPerRow -eq 0)
{
$tableArray += ,($row)
[String[]]$row = @()
}
$elementCounter++
}
if ($row)
{
$tableArray += ,($row)
[String[]]$row = @()
}
$tableArray | Format-Table
}
[String[]]$array = @('SamAccountName', 'msRTCSIP-UserEnabled', 'msRTCSIP-OptionFlags', 'msRTCSIP-PrimaryUserAddress', 'msRTCSIP-PrimaryHomeServer',
'mail', 'msExchMasterAccountSid', 'homeMDB', 'proxyaddresses', 'legacyExchangeDN',
'lastLogonTimestamp', 'logonCount', 'lastLogoff', 'lastLogon', 'pwdLastSet', 'userAccountControl', 'whenCreated', 'whenChanged', 'accountExpires',
'sn', 'givenName', 'displayName', 'distinguishedName', 'initials', 'l', 'st', 'street', 'title', 'description', 'postalCode', 'physicalDeliveryOfficeName', 'telephoneNumber', 'facsimileTelephoneNumber', 'info', 'memberOf', 'co', 'department', 'company', 'streetAddress', 'employeeNumber', 'employeeType', 'objectGUID', 'employeeID', 'homeDirectory', 'homeDrive', 'scriptPath', 'objectSid', 'userPrincipalName', 'url', 'msDS-SourceObjectDN', 'manager', 'extensionattribute8')
PrintArrayAsTable $array 5
这将打印以下输出...
SamAccountName msRTCSIP-UserEnabled msRTCSIP-OptionFlags msRTCSIP-PrimaryUserAddress msRTCSIP-PrimaryHomeServer
mail msExchMasterAccountSid homeMDB proxyaddresses legacyExchangeDN
lastLogonTimestamp logonCount lastLogoff lastLogon pwdLastSet
userAccountControl whenCreated whenChanged accountExpires sn
givenName displayName distinguishedName initials l
st street title description postalCode
physicalDeliveryOfficeName telephoneNumber facsimileTelephoneNumber info memberOf
co department company streetAddress employeeNumber
employeeType objectGUID employeeID homeDirectory homeDrive
scriptPath objectSid userPrincipalName url msDS-SourceObjectDN
manager extensionattribute8
相反,我希望格式打印输出如下...
SamAccountName msRTCSIP-UserEnabled msRTCSIP-OptionFlags msRTCSIP-PrimaryUserAddress msRTCSIP-PrimaryHomeServer
mail msExchMasterAccountSid homeMDB proxyaddresses legacyExchangeDN
lastLogonTimestamp logonCount lastLogoff lastLogon pwdLastSet
userAccountControl whenCreated whenChanged accountExpires sn
givenName displayName distinguishedName initials l
st street title description postalCode
physicalDeliveryOfficeName telephoneNumber facsimileTelephoneNumber info memberOf
co department company streetAddress employeeNumber
employeeType objectGUID employeeID homeDirectory homeDrive
scriptPath objectSid userPrincipalName url msDS-SourceObjectDN
manager extensionattribute8
知道怎么做吗?
答案 0 :(得分:4)
Format-Wide
基本上就是你所描述的。
您需要做的就是为每个字符串构造一个具有单个属性的对象,然后使用Format-Wide -Property
引用该属性名称:
function Print-Grid
{
param(
[Parameter(Mandatory,ValueFromPipeline,Position=0)]
[string[]]$Array,
[Parameter(Position=1)]
[ValidateRange(1,24)]
[int]$ColumnCount
)
$GridSplat = @{
InputObject = $Array|ForEach-Object {
New-Object psobject -Property @{'Value' = $_}
}
Property = 'Value'
}
if(-not $PSBoundParameters.ContainsKey('ColumnCount'))
{
$GridSplat['AutoSize'] = $true
}
else
{
$GridSplat['Column'] = $ColumnCount
}
Format-Wide @GridSplat
}
答案 1 :(得分:0)
看起来缺少的是让你的数据行变成整齐,等宽的列......
我尝试计算出你需要列的最大宽度(比如,数组中最长字符串的长度加1)。你可以这样做:
$width = ($array | %{$_.Length} | Measure-Object -Maximum).Maximum + 1
然后你可以将每个元素填充到这个宽度:
$row += $element.PadRight($width)
答案 2 :(得分:0)
我在尝试做类似的事情时发现了这篇文章,并且似乎找到了一种不同的方法;
$arrayName | select-object -Property @{Name = 'Col1 name'; Expression = { $_[1]}},
@{Name = 'Col2 name'; Expression = { $_[2]}},
@{Name = 'Col3 name'; Expression = { $_[3]}},
@{Name = 'Col4 name'; Expression = { $_[4]}}
答案 3 :(得分:0)
如果你想控制列格式,这里是完整的答案;
$s1errorcode_list | select-object -Property @{Name = 'Col1 name'; Expression = { $_[1]}},
@{Name = 'Col2 name'; Expression = { $_[2]}},
@{Name = 'Col3 name'; Expression = { $_[3]}},
@{Name = 'Col4 name'; Expression = { $_[4]}} |
format-table -Property @{ Expression = 'Col1 name'; width=6 },
@{ Expression = 'Col2 name'; width=84 },
@{ Expression = 'Col3 name'; width=80 },
@{ Expression = 'Col4 name'}