Add Snapshot Information to the VI Client using Powershell

Although I spend quite some time in the Powershell Command Line Interface, the main tool for managing the Virtual Infrastructure remains the VI Client. So wouldn’t it be great if we could somehow show the results of our Powershell VI Toolkit scripts inside the VI Client?
Well, we can! Let’s take a closer look at Custom Fields / Custom Attributes.
If you select either a VMHost (ESX Server) or a VM in the VI Client and open the Summary tab, you will see the Annotations section in the bottom left. When you click Edit, I’m sure you have used the Notes section to enter Descriptions. But have you ever used the Attributes section? Here you can manually add and remove custom attributes and their values. Go ahead and create one. Then select a cluster or datacenter and click the Hosts or Virtual Machines tab. You will notice you can display your custom attribute in this table view, just like all the other properties of your VMs / Hosts. Pretty sweet!
But we don’t want to add and update those fields manually, now do we? Of course not, we’ve got Powershell! Let’s see how we can automate this.
We start by connecting to the Custom Fields Manager:

$VCServerName = “MYVCSERVER”
$VC = Connect-VIServer $VCServerName
$SI = Get-View ServiceInstance
$CFM = Get-View $SI.Content.CustomFieldsManager

Take a look at the $CFM.Field property. It contains an array of available fields. Next step: add our own custom field.

# Variables
$CustomFieldName = “Snapshots”
$ManagedObjectType = “VirtualMachine”
# Check if the custom field already exists
$myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName}
If (!$myCustomField)
{
# Create Custom Field
$FieldCopy = $CFM.Field[0]
$CFM.AddCustomFieldDef($CustomFieldName, $ManagedObjectType, $FieldCopy.FieldDefPrivileges, $FieldCopy.FieldInstancePrivileges)
}

Final step: Fill the custom field with some relevant information:

# Fill Custom Fields
$VMs = Get-VM
$VMViews = $VMs | Get-View
ForEach ($VMView in $VMViews)
{
$SnapshotCount = ($VMView.Snapshot.RootSnapshotList | Measure-Object).Count
If ($SnapShotCount)
{
$VMView.setCustomValue($CustomFieldName,$SnapShotCount)
}
}

Now take a look at the VI Client:
snapshots
More examples coming soon!
Hugo

PS: Don’t forget to schedule the script to run at an interval, so the values are kept up-to-date!

ever wondered where did the sport of basketball originateHaven actually tried it on my skin but I would use a cosmetic Cheap nfl jerseys grade latex for that part.To cheap football jerseys lay a beard, paint on a couple of thin layers of latex over the whole area and allow to dry. The Packers lost three of their major players this season including Rodgers, who missed seven full games but was able to come back last weekend to defeat the Bears along with Randall Cobb and Jermichael Finley. Later, we’ll conclude by examining whether this decline is sufficient to initiate a position, or if it is not, then I’ll suggest watchlist price targets based on a target rate of return. Cheap ray bans The latest is a movie from CBS Films that is based on American Assassin, from the best selling series of action thrillers by Vince Flynn. Rockefeller just because a Scottish man shouts at them and gives them a new menu their cooks can’t even read. Extend upward until your right leg becomes straight again and you stand firm with both feet on top of the chair.. But there’s no connection in my mind to the two incidents,” he said during an interview on ESPN’s Mike Mike morning show.. Despite its growth prospects, the shares trade at a significant discount to the overall market multiple. Coupled with the $750 million remaining from the previous buyback fund, Travelers has $5.75 billion dedicated to repurchasing its stock. We continue to make progress in expanding our relationships with key partners particularly Lenovo. The same goes for an executive summary, which is perhaps the most important aspect of a business plan.. More importantly, I never “focus” on any one aspect two days in a row. SB:Probably until I’m dead. wholesale football jerseys If you compare fees of DPP with your weekend dinner, you will certainly find the sports predictions more affordable. It’s about the struggle between science and religion, and settles on a kind of “Can’t we kiss and make up” message that couldn’t be www.buyoakleysunglasses.com made http://www.bestfakesales.com/sale/ clearer than when Jodie Foster’s scientist character, Ellie, has bridge building sex with Matthew McConaughey’s theologian character, Palmer. The Giants are at 20 7 and they are back in the game!”. With a mean annual wage of $64,140, editors earn significantly more than the average pay for all occupations. In terms of a scoring burst to avoid defeat, this is probably the NRL’s best comeback and a symbol of the Warriors legendary ability to clock off during games.. Twenty five old Buffalo Bills’ player, Kevin Everett, on September 9th tackled another player. In this cockeyed system, the deck is stacked against the Southeastern Conference.. This was a decrease from the year to date average http://www.cheapoakleys2013.com of $784 an ounce. 16, 2011, in Green Bay, Wis.

21 thoughts on “Add Snapshot Information to the VI Client using Powershell”

  1. Hugo, great idea for a script which I will run as a scheduled task.

    I was getting some inconsistent results that it was not correctly counting the number of snapshots when there were multiple sometimes cascading snapshots.

    Here’s my update which seems to work with these. Not sure how the Count is computed differently.
    $VMs = Get-VM
    ForEach ($VM in $VMs)
    {
    $SnapshotCount = (Get-Snapshot -VM $VM | Measure-Object).Count
    $VMView = $VM | Get-View
    $VMView.setCustomValue($CustomFieldName,$SnapShotCount)
    }

  2. @afokkema
    Nope. The script sets the values and exits. You have to schedule the script to run at an interval (using Windows Task Scheduler for instance) to keep the values up-to-date. I added this as a PS to the post, but I’m guessing you read the RSS feed before I updated the post.
    Hugo

  3. Something else I spotted was that each time you run the script it creates another task against each VM whether the number of snapshots has changed or not.

    So, amendment 2 is to check the value of the CustomField and only update it if the snapshot count has changed.
    This removes the extra tasks from your database and also speeds up the script as it only changes the fields when it needs to.

    $VMs = Get-VM

    ForEach ($VM in $VMs)
    {
    $ExistingSnapShotCount = $vm.CustomFields.Item($CustomFieldName)
    $SnapshotCount = (Get-Snapshot -VM $VM | Measure-Object).Count
    If ($SnapshotCount -ne $ExistingSnapShotCount)
    {
    $VMView = $VM | Get-View
    $VMView.setCustomValue($CustomFieldName,$SnapShotCount)
    }
    }

  4. Pingback: Compare ESX configurations with Powershell « H9Newser’s Blog
  5. The code “$SnapshotCount = ($VMView.Snapshot.RootSnapshotList | Measure-Object).Count” will only count the first level of snapshots. To count all snapshots you will need to recurse through the ChildSnapshotList of each snapshot in the RootSnapshotList.

    For example:

    $VMSnapshotCount = 0
    $VMSnapshots = $VMView.Snapshot.RootSnapshotList
    ForEach ($VMSnapshot in $VMSnapshots) {
    Write-Output (“Found snapshot: ” + $VMSnapshot.Name)
    $VMSnapshotCount++;
    $VMSnapshotCount += findSnapshots($VMSnapshot)
    }

    function findSnapshots([VMware.Vim.VirtualMachineSnapshot]$VMSnapshot) {
    $VMChildSnapshots = $VMSnapshot.ChildSnapshotList
    $VMChildSnapshotCount = 0
    ForEach ($VMChildSnapshot in $VMChildSnapshots) {
    $VMChildSnapshotCount += findSnapshots($VMChildSnapshot)
    }
    return $VMChildSnapshots
    }

    Note: This code is untested, but logically you need to recurse through the ChildSnapshotList for each snapshot in the RootSnapshotList.

    Simon

    1. That’s true! So why not change the code into this:
      $SnapshotCount = ($VM | Get-Snapshot | Measure-Object).Count
      You have to Change the loop to be ForEach ($VM in $VMs) and put $VMView = $VM | Get-View inside the loop.
      Hugo

  6. Hi,

    I realize this is an older post, but can you paste what the final code would look like with your suggested “ForEach” change. I’m new to powershell and need a little help if you have a moment.

    Thanks Very Much,

    Ron

  7. Hugo,

    This is a great script, very usefull in our configuration.
    Any idea how (if possible) to get the “age” of the snapshot displayed?

    Thx,
    Mike.

  8. Pingback: ben.neise.co.uk » Add Disk Size Information to the VI Client using Powershell
  9. Pingback: Add Drive Persistence State to the VI Client using Powershell « ben.neise.co.uk

Leave a Reply