VMware Cloud Community
mdreynolds
Contributor
Contributor
Jump to solution

PowerCLI Output Format

Why is the output format different when I run the same exact command in PowerShell interactively and from a script? ...

When in PowerShell interactively and I execute command --> Get-VM | Sort | Select Name,PowerState, NumCpu, MemoryMB

Output is single line per VM result (which is what I want)...


Name PowerState NumCpu MemoryMB
---- ---------- ------ --------
EDLT331-1 PoweredOff 2 1024
EDLT331-2 PoweredOff 2 1024
EDLT331-3 PoweredOff 2 1024
EDLT331-4 PoweredOff 2 1024
EDLT331-5 PoweredOff 2 1024
EDLT331-6 PoweredOff 2 1024
EDLT411-1 PoweredOff 2 1024
EDLT411-2 PoweredOff 2 1024
EDLT411-3 PoweredOff 2 1024

...

But when I run the same exact command from a script VMList.ps1 the format is multiple lines per VM result...

Name Port User
---- ---- ----

Name : EDLT331-1
PowerState : PoweredOff
NumCpu : 2
MemoryMB : 1024


Name : EDLT331-2
PowerState : PoweredOff
NumCpu : 2
MemoryMB : 1024


Name : EDLT331-3
PowerState : PoweredOff
NumCpu : 2
MemoryMB : 1024


Name : EDLT331-4
PowerState : PoweredOff
NumCpu : 2
MemoryMB : 1024

...

Please advise is there an output format environment variable that needs to be set or something?

Labels (2)
0 Kudos
1 Solution

Accepted Solutions
CNorris_Const
Enthusiast
Enthusiast
Jump to solution

Try this:

Connect-VIServer -Server xxxvc01.xyz.com -User user@xyz.local
Get-VM | Sort |Select Name,PowerState, NumCpu, MemoryMB | Format-Table *
Disconnect-VIServer -Confirm:$false

View solution in original post

0 Kudos
5 Replies
mdreynolds
Contributor
Contributor
Jump to solution

Only difference is the VMList.ps1 does a login and logout.  Here is the contents of VMList.ps1...

Connect-VIServer -Server xxxvc01.xyz.com -User user@xyz.local
Get-VM | Sort |Select Name,PowerState, NumCpu, MemoryMB
Disconnect-VIServer -Confirm:$false

0 Kudos
CNorris_Const
Enthusiast
Enthusiast
Jump to solution

Try this:

Connect-VIServer -Server xxxvc01.xyz.com -User user@xyz.local
Get-VM | Sort |Select Name,PowerState, NumCpu, MemoryMB | Format-Table *
Disconnect-VIServer -Confirm:$false
0 Kudos
mdreynolds
Contributor
Contributor
Jump to solution

Thanks the | Format-Table does work.  Is there a variable to set it as the default

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, there is no variable to set.
The PowerShell output engine has some simple rules.
- if an object has 4 or fewer properties, the output is shown in tabular format

- for 5 and more properties, the output is shown in list format

The number of properties that are shown for a specific object are normally defined in .ps1xml files.
For a VirtualMachine object, the default view are 4 properties (Name,PowerState,Num CPUs,MemeoryGB), hence they are displayed in tabular format (rule 1 above).

In your script, the first object in the pipeline comes from Connect-VIServer, the following from the Get-VM
The PS output engine uses the Connect-VIServer object to define the format of the output.
See the "Name Port User" line in tabular format.
Then follow the VM objects, which are different, and the PS engine falls back on the list format.

Which are in fact 2 other rules of the PS output engine.

- the first object in the pipeline determines which properties are shown
- if another object is found in the pipeline, fall back in list format


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

0 Kudos
CNorris_Const
Enthusiast
Enthusiast
Jump to solution

The legend himself. Thanks LucD for clarifying!

0 Kudos