VMware Cloud Community
tendonsie
Contributor
Contributor
Jump to solution

Get-VM to Get-View -viewtype VirtualMachine

We want to replace our Get-VM function with Get-View -viewtype VirtualMachine because it's a faster method.

But for some reason, the output is not the same.

You would expect that both return the same VirtualMachine object.

Output with Get-VM:

Name, PowerState, NUM CPUs, MemoryGB

Output with Get-View -viewtype VirtualMachine:

Capability, Config, Layout, LAyoutEx, Storage,... all VMware.Vim.VirtualMachine object.

Our used methods:

    $Hosts = Get-VMsByLocation -Location $Location

    $VMs = @()

    $Hosts | ForEach{

A)        $VMs += Get-VM $_

B)        $VMs += Get-View -ViewType VirtualMachine  –Filter @{“Name”=$_}    

    }

https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/Get-VM.html

https://communities.vmware.com/thread/474763

https://www.vmware.com/support/developer/converter-sdk/conv50_apireference/vim.VirtualMachine.html

https://pubs.vmware.com/vsphere-51/index.jsp#com.vmware.powercli.cmdletref.doc/VirtualMachine.html

1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, tendonsie-

Yes, you are correct:  if you change the object type that you are using, then subsequent calls what used <old object type> will likely not work with <new object type>.

Though, another part of the power of Get-View:  you have direct access to a representation of the "full" vSphere object -- the VirtualMachine .NET View object, in this case.  So, you do no need another call to get the CpuReservationMhz -- you can get it directly from the VirtualMachine object that you will have already retrieved.  An example of include such property in the output (building on the example that I first gave):

Get-View -ViewType VirtualMachine -Property Name,Runtime.PowerState,Config.Hardware,Config.CpuAllocation.Reservation | Select-Object Name,
    @{n
="PowerState"; e={$_.Runtime.PowerState}},
    @{n
="NumCPU"; e={$_.Config.Hardware.NumCPU}},
    @{n
="MemoryGB"; e={$_.Config.Hardware.MemoryMB / 1KB}},
    @{n
="CpuReservationMhz"; e={$_.Config.CpuAllocation.Reservation}}

You see the additional calculated property, CpuReservationMhz, here.  Now you need not even call the Get-VMResourceConfiguration cmdlet.

So, the work comes to be discovering what properties of the .NET View object that you have retrieved.  And, this work is supported by the vSphere API documentation, a link to which is available at VMware vSphere Web Services SDK Documentation.  How's that do?

View solution in original post

4 Replies
jpsider
Expert
Expert
Jump to solution

‌hhave you tried .name?

B)        $VMs += Get-View -ViewType VirtualMachine  –Filter @{“Name”=$_.Name}

sorry not not at a computer to test.

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, tendonsie-

The properties are different because the two cmdlets are returning different object types.  That is, Get-VM returns a VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl object, whereas Get-View -ViewType VirtualMachine returns a VMware.Vim.VirtualMachine object.

Brian Graf did a series on Get-View this year, and those are a good place to start for background info:  Part 1, Part 2, and Part 3.

But, particularly for your question:  you can still get the properties that you desire from the VirtualMachine object (returned by Get-View) -- you just need to dig a little to get them.  For example, to use Get-View and then return those four default properties that you noted that Get-VM returns, it would be something like:

Get-View -ViewType VirtualMachine -Property Name,Runtime.PowerState,Config.Hardware | Select-Object
    Name,
    @{n
="PowerState"; e={$_.Runtime.PowerState}},
    @{n
="NumCPU"; e={$_.Config.Hardware.NumCPU}},
    @{n
="MemoryGB"; e={$_.Config.Hardware.MemoryMB / 1KB}}

More typing, obviously, but as you mentioned, Get-View can be far faster than using other PowerCLI cmdlets.  So, you just need to access the given properties to return the information that you desire about the VirtualMachine objects.  That help?

tendonsie
Contributor
Contributor
Jump to solution

Thanks mattboren for the answer. I'm a step closer.

If I'm right, your solution will not work if you need to pipe that output to another function because it's not the same VirtualMachine object like before.

Example:

Get-VMResourceConfiguration will expact that your provde a VirtualMachine object that you got from Get-VM.

vSphere 6.0 Documentation Center

Before I could to:

$VMs | foreach {

...

    $myVar = $($_ | Get-VMResourceConfiguration).CpuReservationMhz

...

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, tendonsie-

Yes, you are correct:  if you change the object type that you are using, then subsequent calls what used <old object type> will likely not work with <new object type>.

Though, another part of the power of Get-View:  you have direct access to a representation of the "full" vSphere object -- the VirtualMachine .NET View object, in this case.  So, you do no need another call to get the CpuReservationMhz -- you can get it directly from the VirtualMachine object that you will have already retrieved.  An example of include such property in the output (building on the example that I first gave):

Get-View -ViewType VirtualMachine -Property Name,Runtime.PowerState,Config.Hardware,Config.CpuAllocation.Reservation | Select-Object Name,
    @{n
="PowerState"; e={$_.Runtime.PowerState}},
    @{n
="NumCPU"; e={$_.Config.Hardware.NumCPU}},
    @{n
="MemoryGB"; e={$_.Config.Hardware.MemoryMB / 1KB}},
    @{n
="CpuReservationMhz"; e={$_.Config.CpuAllocation.Reservation}}

You see the additional calculated property, CpuReservationMhz, here.  Now you need not even call the Get-VMResourceConfiguration cmdlet.

So, the work comes to be discovering what properties of the .NET View object that you have retrieved.  And, this work is supported by the vSphere API documentation, a link to which is available at VMware vSphere Web Services SDK Documentation.  How's that do?