Why- Script output changes from table layout to list layout just by using Write-Host?

Julian Milano 1 Reputation point
2022-10-28T03:20:49.607+00:00

I have a script which grabs the VMTools version of each VM in a specified location. The output looks like this:

Name HW Version VMware Toos Status VMware Tools version VMware Tools version EXP


xrtAAPPVLB07 vmx-09 toolsOk 2147483647
NSTQLDDR vmx-08 toolsOk 12320 12.1.0
PuPQLDDR vmx-11 toolsOk 12320 12.1.0
DMTQLDDRSQL vmx-11 toolsOk 12320 12.1.0
SAPQLDDR vmx-08 toolsOk 12320 12.1.0
DMTQLDDR12 vmx-11 toolsOk 12320 12.1.0

Code:

Param (
[String]$Location = '*'
)

Since 'Get-View' does not work on locations such as folders and clusters,

get the list of VMs in the location.

$oVMs = Get-VM -Location $Location

$AllVMViews = @()
ForEach ($oVM in $oVMs)
{

Get the current VM name to use in 'Get-View'.

$VMName = $oVM.Name

Since 'Get-View' is not able to return the VMTools version in the ##.#.## format,

get it from 'VM-Guest'.

$VMToolsVer2 = $oVM | Get-VMGuest | Select ToolsVersion
$MyVMView = Get-View -ViewType VirtualMachine -Filter @{'Name'=$vmName} |
Select Name,
@{N="HW Version";E={$.Config.version}},
@{N='VMware Toos Status';E={$
.Guest.ToolsStatus}},
@{N="VMware Tools version";E={$_.Config.Tools.ToolsVersion}} ,
@{N="VMware Tools version EXP";E={$VMToolsVer2.ToolsVersion}}
$AllVMViews += $MyVMView
}

$AllVMViews | Export-CSV -Path 'VMTools_Report.CSV' -NoTypeInformation -UseCulture

$AllVMViews | Format-Table -AutoSize

If I change the last line to "Write-Host $AllVMViews | Format-Table -AutoSize", the output changes to this:

@{Name=xrtpuiPVLB07; HW Version=vmx-09; VMware Toos Status=toolsOk; VMware Tools version=2147483647; VMware Tools version EXP=} @{Name=NSTQLDDR; HW Version=vmx-08; VMware Toos Status=toolsOk; VMware Tools version=12320; VMware Tools version EXP=12
.1.0} @{Name=PuPQLDDR; HW Version=vmx-11; VMware Toos Status=toolsOk; VMware Tools version=12320; VMware Tools version EXP=12.1.0} @{Name=ttrQLDDRSQL; HW Version=vmx-11; VMware Toos Status=toolsOk; VMware Tools version=12320; VMware Tools version
EXP=12.1.0} @{Name=SAPQLDDR; HW Version=vmx-08; VMware Toos Status=toolsOk; VMware Tools version=12320; VMware Tools version EXP=12.1.0} @{Name=ttrQLDDR12; HW Version=vmx-11; VMware Toos Status=toolsOk; VMware Tools version=12320; VMware Tools ver
sion EXP=12.1.0}

Why?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,889 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 38,831 Reputation points
    2022-10-28T15:14:22.72+00:00

    Write-Host produces a "stringified" version of the object $AllVMViews.

    The variable $allVMViews is an array of PSCustomObjects. When you place it on the left side of a pipe the individual elements of the array (each of which is a PSCustomObject) are sent through the pipe, not the array itself. The Format-Table cmdlet (when used without an argument list) uses a default format to look for the members of a PSCustomObject with the type "NoteProperty", and ignores the rest.

    A string object typically won't have any properties that Format-Table finds in its default formatting instructions.

    0 comments No comments