PowerShell, Hyper-V: Drain standalone Hyper-V host

If you need to drain a non clustered Hyper-v server then you have the lovely shared nothing live migrations tool at your disposal. Of course, its greatness is matched only by how very slow and tedious it is. What you really want to do is set something going and then be told when it's finished.

So, I wrote this script that does exactly that, enjoy!

$VPSS = Get-VM

foreach ($VPS in $VPSS)
{
$VPSname = $VPS.VMName
$now = get-date -Format g
Write-Host "$now :Moving VM : $VPSname" 
Move-VM $VPSname [Servername] -IncludeStorage -DestinationStoragePath [path]$VPSname
}
Send-MailMessage -To "recipient <recipients@mail.net>" -From "VPS migtool <VPSMigtool@mail.net>" -Subject "Migration complete" -SmtpServer "smtpserver"