VMware Cloud Community
LukePruen
Contributor
Contributor
Jump to solution

Object display format changed after Connect-VIServer

I have found the Connect-VIserver effects the output format of my custom objects.For example

Without Connect-VIServer

$output=@() $hosts = @("host1","host2") $VMs = @("VM1","VM2") $obj1 = New-Object psobject $obj1 | Add-Member NoteProperty -Name "Host" $hosts[0] $obj1 | Add-Member NoteProperty -Name "VM" $VMs[0] $obj2 = New-Object psobject $obj2 | Add-Member NoteProperty -Name "Host" $hosts[1] $obj2 | Add-Member NoteProperty -Name "VM" $VMs[1]      $output = $output + ($obj1, $obj2) $output


Provides this output:

1.PNG

With Connect-VIServer


Connect-VIServer localhost -user $VCuser -password $VCpass $output=@() $hosts = @("host1","host2") $VMs = @("VM1","VM2") $obj1 = New-Object psobject $obj1 | Add-Member NoteProperty -Name "Host" $hosts[0] $obj1 | Add-Member NoteProperty -Name "VM" $VMs[0] $obj2 = New-Object psobject $obj2 | Add-Member NoteProperty -Name "Host" $hosts[1] $obj2 | Add-Member NoteProperty -Name "VM" $VMs[1]      $output = $output + ($obj1, $obj2) $output

Output looks like

2.PNG

I cant find a way to correct this and was hoping someone may have a solution?

Thanks,

Luke

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

For the explanantion of what you are seeing you have to understand how PowerShell handles objects in the output stream.

A very good explanation can be found in How PowerShell Formatting and Outputting REALLY works.

In short:

  • the Connect-VIServer outputs a VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl object
  • there is no registered view (.ps1xml files) for this object type
  • so Out-Default looks at the number of properties on the 1st object in the stream
  • Connect-VIServer has more than 5 properties, so Out-Format goes for the Format-List
  • and all the following objects in the output stream will also be displayed with Format-List


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

For the explanantion of what you are seeing you have to understand how PowerShell handles objects in the output stream.

A very good explanation can be found in How PowerShell Formatting and Outputting REALLY works.

In short:

  • the Connect-VIServer outputs a VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl object
  • there is no registered view (.ps1xml files) for this object type
  • so Out-Default looks at the number of properties on the 1st object in the stream
  • Connect-VIServer has more than 5 properties, so Out-Format goes for the Format-List
  • and all the following objects in the output stream will also be displayed with Format-List


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LukePruen
Contributor
Contributor
Jump to solution

Thanks LucD, that help me get it fixed. I used Out-Default to return the formatting to default.

Connect-VIServer localhost -user $VCuser -password $VCpass
$output=@()
$hosts = @("host1","host2")
$VMs = @("VM1","VM2")
$obj1 = New-Object psobject
$obj1 | Add-Member NoteProperty -Name "Host" $hosts[0]
$obj1 | Add-Member NoteProperty -Name "SVA" $VMs[0]
$obj2 = New-Object psobject
$obj2 | Add-Member NoteProperty -Name "Host" $hosts[1]
$obj2 | Add-Member NoteProperty -Name "SVA" $VMs[1]
$output = $output + ($obj1, $obj2)
$output | Out-Default

0 Kudos