VMware Cloud Community
chh_sys
Contributor
Contributor
Jump to solution

Strange output differences of get-vm

Hi,

While writing a script today I came across a strange difference in the output of get-vm.

when running from the CLI I see the following:

get-vm fw-int


Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
fw-int               PoweredOff 1        256

running from inside a script (connect-viserver; get-vm, disconnect-viserver) the output looks like that:

PowerState              : PoweredOff
Version                 : v7
Description             :
Notes                   :
Guest                   : fw-int:
NumCpu                  : 1
MemoryMB                : 256
HardDisks               : {Festplatte 1}
NetworkAdapters         : {Netzwerkadapter 1, Netzwerkadapter 2}
UsbDevices              : {}
CDDrives                : {CD-/DVD-Laufwerk 1}
FloppyDrives            : {Diskettenlaufwerk 1}
Host                    : esx01.xxx
HostId                  : HostSystem-host-29
VMHostId                : HostSystem-host-29
VMHost                  : esx01.xxx
VApp                    :
FolderId                : Folder-group-v217
Folder                  : MgmtSystems
ResourcePoolId          : ResourcePool-resgroup-28
ResourcePool            : Resources
PersistentId            : 521793ce-fdda-5652-7b47-ecc84ee3067e
UsedSpaceGB             : 0,6274801
ProvisionedSpaceGB      : 2,250527
DatastoreIdList         : {Datastore-datastore-9579}
HARestartPriority       : ClusterRestartPriority
HAIsolationResponse     : AsSpecifiedByCluster
DrsAutomationLevel      : AsSpecifiedByCluster
VMSwapfilePolicy        : Inherit
VMResourceConfiguration : CpuShares:Normal/1000 MemShares:Normal/2560
CustomFields            : {[Logged in User, ], [Transfer Server Status, ], [VM Lock Status, ], [View Local Mode Status, ]}
ExtensionData           : VMware.Vim.VirtualMachine
Id                      : VirtualMachine-vm-6052
Name                    : fw-int
Uid                     : /VIServer=@vcenter.xxx:443/VirtualMachine=VirtualMachine-vm-6052/

If I use get-vm|format-table in the script I get an error

out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely

caused by a user-specified "format-table" command which is conflicting with the default formatting.
    + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
    + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand

If I remove the connect-viserver from the script and run it after  connecting manually I get the same output as if I am running the command  from the cli.

How are these differences possible?

The PowerCLI version used is 4.1 U1 build 332441. The vSphere environmet is 4.1 (no Update 1 yet).

Thanks in advance

Christian

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Christian,

this happens when your script outputs two different record types. In this case the output of the Connect-VIserver and the Get-VM cmdlets. You can prevent this behavior in your case by changing the Connect-VIServer line into $VIServer = ConnectVIserver.

Regards, Robert

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

View solution in original post

0 Kudos
3 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Christian,

this happens when your script outputs two different record types. In this case the output of the Connect-VIserver and the Get-VM cmdlets. You can prevent this behavior in your case by changing the Connect-VIServer line into $VIServer = ConnectVIserver.

Regards, Robert

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

Hi Robert,

thanks for the quick answer.

This really solved it.

Although I don't really understand why this influences the output of get-vm.

Probably have to read and think a bit more .

Christian

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Christian,

in a PowerShell/PowerCLI script you should always try to output only a single record type. Because if you output two or more record types, the second and consequent record types will be output in list format. You can try this yourself by making four different scripts. The first one, Test1.ps1, contains:

Get-VM


If you run this script, you will get the output in a table format. That is because this script returns only a single record type.

The second script, Test2.ps1, contains:

Get-VMHost

Get-VM

The output of this script will be the hosts in table format and the VM's in list format.

The third script, Test3.ps1, contains:

Get-VM

Get-VMHost

The output of this script will be the VM's in table format and the hosts in list format.

To prevent this behaviour you can assign the output of a cmdlet to a variable. Like you did with $VIserver = Connect-VIserver. In this case the result of the cmdlet is not written to the output stream of the script.

We will change the second script, Test2.ps1, into Test4.ps1:

$VMHost = Get-VMHost
Get-VM


This script shows only the virtual machines in a table format.

I hope this explains the behaviour in more detail.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos