Updated Move-VMStorage2 function for Hyper-V PowerShell

I have been updating my function that extends the Hyper-V PowerShell module cmdlet Move-VMStorage. A while ago I made a blog post about that I think that the folders on the source directory should be removed when doing a Live Storage Migration or you will get a mess with empty VM folders after a while and that can cause some confusion for the admins.

The updates in this script function are the following:

  • I will not delete folders if they do not reside within a folder with the VM´s name (In the earlier version I just deleted and that recursive with no questions asked which could have some consequences )
  • And if the folder was the default or named with another name you will get an output that tells you to clean manually
  • If you do not give the VM´s Name in the -Path parameter I will add that for you to get a nice and tidy folder structure

Here is a screendump on the updated function in action

move-vmstorage2

And here you can see what happens if I move from a folder that is not named after the VM´s name

manuallyclean

And here is a screendump of the folders that are left in the c:\vms that you need to manually delete and you might want to check that not another vm is residing inside these folders before removing them 😛

foldersbehind
01<#
02.Synopsis
03   An updated Move-VMStorage function
04.DESCRIPTION
05   To also remove the folder where the VM was residing this function also deletes the folder after moving the VM
06   This function also helps you in creating a folder in the path if forgotten
07.EXAMPLE
08   Move-VMStorage2 -VMName test -ComputerName HV02 -Path \\SMB-srv01\VMs\test
09.NOTES
10Author: Niklas Akerlund 20130226
11Version: 0.2
12#>
13function Move-VMStorage2
14{
15    [CmdletBinding()]
16    [OutputType([int])]
17    Param
18    (
19        # A name of a VM
20        [Parameter(Mandatory=$true,
21                   ValueFromPipelineByPropertyName=$true,
22                   Position=0)]
23        $VMName,
24        # The name of the Hyper-V host
25        [Parameter(Mandatory=$false,
26                   ValueFromPipelineByPropertyName=$true,
27                   Position=1)]
28        $ComputerName = "localhost",
29        # The path where the VM is going to be relocated to.
30         [Parameter(Mandatory=$true,
31                   ValueFromPipelineByPropertyName=$true,
32                   Position=2)]
33        [string] $Path
34    )
35 
36        # Lets move and tidy the source folder
37        $VM = Get-VM $VMName -ComputerName $ComputerName
38        # For some reason the path does not get refreshed when moving one VM several times in the same console that is why i do a select *
39        $VMOldPath = Get-VM $VMName -ComputerName $ComputerName | select * -ExpandProperty Path
40                 
41        if ($Path -notmatch $VM.VMName){
42            $Path = $Path + "\" + $VM.VMName
43            Move-VMStorage -VM $VM -DestinationStoragePath $Path
44        }else{
45            Move-VMStorage -VM $VM -DestinationStoragePath $Path
46        }
47         
48        if (($VMOldPath.StartsWith("\\")) -and $VMOldPath -match $VM.VMName) {
49           Remove-Item -Path $VMOldPath -Recurse -Force
50        }elseif ($VMOldPath -match $VM.VMName){
51            Invoke-Command -ComputerName $VM.ComputerName -ScriptBlock {Remove-Item -Path $Using:VMOldPath -Recurse -Force}
52        }else{
53            Write-Host "The VM :" $VM.VMName " was in the following path " $VMOldPath " Clean it manually!"
54        }
55         
56}

Comments

Nick M
Reply

Hello, I have a question about your function. Currently, i think it would do a good service to those admins who use live migration, whether shared stored in a cluster, or shared-nothing in a standalone sense. But how does it work when you run VMM 2012 SP1? How does VMM handle moves of machines? Does it leave directories behind too?

And if so, does VMM handle typical, user initiated VM moves differently than it does the auto-dynamic optimized moves that could happen up to every 15min?

Leave a Reply to Nick MCancel reply

name*

email* (not published)

website