Powershell Export Hyper-VM

By | April 2, 2018

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et sapien purus. Sed augue ipsum, pellentesque eu sodales non, dictum ac metus. Maecenas a nibh pretium, eleifend orci sit amet, venenatis risus. Vestibulum sollicitudin eros a tortor luctus, sed eleifend enim hendrerit. Suspendisse in viverra lectus. Vivamus vitae orci tellus. Curabitur a ligula dolor. Phasellus malesuada non velit vitae rhoncus. Morbi mollis imperdiet vestibulum. Vivamus felis magna, efficitur eu metus eu, ultrices bibendum erat.

$date = Get-Date -Format dd.MM.yyyy_HH.mm.ss
$VM = Get-VM VM1, VM2, VM5
foreach ($i in Get-ChildItem E:\Hyper-V_Backup\*)
{
    if ($i.CreationTime -lt ($(Get-Date).AddDays(-2)))
    {
        Remove-Item $i -Recurse
    }
}

foreach ($i in Get-ChildItem \\RemoteServer\Hyper-V_Backup$\*)
{
    if ($i.CreationTime -lt ($(Get-Date).AddDays(-2)))
    {
        Remove-Item $i -Recurse
    }
}

Stop-VM $VM
Export-VM $VM -Path E:\Hyper-V_Backup\$date
Start-VM $VM
robocopy /E /S E:\Hyper-V_Backup\$date \\RemoteServer\Hyper-V_Backup$\$date