Set VMware Snapshot Location with Powershell

Snapshots are m*th3rfcukers. If you’re not careful, they will mass-murder your vms. Yet they allow you to time-travel! You want to use them, but how to prevent a massacre? Here’s how: relocate the delta files.

When you create a snapshot, the current state of the vm is preserved by leaving the disk files alone. All changes since the moment of creating the snapshot are written to delta files. The delta files are stored in the vm’s working directory. The working directory is – by default – the location where the vmx and other config files reside. If that datastore runs out of free space – especially if it also contains disk files – you’re in a bit of a kerfuffle. Vms not booting or being frozen as if they stared into Medusa’s reptoid eyes.

So you can do two things: reserve overhead in your datastores and stay afraid some overactive snapshot might destroy your environment, or set the working directory of your vms to some big-ass datastore you don’t use for anything else and let the snapshots enjoy themselves. If they fill up the datastore, they only kill all vms with snapshots, not the rest.

But how, you ask? You can edit the vmx files of you vms manually – which requires your vms to be powered down – and add the line: workingDir = “/vmfs/volumes/<insanely long guid you need to somehow find>/”

Or, you run my script and change the working location on the fly:

Continue reading “Set VMware Snapshot Location with Powershell”

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)

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

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 made 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 of $784 an ounce. 16, 2011, in Green Bay, Wis.

Powershell Oneliner #4

Yesterday, alanrenouf asked the following question on the VMware Community VI Toolkit forums:

Is there a way (preferably a one-liner) to get a list of vm’s and the number of snapshots per vm?

Here’s a script that will get that info:

$VC = Connect-VIServer $VCServerName
$vms = Get-VM
$myCol = @()
ForEach ($vm in $vms)
  $snapshots = Get-Snapshot -VM $vm
  $myObj = "" | Select-Object VM, NumSnapshots
  $myObj.VM = $
  $myObj.NumSnapshots = ($snapshots | measure-object).count
  $myCol += $myObj
$myCol | Where-Object{$_.NumSnapshots -gt 0} | Sort-Object VM | Format-Table -AutoSize

And here’s a one-liner that does the same thing: 

Get-VM |
  Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} |
  Format-Table Name, `
  @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}