In Get-View Part 1: Introduction I introduced the advanced feature Get-View and a little bit about what it does. Before getting into the views and extension data topic I wanted to add a few thoughts as a segway into this post.
- Part 1: Introduction
- Part 2: Extension Data
- Part 3: Performance Impact
What is Get-View?
If I wasn’t very clear in the first post, Get-View is a method that allows PowerCLI users to return more information in a much faster way. The information returned is not formatted nicely like that of normal PowerCLI cmdlets like Get-VM, however like I said previously, this is a more advanced topic and allows access to more data than the average Joe may require. Using Get-View allows users the ability to work with the underlying API which can bring back many settings and property values from your virtual environment.
Why use get-view?
Similar to the previous paragraph, Get-View allows for faster retrieval of information, as well as information that is otherwise invisible to the user when running other PowerCLI cmdlets. Between its increased speed of data retrieval and the breadth of information available at the users fingertips, it becomes very useful when running reports or other queries on your virtual environment.
What ViewTypes can I use?
When using the get-view cmdlet like we did in the previous post, I used the command:
$VM = get-view –viewtype VirtualMachine –filter @{“Name”=”SDDC-VCAC-IAAS”}
This specified that I was looking for a virtual machine and filtered by the VM’s name. But what are all the ViewTypes I can choose from? Here they are:
- ComputeResource – Cluster view
- Datacenter – Datacenter view
- Datastore – Datastore view
- DistributedVirtualSwitch –vDS view
- Folder – Folder view
- HostSystem – ESX Host view
- Network – Virtual network view
- ResourcePool – Resource Pool view
- VirtualMachine – Virtual Machine view
Now that you know what the options are for ViewTypes, you can give them a shot. To return all objects of an object type simply enter:
get-view –viewtype <your choice of view object> and hit enter. You can always filter the results like we did for the VM above.
Are there any shortcuts?
Well, I’m glad you asked, because as it turns out, if you know what you are looking for and where it is located within the VIObject, there is a cheat that you can use to get information visibile in get-view, without using the get-view command. This is known as ExtensionData. ExtensionData was released back in the 4.x days of PowerCLI that allows you to access the VIObject properties without using get-view. let me show you how this works.
If we save our command as a variable, we can then refer back to it and dig into the object properties:
$VM1 = get-view –viewtype VirtualMachine –filter @{“Name”=”SDDC-VCAC-IAAS”} *Note that the filter hashtable works for any property of the object, not just the name
We can then reference the variable and see all of the properties of that object.
We can navigate deeper into these properties until we wind up at the desired property and value, as seen below:
$VM1.Summary.Config.VmPathName to show us where our VMX file is.
Now that we’ve done it the longer way using Get-View, lets do the same thing using extension data.
$VM = Get-VM “SDDC-VCAC-IAAS” (we are referencing the exact same VM as in the example above)
When we call $VM you’ll notice it looks different than when we called $VM1. That’s because we used the get-VM cmdlet this time and it formatted the data and only returned a subset of what is actually there.
Now let’s add “ExtensionData” to our object and see what happens:
$VM.ExtensionData.Summary.Config
We see the EXACT same information as we did in the first example solely by using the .ExtensionData.<desired location> on our VM object.
We can utilize this same functionality across the rest of the objects in our virtual infrastructure, allowing us to be even more specific in our reporting, logic and scripts that we run.
Performing Actions
Now that we’ve shown how you can search through and find advanced data leveraging get-view, we should talk about how you can perform actions for the specific objects while using get-view. You may notice that the cmdlets that work with get-vm do not work with get-view. That is because those cmdlets are made to work against a VIObject and when we pull back information with get-view, this data is not formatted into a VIObject, rather it’s part of a view object. This means you will receive an error when trying to use additional cmdlets with your view object.
We have two solutions:
- The first (and easiest because it allows us to work with what has been so common for us) is to use the ‘Get-VIObjectByVIView‘ cmdlet, which will then convert the view object to a VIObject usable by the rest of the PowerCLI cmdlets.
$vcsa = get-view -ViewType VirtualMachine -Filter @{“name”=”vCSA6”}
$vcsa | Get-VIObjectByVIView | Start-VM
- The second (which isn’t hard, it’s just not something most people have used) are the view methods. These can be found when using ‘get-view | get-member’:
This shows us a list of tasks that we can perform on this view object. Once you know what these tasks and methods are, it is very simple to use them as you can see below:
$vcsa = get-view -ViewType VirtualMachine -Filter @{“name”=”vCSA6”}
$vcsa.SuspendVM_Task()
Summary
To summarize, as you become more familiar with get-view you’ll learn there are some tricks and shortcuts to getting at the raw data you may need for the scripts or reports you are running. Knowing what ViewTypes are available and how to use ExtensionData will allow you to achieve cleaner, shorter, and more concise code.
Stay tuned for the final post on this get-view series (Part 3: Performance Impact)