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:

# Description:
# Set's the Working Location for a VM. This location will hold the vswap and snapshot delta files.

# Usage:
# First, connect to a virtual server server (Connect-VIServer)
#
# Set snapshot location for one vm (folder must already exist on datastore):
# Set-VMSnapshotLocation -vm VM001 -Location "[BigAssDatastore] Snapshots"
#
# For all vms at once (no folder specified will put all in the root of the datastore):
# Get-VM | ForEach-Object {Set-VMSnapshotLocation -vm $_ -Location "[BigAssDatastore]"}
#
# Reset the location to the original location (with the vmx file):
# Set-VMSnapshotLocation -vm VM001 -Reset

Function Set-VMSnapshotLocation
	{
	param($vm, [string]$Location, [switch]$Reset=$False)

	#Region Input Validation
	# Check vm parameter
	If (!$vm)
		{
		Throw "No VM specified."
		}
	# Check Location parameter.
	# Can be empty when using Reset, can be "[datastore]" or can be "[datastore] folder" (folder must exist).
	If (!$Reset -and $Location -notmatch "\[\S+\](\s\w+)?")
		{
		Throw "Invalid location specified. Syntax: [Datastore] Folder"
		}
	# Specifying Reset will set Location to empty, which causes the original location to be used (same location as vmx file).
	If ($Reset)
		{
		$Location = ""
		}
	# Make sure to get a vm object to work with.
	# This allows both using a vm name when targeting one vm (Set-VMSnapshotLocation -vm VM001 -Reset);
	# AND piping a collection of vm's (Get-VM | ForEach (Set-VMSnapshotLocation -vm $_ -Location "[BigAssDatastore] Snapshots" ).
	If ($vm.GetType().Name -eq "String")
		{
		$oVM = Get-VM $vm
		}
	ElseIf ($vm.GetType().Name -eq "VirtualMachineImpl")
		{
		$oVM = $vm
		}
	Else
		{
		Throw "Parameter vm is of an invalid type."
		}
	#EndRegion InputValidation

	#Region Main
	# Get advanced properties of vm
	$vmView = $oVM | Get-View
	# Create object containing new properties
	$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
	$vmConfigSpec.Files = New-Object VMware.Vim.VirtualMachineFileInfo
	$vmConfigSpec.Files.SnapshotDirectory = $Location
	# Reconfigure VM using new properties
	$vmView.ReconfigVM($vmConfigSpec)
	#EndRegion Main
	}

You can download it here: Set-VMSnapshotLocation (rename to .ps1)

I tried hard as I could, but I could not get my vm to die. Even running the script, creating a snapshot, running the script to move the working location again and deleting the snapshot did not prove a problem. However, I urge you to be careful with it. I’m not responsable for anything you do with it.

Tiny glitch: the VI Client shows the wrong datastore location when a vm has a snapshot in a modified workingdir.

Enjoy.

»crosslinked«

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

Athletic trainers can work as coaches or coordinators with the team, work on drills and practices, and prevent and isolate injuries. Performance was below our expectations during June, driven by semi annual sales performance; however, we leveraged our read and react capabilities and rebounded with our July floor set, delivering strong growth. You could also make the 9 ball on the break, and win the game that way. Put all those party nightmares to rest! Follow these instructions and you can have the PERFECT pi for any occasion for under $10.00, with no measuring or mixing of messy pastes.. “There is a hell of a lot of http://www.cheapnfljerseyssu.com belief in this group and I guess we have got a lot of guys who have been here before. You’d teach something to Leonard in a drill on an individual basis, and he was applying it in the next drill. The time AFTER Christmas is always a great time to search out games, too. “Federal Wholesale NFL Jerseys Home Loan Banks were freed to increase their purchase of mortgage backed bonds by about $150 billion as part of a government effort to pump money back into the market, according to Bloomberg. Computer simulation, so one human player called one team’s offensive plays. There were number of measures which were included in this index, However, to keep the HDI simple and manageable, the following main variables were included in it (a) Life expectancy was chosen as a measure of long life (b) Literacy as an index of knowledge and (c) Real GDP per person.. Out of nowhere, a hip looking woman enters the room and throws the new Macintosh into Cheap NFL Jerseys China the screen, shattering it in the process. Or, sometimes, they showed them the awls themselves, as they were stabbed into them. RAYMOND COLELLO: You know, at that point, I was thinking, wow, they really need a force field around their heads of some sort. The problem isn’t just cheap oakleys the Seahawks here. Stirring news for hardcore NFL Fans! http://www.elitecheapnfljerseysauthentic.com The NFL discount football jerseys RedZone cheap nfl jerseys channel has been available on dish network from 2009. Mariota has excellent size at about 6’4 215 and runs a sub 4.5. OPL, outer plexiform layer; INL, inner nuclear layer; IPL, inner plexiform layer; GCL, ganglion cell layer. Factor in travel to games and meetings and it can be as many as 50 hours, said Hank Zaborniak, who was a fill in official during the lockout.. There’s a ton of those tools, so it’s fine to waste one. In any case, that idiot, the only one out of the original six muggers, was instantly kicked in throat.. To such statesmen as she had then that act of brigandage must have appeared inspired by great political wisdom. The bowling industry is certainly no stranger to big corporate sponsors and has partnered with NASCAR racing and other big sports and entertainment industries.

15 thoughts on “Set VMware Snapshot Location with Powershell”

  1. Hi,

    great Script. I search for an script about snapshot. With Defines who has created, deleted an the timestamp about this.

    Can you help me about this???

    thanks

    1. Hi Wolfi,
      Tanks.
      I don’t have the time to write a script for you right away. But search for PowerCLI Snapshots Get-VIEvent and you might find something.
      I know the info you need is not included in the properties of a snapshot, so you have to retrieve it from the event generated when a snapshot is created.
      Hugo

      »crosslinked«

  2. Pingback: Changing snapshot location without VM downtime « a CraZy PeNguIn
  3. Love the script – thanks.

    Your script sets the workingdir which affects both swap and snapshots, but I’d like to keep my swap files with the vm. Do you know how to set sched.swap.dir on-the-fly? I can only find references to editing the vmx file.

  4. Running the command on one VM comes back with Set-VMSnapshotLocation is not recognized as the name of a cmdlet. We are running 4.1 U1. Cannot find any mention of this cmdlet anywhere except your site. Something I need to add to PowerCLI to get this to work? Thanks.

  5. I struggled a bit with getting this to work. Here’s how I got it to work.

    PS>> .”C:\Scripts\Set-VMSnapshotLocation.ps1″
    PS>> Set-VMSnapshotLocation -vm vm01 -location “datastore”
    PS>> Set-VMSnapshotLocation -vm vm01 -location “datastore”

    The first line adds the function(s) within the .ps1 file to your PS session, which can then be called directly with parameters. I’ll probably rename the .sp1 file to something shorter, in my environment.

  6. The key to adding the function was the fully qualified path. Using .\Set-VMSnapshotLocation.ps1 from my scripts folder did not add the function to my session.

    You should verify the function was added before executing…

    PS>> get-childitem function:set*

Leave a Reply