VMware Cloud Community
clmiller24x7
Contributor
Contributor
Jump to solution

Get-VM will not display summary when invoked from powershell script?

I want a simple Windows10 powershell script to start VMs then show the VMs that are powered on.  Like this:

Connect-VIServer -Server esx-101.mylab.net -User root -Password mypassword

Start-VM ServerVM1

Start-VM ServerVM2

Get-VM

Disconnect-VIServer -Confirm:$false

This works, however the output of the Get-VM command is verbose and detailed. If I don't disconnect from the host in the script and then type the exact same "Get-VM" syntax in the open powershell, then I get a summary of the VMs (one line per VM).  Why would the command give verbose output only when run in a powershell script?  The sound you hear is me banging my head.....

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The reason you are seeing what you are seeing is due to the reason the 'formatter' works in PowerShell.

There are some rules when the formatter is dealing with multiple, non-primitive objects in the pipeline.


In this case, the first non-primitive object is the result from your Connect-VIServer, for which there is a type declaration in PowerCLI.

This results in a Format-Custom type of format, which will lock in all subsequent non-primitive objects in Format-List (and not the Format-Table you get when you just do Get-VM).

Instead of hiding output, you can 'reset' the formatter, by piping each object  to the Out-Default cmdlet.

This will force the formatter to handle each non-primitive object as if it is the first one.

Connect-VIServer -Server esx-101.mylab.net -User root -Password mypassword | Out-Default

Start-VM ServerVM1 | Out-Default

Start-VM ServerVM2 | Out-Default

Get-VM | Out-Default

Disconnect-VIServer -Confirm:$false | Out-Default


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

View solution in original post

2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The output you get from a command in a script depends on the output from the previous commands. You can pipe the output from the commands you don't want to see to | Out-Null.

Connect-VIServer -Server esx-101.mylab.net -User root -Password mypassword | Out-Null

Start-VM ServerVM1 | Out-Null

Start-VM ServerVM2 | Out-Null

Get-VM

Disconnect-VIServer -Confirm:$false | Out-Null

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

The reason you are seeing what you are seeing is due to the reason the 'formatter' works in PowerShell.

There are some rules when the formatter is dealing with multiple, non-primitive objects in the pipeline.


In this case, the first non-primitive object is the result from your Connect-VIServer, for which there is a type declaration in PowerCLI.

This results in a Format-Custom type of format, which will lock in all subsequent non-primitive objects in Format-List (and not the Format-Table you get when you just do Get-VM).

Instead of hiding output, you can 'reset' the formatter, by piping each object  to the Out-Default cmdlet.

This will force the formatter to handle each non-primitive object as if it is the first one.

Connect-VIServer -Server esx-101.mylab.net -User root -Password mypassword | Out-Default

Start-VM ServerVM1 | Out-Default

Start-VM ServerVM2 | Out-Default

Get-VM | Out-Default

Disconnect-VIServer -Confirm:$false | Out-Default


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