2
votes

I have an arraylist of objects, but I want to output them to the console in a tabular format like Get-Process does for processes.

How can I output a List as a Table?

Format-Table just throws an error

$bulidsList | Format-Table -Auto -InputObject $bulidsList

"The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input"

3
We can't answer this without knowing what $buildsList is and how it was built. What is $buildsList.GetType().fullname. One of the ones in the example is spelled wrong. - Matt

3 Answers

4
votes

You mentioned in the question "An arraylist of objects". Here is a simple test to show you how those used with Format-Table:

$a = @()
$a += [pscustomobject]@{a = 1; b = 2}
$a += [pscustomobject]@{a = 3; b = 4}
$a += [pscustomobject]@{a = 5; b = 6}

$a | Format-Table

Here is the output:

a b
- -
1 2
3 4
5 6
2
votes

Why do you use both pipe and -InputObject? (and at least one has a typo I guess)

$bulidList | Format-Table
1
votes

I face this problem recently. I wanted to get list of Azure virtual machines and its IP addresses and display as table.

First solution did not work.

function Get-IpAddresses1{
   Write-Host "Private Ip Addresses"
   $privateIpAddress = New-Object System.Collections.ArrayList;
   Get-AzNetworkInterface | where {$_.VirtualMachine.Id -ne $null} | foreach {
      $privateIpAddress.Add(@{
          VirtualMachine = ($_.VirtualMachine.Id.Split('/') | select -Last 1)
          Ip = $_.IpConfigurations[0].PrivateIpAddress
      });
   }
   $privateIpAddress | Format-Table;
}

It gives result as follows.

Private Ip Addresses
0
1
2
3

Name                           Value
----                           -----
Ip                             10.0.1.5
VirtualMachine                 hemantcassandratest1
Ip                             10.0.1.6
VirtualMachine                 hemantcassandratest2
Ip                             10.0.1.7
VirtualMachine                 hemantcassandratest3
Ip                             10.0.0.4
VirtualMachine                 hemanttestvm

To get rid of indexes getting printed assign return value of Add method to a temp variable.

 function Get-IpAddresses1{
   Write-Host "Private Ip Addresses"
   $privateIpAddress = New-Object System.Collections.ArrayList;
   Get-AzNetworkInterface | where {$_.VirtualMachine.Id -ne $null} | foreach {
      $temp = $privateIpAddress.Add(@{
          VirtualMachine = ($_.VirtualMachine.Id.Split('/') | select -Last 1)
          Ip = $_.IpConfigurations[0].PrivateIpAddress
      });
   }
   $privateIpAddress | Format-Table;
}

I get result as follows now.

Private Ip Addresses

Name                           Value
----                           -----
Ip                             10.0.1.5
VirtualMachine                 hemantcassandratest1
Ip                             10.0.1.6
VirtualMachine                 hemantcassandratest2
Ip                             10.0.1.7
VirtualMachine                 hemantcassandratest3
Ip                             10.0.0.4
VirtualMachine                 hemanttestvm

To get the result as per my expectation I had to use another syntax for creating object.

function Get-IpAddresses{
  Write-Host "Private Ip Addresses"
  $privateIpAddress = New-Object System.Collections.ArrayList;
  Get-AzNetworkInterface | where {$_.VirtualMachine.Id -ne $null} | foreach {
      $temp = $privateIpAddress.Add((New-Object PSObject -Property @{ 
       VirtualMachine= ($_.VirtualMachine.Id.Split('/') | select -Last 1); 
       Ip=$_.IpConfigurations[0].PrivateIpAddress}));
  }
  $privateIpAddress | Format-Table;
}

Now the result is as follows.

Private Ip Addresses

Ip       VirtualMachine
--       --------------
10.0.1.5 hemantcassandratest1
10.0.1.6 hemantcassandratest2
10.0.1.7 hemantcassandratest3
10.0.0.4 hemanttestvm

Cheers, Hemant