# 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 }