Friday, January 17, 2014

Change Storage Location of Smart Paging File and Snapshot location for HyperV VMs

So when you provision a big HyperV machine and forget to change the default location for "Virtual Machines" it won't take long till the system volume of your Hyper visor starts to run out of disk.

After you punish the admin that didn't change the default Virtual Machine Location you can use this script to move the paths to same folder as the first VHD of each VM.

$vms = get-vm -ComputerName SOMEHYPERVBOX | where {$_.smartpagingfilepath -like "c:\*"| select Name, SmartPagingfilepath

foreach ($vm in $vms)
{
    [string]$newpath = [system.io.path]::getdirectoryname((Get-VM -ComputerName 
SOMEHYPERVBOX -Name $vm.name | Get-VMHardDiskDrive|select -first 1).path)
    [string]$name = $vm.name
    Move-VMStorage -ComputerName 
SOMEHYPERVBOX -VMName $name -SmartPagingFilePath "$newpath" -SnapshotFilePath "$newpath"
}
This works for us, you may need to tweak it to comply with the particular requirements of your environment.

Good luck and make sure that Admin never rolls servers into prod with default settings again!

3 comments:

  1. Hi Ben,

    Does the smart page file location and snapshot file path should be under same path always?

    ReplyDelete
  2. Works great and correctly changes the path on the VM, but the hypervisor still puts the paging file in the old location. I've restarted the host and everything, any ideas? All I can think of is deleting the VM and starting fresh!

    ReplyDelete
  3. Nevermind, I was confusing the paging file with the .bin files, which are set upon creation only and can't be moved without exporting and importing the VM. Task for another day!

    ReplyDelete