My goal is to write a function that supports writing any array as a table, where the args given are the array, and the number of columns wanted in the table.
I have the following code...
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
This will print the following output...
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
Instead, I want the format printout to be like the following...
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
Any idea how to do this?
Format-Wide
does basically what you describe already.
All you need to do is construct an object with a single property for each string, and then refer to that property name with 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
}