PowerShell: Hyper-V Virtual Machine Backup Utility (Version 4.2)

In a previous post I wrote about the first version of my Hyper-V backup PowerShell script. This post will serve as a change log and documentation page, as the previous post was more about how the script is written as a reference for those wanting to learn PowerShell.

My Hyper-V Backup Utility PowerShell script can be downloaded from the Microsoft TechNet Gallery and the PowerShell Gallery.

Features and Requirements

  • The script is designed to be run on a Hyper-V host.
  • The device must also have Hyper-V management tools and PowerShell modules installed.
  • The script can be used to backup VMs to a device which the Hyper-V host does not have the access to run a regular Export operation.
  • The script can be used to backup VMs in a Hyper-V Cluster.
  • The script requires at least PowerShell 5.0

The script has been tested on Windows 10, Windows Server 2016 (Datacenter and Core installations) and Windows Server 2012 R2 (Datacenter and Core Installations) with PowerShell 5.0.

Should I use the -NoPerms switch or not?

The -NoPerms switch is the biggest variable in the script and it is intended as a workaround when used in an environment where the Hyper-V host doesn’t or can’t be given the required permissions to run a regular export. If you are unsure, I would suggest using the script without the -NoPerms switch first and if you run into problems, then use it.

Below are the operations the script performs with and without the -NoPerms switch.

When the -NoPerms switch is enabled:

  1. Get a list of all running Virtual Machines on the host.
  2. Gracefully shutdown the first alphabetically named VM.
  3. Copy all configuration, VHD, and snapshot/checkpoint files to the specified backup location.
  4. Start the Virtual Machine, and move on to the next if applicable.
  5. Optionally cleanup old backups or keep a configurable number of days worth of backups.
  6. Optionally create a zip file of the export and remove the original backup folder.
  7. Optionally create a log file and email it to an address of your choice.

When the -NoPerms switch is not enabled:

  1. Get a list of all running Virtual Machines on the host.
  2. Run an export operation of each VM alphabetically, exporting the VMs to the specified backup location. The VMs are kept online – no downtime.
  3. Optionally cleanup old backups or keep a configurable number of days worth of backups.
  4. Optionally create a zip file of the export and remove the original backup folder.
  5. Optionally create a log file and email it to an address of your choice.

Detailed Explanation Of The Need For The -noperms Switch

Hyper-V’s export operation require that the computer account in Active Directory have access to the location where the exports are being saved. I recommend creating an Active Directory group for the Hyper-V hosts and then giving the group the required ‘Full Control’ file and share level permissions. When a NAS such as a QNAP device for example, is intended to be used as an export location, Hyper-V will not be able to perform the operation as the computer account will not have access to the share on the NAS. Unfortunately, to copy all the files necessary for a complete backup the VM must be in an offline state for the operation to be completed, so the VM will be shutdown for the duration of the copy process.

Using this script with a Hyper-V Cluster

I’ve tested the script backing up VMs running on a Hyper-V cluster and it works just as with standalone Hyper-V hosts. I recommend setting up a staggered Scheduled Task to run the script on each of the Hyper-V hosts in the cluster. The script will detect if the host running it has any Virtual Machines with the status of ‘Running’ and perform a backup, as configured.

Generating an encrypted password file

If you’ve used a previous version I’ve changed how the script handles configuring a password for the log notification e-mail. The password must now be in an encrypted text file. The advantage of this is that the password will no longer be in plain text, which is nice. The downside is that you will now need to generate a password file. The command to do this is pretty simple, but it must be generated on the computer the script will be running on, and as the user used to run the script.

To generate the password file, run the following command in PowerShell. When running the command you will be prompted for a username and password. The username doesn’t matter and can be anything, but the password must be the password you want to use to authenticate to your SMTP server.

$creds = Get-Credential
$creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt

After running the commands, you should have a text file contained the encrypted password. Enter the path and filename for the -pwd switch to configure authenticated e-mail notification.

Configuration

The configuration can now be done via command line switches, instead of having to edit the script itself. Here’s a list of all the switches and example configurations.

Command Line Switch Mandatory Description Example
-BackupTo Yes Location of where to store the backups. Each VM will be stored in it’s own folder under the specified backup location. Can be local or UNC. Do not add a trailing \ on the end of the path. \\nas\Backups OR E:\Backups
-NoPerms No Set if the backup location is a location where the Hyper-V host will not have the permissions to perform a export operation, such as a NAS appliance

When set the VMs will be shutdown to perform the backup, when not set a regular Hyper-V export, with the VMs kept online, will be done.

N/A
-Keep No Use this switch to instruct the script to keep a specified number of days worth of backups. The script used the file creation date to determine how old a backup is. If -Compress is also used it will only delete zip file backups, and not folder backups. Every effort has been taken to only remove backup files or folders generated by this utility. 30
-Compress No Use this switch to generate a compressed zip file of the backup and remove the original backup folder afterwards. N/A
-L No Location to store the optional log file. The name of the log file is generated automatically. Do not add a trailing \ to the end of the path. E:\scripts
-SendTo No The email address to send the log file to. me@contoso.com
-From No* The email address that the log file should be sent from.

*This switch isn’t mandatory but is required if you wish to email the log file.

HyperV@contoso.com
-Smtp No* SMTP server address to use for the email functionality.

*This switch isn’t mandatory but is required if you wish to email the log file.

mail01.contoso.com

OR

smtp.live.com

OR

smtp.office365.com

-User No* The username of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

example@contoso.com
-Pwd No* The location of the file containing the encrypted password of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on your SMTP server.

c:\scripts\ps-script-pwd.txt
-UseSsl No* Add this option if you wish to use SSL with the configured SMTP server.

Tip: If you wish to send email to outlook.com or office365.com you will need this.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

N/A

Change Log

04/03/2018 Version 4.2

  • Improved logging slightly to be more clear about which VM’s previous backups are being deleted.

03/03/2018 Version 4.1

  • Added option to compress the VM backups to a zip file. This option will remove the original VM backup
  • Added option to keep a configurable number of days worth of backups, so you can keep a history/archive of previous backups. Every effort has been taken to only remove backup files or folders generated by this utility.
  • Changed the script so that when backup is complete, the VM backup folders/zip files will be have the time and date append to them.

15/01/2018 Version 4.0

  • The backup script no longer creates a folder named after the Host server. The VM backups are placed in the root of the specified backup location.
  • Fixed a small issue with logging where the script completes the backup process, then states incorrectly “there are no VMs to backup”.

12/01/2018 Version 3.9

  • Fixed a small bug that occurred when there were no VMs to backup, the script incorrectly logged an error in exporting the VMs. It now states that that are no VMs to backup.

12/01/2018 Version 3.8

  • The script has been tested performing backups of Virtual Machines running on a Hyper-V cluster.
  • Minor update to documentation.

16/10/2017 Version 3.7

  • Changed SMTP authentication to require an encrypted password file.
  • Added instructions on how to generate an encrypted password file.

07/10/2017 Version 3.6

  • Added necessary information to add the script to the PowerShell Gallery.

18/09/2017 Version 3.5

  • Improved the log output to be easier to read.

22/07/2017 Version 3.4

  • Improved commenting on the code for documentation purposes.
  • Added authentication and SSL options for e-mail notification.

20/05/2017 Version 3.3

  • Added configuration via command line switches.
  • Added option to perform regular online export if destination allows it.

24/04/2017 Minor Update

  • Cleaned up the formatting and commented sections of the script.

21/04/2017 Minor Update

  • Added the ability to email the log file when the script completes.

 

If you’d like to get in touch with me, please leave a comment or tweet me.

-Mike

Follow Mike on Twitter: @Digressive

PowerShell Code


<#PSScriptInfo .VERSION 4.2 .GUID c7fb05cc-1e20-4277-9986-523020060668 .AUTHOR Mike Galvin twitter.com/digressive .COMPANYNAME .COPYRIGHT (C) Mike Galvin. All rights reserved. .TAGS Hyper-V Virtual Machines Cluster CSV Full Backup Export Permissions Zip History .LICENSEURI .PROJECTURI https://gal.vin/2017/09/18/vm-backup-for-hyper-v .ICONURI .EXTERNALMODULEDEPENDENCIES Windows 10/Windows Server 2016/Windows 2012 R2 Hyper-V PowerShell Management Modules .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES Hyper-V PowerShell Management Tools .RELEASENOTES #>

<# .SYNOPSIS Hyper-V Backup PowerShell Utility - Creates a full backup of running Hyper-V Virtual Machines. .DESCRIPTION This script creates a full backup of running Hyper-V Virtual Machines. This script will: Create a full backup of Virtual Machine(s), complete with configuration, snapshots/checkpoints, and VHD files. If the -NoPerms switch is used, the script will shutdown the VM and copy all the files to the backup location, then start the VM. You should use the -NoPerms switch if Hyper-V does not have the appropriate permissions to the specified backup location to do an export. If the -NoPerms switch is NOT used, the script will use the built-in export function, and the VMs will continue to run. The -Keep switch should be used to keep the specified number of days worth of backups. For example, to keep one months worth of backups use -Keep 30. The -Compress switch should be used to generate a zip file of each VM that is backed up. The original backup folder will be deleted afterwards. Important note: This script should be run on a Hyper-V host. The Hyper-V PowerShell management modules should be installed. Please note: to send a log file using ssl and an SMTP password you must generate an encrypted password file. The password file is unique to both the user and machine. The command is as follows: $creds = Get-Credential $creds.Password | ConvertFrom-SecureString | Set-Content c:\foo\ps-script-pwd.txt .PARAMETER BackupTo The path the Virtual Machines should be backed up to. A folder will be created in the specified path and each VM will have it's own folder inside. .PARAMETER L The path to output the log file to. The file name will be Hyper-V-Backup-YYYY-MM-dd-HH-mm-ss.log .PARAMETER NoPerms Instructs the script to shutdown the running VM(s) to do the file-copy based backup, instead of using the Hyper-V export function. When multiple VMs are running, the first VM (alphabetically) will be shutdown, backed up, and then started, then the next and so on. .PARAMETER Keep Instructs the script to keep a specified number of days worth of backups. The script will delete VM backups older than the number of days specified. .PARAMETER Compress This option will create a .zip file of each Hyper-V VM backup. Available disk space should be considered when using this option. .PARAMETER SendTo The e-mail address the log should be sent to. .PARAMETER From The from address the log should be sent from. .PARAMETER Smtp The DNS or IP address of the SMTP server. .PARAMETER User The user account to connect to the SMTP server. .PARAMETER Pwd The txt file containing the encrypted password for the user account. .PARAMETER UseSsl Connect to the SMTP server using SSL. .EXAMPLE Hyper-V-Backup.ps1 -BackupTo \\nas\vms -NoPerms -Keep 30 -Compress -L E:\scripts -SendTo me@contoso.com -From hyperv@contoso.com -Smtp smtp.outlook.com -User user -Pwd C:\foo\pwd.txt -UseSsl This will shutdown all running VMs and back up their files to \\nas\vms. Each VM will have their own folder. A zip file for each VM folder will be created, and the folder will be deleted. Any backups older than 30 days will also be deleted. The log file will be output to E:\scripts and sent via email. #>

[CmdletBinding()]
Param(
    [parameter(Mandatory=$True)]
    [alias("BackupTo")]
    $Backup,
    [alias("keep")]
    $History,
    [alias("L")]
    $LogPath,
    [alias("SendTo")]
    $MailTo,
    [alias("From")]
    $MailFrom,
    [alias("Smtp")]
    $SmtpServer,
    [alias("User")]
    $SmtpUser,
    [alias("Pwd")]
    $SmtpPwd,
    [switch]$Compress,
    [switch]$UseSsl,
    [switch]$NoPerms)

## If logging is configured, start log
If ($LogPath)
{
    $LogFile = ("Hyper-V-Backup-{0:yyyy-MM-dd-HH-mm-ss}.log" -f (Get-Date))
    $Log = "$LogPath\$LogFile"

    ## If the log file already exists, clear it
    $LogT = Test-Path -Path $Log

    If ($LogT)
    {
        Clear-Content -Path $Log
    }

    Add-Content -Path $Log -Value "****************************************"
    Add-Content -Path $Log -Value "$(Get-Date -Format G) Log started"
    Add-Content -Path $Log -Value ""
}

## Set variables for computer name and get all running VMs
$Vs = $Env:ComputerName
$Vms = Get-VM | Where-Object {$_.State -eq 'Running'}

## Check to see if there are any running VMs
If ($Vms.count -ne 0)
{

    ## For logging
    If ($LogPath)
    {
        Add-Content -Path $Log -Value "$(Get-Date -Format G) This virtual host is: $Vs"
        Add-Content -Path $Log -Value "$(Get-Date -Format G) The following VMs will be backed up:"

        ForEach ($Vm in $Vms)
        {
            Add-Content -Path $Log -Value "$($Vm.name)"
        }
    }

    ## If the NoPerms switch is set do the following commands
    If ($NoPerms) 
    {
        ## For each VM do the following
        ForEach ($Vm in $Vms)
        {

            ## Test for the existence of a previous VM export. If it exists, delete it
            $VmExportBackupTest = Test-Path "$Backup\$($Vm.name)"
            If ($VmExportBackupTest -eq $True)
            {
                Remove-Item "$Backup\$($Vm.name)" -Recurse -Force
            }

            ## Create directories
            New-Item "$Backup\$($Vm.name)" -ItemType Directory -Force
            New-Item "$Backup\$($Vm.name)\Virtual Machines" -ItemType Directory -Force
            New-Item "$Backup\$($Vm.name)\VHD" -ItemType Directory -Force
            New-Item "$Backup\$($Vm.name)\Snapshots" -ItemType Directory -Force
            
            ## For logging, test for creation of backup folders, report if they havn't been created
            If ($LogPath)
            {
                $VmFolderTest = Test-Path "$Backup\$($Vm.name)\Virtual Machines"
                If ($VmFolderTest -eq $True)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$($Vm.name)\Virtual Machines"
                }

                Else
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$($Vm.name)\Virtual Machines"
                }

                $VmVHDTest = Test-Path "$Backup\$($Vm.name)\VHD"
                If ($VmVHDTest -eq $True)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$($Vm.name)\VHD"
                }

                Else
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$($Vm.name)\VHD"
                }
            
                $VmSnapTest = Test-Path "$Backup\$($Vm.name)\Snapshots"
                If ($VmSnapTest -eq $True)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created backup folder $Backup\$($Vm.name)\Snapshots"
                }

                Else
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem creating folder $Backup\$($Vm.name)\Snapshots"
                }
            }

            ## Stop the VM
            Stop-VM $Vm

            ## For logging
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Stopping VM: $($Vm.name)"
            }

            ## Pause the script for 5 seconds
            Start-Sleep -S 5

            ## Copy the config files and folders
            Copy-Item "$($Vm.ConfigurationLocation)\Virtual Machines\$($Vm.id)" "$Backup\$($Vm.name)\Virtual Machines\" -Recurse -Force
            Copy-Item "$($Vm.ConfigurationLocation)\Virtual Machines\$($Vm.id).*" "$Backup\$($Vm.name)\Virtual Machines\" -Recurse -Force

            ## For logging
            If ($LogPath)
            {
                $VmConfigTest = Test-Path "$Backup\$($Vm.name)\Virtual Machines\*"
                If ($VmConfigTest -eq $True)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied $($Vm.name) configuration to $Backup\$($Vm.name)\Virtual Machines"
                }

                Else
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the configuration for $($Vm.name)"
                }
            }

            ## Copy the VHD
            Copy-Item $Vm.HardDrives.Path -Destination "$Backup\$($Vm.name)\VHD\" -Recurse -Force

            ## For logging
            If ($LogPath)
            {
                $VmVHDCopyTest = Test-Path "$Backup\$($Vm.name)\VHD\*"
                If ($VmVHDCopyTest -eq $True)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied $($Vm.name) VHDs to $Backup\$($Vm.name)\VHD"
                }

                Else
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the VHDs for $($Vm.name)"
                }
            }

            ## Get the VMs snapshots/checkpoints, if any
            $Snaps = Get-VMSnapshot $Vm

            ## For each snapshot do the following
            ForEach ($Snap in $Snaps)
            {

                ## Copy the snapshot config files and folders
                Copy-Item "$($Vm.ConfigurationLocation)\Snapshots\$($Snap.id)" "$Backup\$($Vm.name)\Snapshots\" -Recurse -Force
                Copy-Item "$($Vm.ConfigurationLocation)\Snapshots\$($Snap.id).*" "$Backup\$($Vm.name)\Snapshots\" -Recurse -Force

                ## For logging
                If ($LogPath)
                {
                    $VmSnapCopyTest = Test-Path "$Backup\$($Vm.name)\Snapshots\*"
                    If ($VmSnapCopyTest -eq $True)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied checkpoint configuration for $Backup\$($Vm.name)\Snapshots"
                    }

                    Else
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem copying the checkpoint configuration for $($Vm.name)"
                    }
                }

                ## Copy the snapshot root VHD
                Copy-Item $Snap.HardDrives.Path -Destination "$Backup\$($Vm.name)\VHD\" -Recurse -Force

                ## For logging
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully copied checkpoint VHDs for $($Vm.name) to $Backup\$($Vm.name)\VHD"
                }
            }

            ## Start the VM
            Start-VM $Vm

            ## For logging
            If ($LogPath)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Starting VM: $($Vm.name)"
            }

            ## Pause the script for 30 seconds before proceeding
            Start-Sleep -S 30

            ## If the keep option is not configured
            If ($History -eq $Null)
            {
                ## If the compress option is not configured
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folders
                    Get-ChildItem -Path $Backup -Filter "$($Vm.name)-*-*-*-*-*-*" -Directory | Remove-Item -Recurse -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous backup folders for $($Vm.name)"
                    }
                }
            }

            ## If the keep option is configured
            Else
            {
                ## If the compress option is not configured
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folder that are older than the configured number of days
                    Get-ChildItem -Path $Backup -Filter "$($Vm.name)-*-*-*-*-*-*" -Directory | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Recurse -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing backup folders for $($Vm.name) older than: $History days"
                    }
                }
            }

            ## If the compress option is configured
            If ($Compress)
            {
                ## If the keep option is not configured
                If ($History -eq $Null)
                {
                    ## Remove all previous compressed backups
                    Remove-Item "$Backup\$($Vm.name)-*-*-*-*-*-*.zip" -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous compressed backups for $($Vm.name)"
                    }
                }

                ## If the keep option is configured
                Else
                {
                    ## Remove previous compressed backups that are older than the configured number of days
                    Get-ChildItem -Path "$Backup\$($Vm.name)-*-*-*-*-*-*.zip" | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing compressed backups for $($Vm.name) older than: $History days"
                    }
                }

                ## Compress the VM backup into a zip, and delete the VM export folder
                Add-Type -AssemblyName "system.io.compression.filesystem"
                [io.compression.zipfile]::CreateFromDirectory("$Backup\$($Vm.name)", "$Backup\$($Vm.name)-{0:yyyy-MM-dd-HH-mm-ss}.zip" -f (Get-Date))
                Get-ChildItem -Path $Backup -Filter "$($Vm.name)" -Directory | Remove-Item -Recurse -Force

                ## For logging
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created compressed backup of $($Vm.name)"
                }
            }
        
            ## If the compress option is not configured
            Else
            {
                ## Rename the export of each VM to include the date
                Get-ChildItem -Path $Backup -Filter $($Vm.name) -Directory | Rename-Item -NewName ("$Backup\$($Vm.name)-{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date))
            }

            ## Pause the script for 30 seconds before proceeding
            Start-Sleep -S 30
        }
    }

    ## If the NoPerms option is not set
    Else
    {
        ForEach ($Vm in $Vms)
        {
            ## Test for the existence of a previous VM export. If it exists, delete it otherwise the export will fail
            $VmExportBackupTest = Test-Path "$Backup\$($Vm.name)"
            If ($VmExportBackupTest -eq $True)
            {
                Remove-Item "$Backup\$($Vm.name)" -Recurse -Force
            }
        }

        ## Do a regular export of the VMs
        $Vms | Export-VM -Path "$Backup"

        ## For logging
        If ($LogPath)
        {
            $VmExportTest = Test-Path "$Backup\*"
            If ($VmExportTest -eq $True)
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully exported specified VMs to $Backup"
            }

            Else
            {
                Add-Content -Path $Log -Value "$(Get-Date -Format G) ERROR: There was a problem exporting the specified VMs to $Backup"
            }
        }

        ForEach ($Vm in $Vms)
        {
            ## If the keep option is not configured
            If ($History -eq $Null)
            {
                ## If the compress option is not configured
                If ($Compress -eq $False)
                {
                    ## Remove all previous backup folders
                    Get-ChildItem -Path $Backup -Filter "$($Vm.name)-*-*-*-*-*-*" -Directory | Remove-Item -Recurse -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous backup folders for $($Vm.name)"
                    }
                }
            }

            ## If the keep option is configured
            Else
            {
                ## If the compress option is not configured
                If ($Compress -eq $False)
                {
                    ## Remove previous backup folders older than the configured number of days
                    Get-ChildItem -Path $Backup -Filter "$($Vm.name)-*-*-*-*-*-*" -Directory | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Recurse -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing backup folders for $($Vm.name) older than: $History days"
                    }
                }
            }

            ## If the compress option is enabled
            If ($Compress)
            {
                ## If the keep option is not configured
                If ($History -eq $Null)
                {
                    ## Remove all previous compressed backups
                    Remove-Item "$Backup\$($Vm.name)-*-*-*-*-*-*.zip" -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing previous compressed backups for $($Vm.name)"
                    }
                }

                ## If the keep option is configured
                Else
                {
                    ## Remove previous compressed backups older than the configured number of days
                    Get-ChildItem -Path "$Backup\$($Vm.name)-*-*-*-*-*-*.zip" | Where-Object CreationTime –lt (Get-Date).AddDays(-$History) | Remove-Item -Force

                    ## For logging
                    If ($LogPath)
                    {
                        Add-Content -Path $Log -Value "$(Get-Date -Format G) Removing compressed backups for $($Vm.name) older than: $History days"
                    }
                }

                ## Compress the VM backup into a zip, and delete the VM export folder
                Add-Type -AssemblyName "system.io.compression.filesystem"
                [io.compression.zipfile]::CreateFromDirectory("$Backup\$($Vm.name)", "$Backup\$($Vm.name)-{0:yyyy-MM-dd-HH-mm-ss}.zip" -f (Get-Date))
                Get-ChildItem -Path $Backup -Filter "$($Vm.name)" -Directory | Remove-Item -Recurse -Force

                ## For logging
                If ($LogPath)
                {
                    Add-Content -Path $Log -Value "$(Get-Date -Format G) Successfully created compressed backup of $($Vm.name)"
                }
            }
        
            ## If the compress option is not enabled
            Else
            {
                ## Rename the export of each VM to include the date
                Get-ChildItem -Path $Backup -Filter $($Vm.name) -Directory | Rename-Item -NewName ("$Backup\$($Vm.name)-{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date))
            }
        }
    }
}

## If there are no VMs, then do nothing
Else
{
    ## For Logging
    If ($LogPath)
    {
        Add-Content -Path $Log -Value "$(Get-Date -Format G) There are no VMs running to backup"
    }
}

## If log was configured stop the log
If ($LogPath)
{
    Add-Content -Path $Log -Value ""
    Add-Content -Path $Log -Value "$(Get-Date -Format G) Log finished"
    Add-Content -Path $Log -Value "****************************************"

    ## If email was configured, set the variables for the email subject and body
    If ($SmtpServer)
    {
        $MailSubject = "Hyper-V Backup Log"
        $MailBody = Get-Content -Path $Log | Out-String

        ## If an email password was configured, create a variable with the username and password
        If ($SmtpPwd)
        {
            $SmtpPwdEncrypt = Get-Content $SmtpPwd | ConvertTo-SecureString
            $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList ($SmtpUser, $SmtpPwdEncrypt)

            ## If ssl was configured, send the email with ssl
            If ($UseSsl)
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
            }

            ## If ssl wasn't configured, send the email without ssl
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds
            }
        }

        ## If an email username and password were not configured, send the email without authentication
        Else
        {
            Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer
        }
    }
}

## End

42 Comments Add yours

  1. Joe says:

    Hi. I’m trying to use your script, but every VM that it tried to backup it says
    Export failed for virtual machine with error ‘The specified server cannot perform the
    requested operation.’ (0x8007003A).

    Like

    1. Mike Galvin says:

      Hi there,

      I’ve not come across this error before. What version of Hyper-V are you running?

      Like

      1. Joe says:

        I’m using Hyper-V on windows 10. It works if i do -noperms

        Like

      2. Mike Galvin says:

        Ok, thanks. If it works with the -noperms option then it’s most likly because of the permissions that the computer running Hyper-V has to the destination that you have configured.

        For me, I added the -noperms switch because my Hyper-V server is a member of a domain and I wanted to export the VMs to a NAS device which was not running a Windows OS, and so could not grant the permissions needed for the proper export function to work.

        Like

      3. Joe says:

        That makes sense, I’m in the same boat trying to backup to a NAS.

        But the script is awesome, thank you for sharing!

        Like

      4. Mike Galvin says:

        No problem. Thanks for checking it out, I’m glad it’s helped you out. 🙂

        Like

  2. Papee John says:

    Great script, amazing job.
    Easy to undestarnd code

    Thank you very much

    Like

    1. Mike Galvin says:

      Thanks for the kind words, glad it helped!

      Like

  3. R. E. Martin says:

    I want to select specific VM’s so I assume I need to change the;
    $vs = $env:computername
    $vms = Get-VM | Where-Object {$_.State -eq ‘Running’}
    to somehow select the specific names instead of just running?
    I am not a programmer or understand scripting well, but it seems that it should be fairly easy since most of the VM’s names are similar to XXX16Vxx. Or just explicitly name them when running the script.

    Like

    1. Mike Galvin says:

      That’s right. You can change:
      $vms = Get-VM | Where-Object {$_.State -eq ‘Running’}

      to

      $vms = Get-VM -name *16V*

      for all VMs with 16V in the name. Lots more info on the Get-VM cmdlet here: https://technet.microsoft.com/en-us/itpro/powershell/windows/hyper-v/get-vm?f=255&MSPPError=-2147217396

      Like

  4. R. E. Martin says:

    Can I assume from the script the previously exported VM is deleted before the next export for that particular VM name?
    Also, I am going to try to modify the script in order to export certain VM’s to one host and other VM’s to another for DR purposes. I am not very good at scripting, but it seems that it should be fairly easy to modify one script or just have 2 scripts run (one script exports VM’s 1 & 2 to HOST1 and one script VM 3 to HOST2).

    Like

    1. Mike Galvin says:

      That’s correct, it will remove the previous backup of the VM, if it’s there. I like the idea of exporting to another VM host. 🙂

      Like

  5. Federico Barreiro says:

    Hello, i have an issue, when i execute the scritp and put the backup directory g:\ the shell said couldn’t creat a directory because didn’t founde the file in line 225 character 12
    + $Vms | Export-VM -Path “$Backup\$vs”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Microsoft.HyperV.PowerShell.VMTask:VM
    Task) [Export-VM], VirtualizationOperationFailedException
    + FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.Ex
    portVMCommand

    Like

    1. Federico Barreiro says:

      solved, bad typing lol

      Liked by 1 person

      1. Mike Galvin says:

        No worries!

        Like

  6. Lennon says:

    Awesome script, great job. One feature that would be great is date and time stamped directories e.g. ///. Right now I’m scripting them at the top level via another script that calls the backup script but it’s a bit clunky. Feels like the time stamp should be lower in the tree per the example.

    Like

    1. Lennon says:

      Ouch, parsed the brackets out of my last example. Should have been: /host/VM/time&date/resources

      Like

      1. Mike Galvin says:

        That’s a good idea! I’ll maybe add it in a future release. Cheers!
        -Mike

        Like

  7. Quinten Questel says:

    Hi If i wanted to keep the last three backups how would i accomplish this?

    Like

    1. Quinten Questel says:

      Ok I don’t need this feature anymore,

      Like

  8. emerson says:

    hi, how to modify the script as weekly run?

    Like

    1. Mike Galvin says:

      You shouldn’t have to modify the script at all, just run the script as a Scheduled Task, once a week. The command would look something like this: PowerShell -ExecutionPolicy bypass F:\scripts\Hyper-V-Backup.ps1 -backupto \\nas\Archive\VM_Backups -l F:\scripts -noperms -sendto me@contoso.com -from vs10@nct.ac.uk -smtp smtp.contoso.com

      -Mike

      Like

      1. emerson says:

        Thanks Mike.
        One more question, if I want to copy the backup to NAS, is it necessary to shutdown the VM to backup?

        Like

      2. Mike Galvin says:

        Hi there,
        It depends on the NAS. I used the example of a NAS because for me, my NAS is a QNAP and not running Windows, so Hyper-V can’t authenticate with it to do a live export – so the VM must be shutdown to copy the files. You may have a NAS which Hyper-V can authenticate to. I realise it’s not ideal to have to shutdown, but I’d rather shutdown and get a known good backup.

        -Mike

        Like

  9. Alfred says:

    Hi Mike. Works great. I’m a newbe at this… can you point me on how to restore the VM backups if the original Windows 10 machine was totally lost… so now the VMs need to be restored to a different Windows 10 machine. Thanks

    Like

    1. Mike Galvin says:

      Hi Alfred, Sure thing – you copy the folders of the VMs from your backup location, and do a Virtual Machine import in Hyper-V. You can put the VHDs separate from the VM configuration files if you want to, or need to. When importing, it’ll ask you where the locations of the files are.

      -Mike

      Like

  10. Alfred says:

    Thanks Mike. Had done that but had gotten an error. Did it again and here are the details:

    On Windows 10 Host A:
    – Created a ubuntu vm called “test” (no checkpoint was created)
    – Successfully launched your script: ” .\Hyper-V-Backup.ps1 –backup to D:\Bridge\VMBackup\VM -l D:\Bridge\VMBackup\logs -noperms”
    – The new backup dir has: An empty “Snapshots” dir —– a “VHD” dir with just one .avhdx file —– and a “Virtual Machines” dir with 3 files in it: .vmcx, .vmgs and .VMRS and an empty dir called “6d68d914-6ec0-4bb2-9854-ace2930df601”

    Copied the backup dir to a new Windows 10 (Host B) and did the following:
    – Launched “Import Virtual Machine”
    – For “Specify the folder containing the virtual machine to import”: Pointed to the parent dir containing the 3 dir your backup script created
    – For “Select the virtual machine to import” I selected “test”
    – For choose the type of import to perform I selected “Registered the virtual machine in-place” (fyi also tried “copy the virtual machine” but get the same error below)
    – HERE IS THE PROBLEM.. For “Where are the virtual disks for this virtual machine stored”: Pointed to the VHD dir… seems to accept it…
    – … but when I click “Finish” I get an import error… “fail to open virtual disk” … seems like it’s looking for a .avhdx file

    What am I doing wrong?

    -Cheers, Alfred

    Like

    1. Alfred says:

      solved… typo 😦

      Liked by 1 person

  11. CJ says:

    Very nice script, running it right now and it looks good.
    But two things 🙂

    I was not sure if I can put “:” in the script because my mailhost running a diffrent port for secure SMTP (even non secure it running another port then 25)

    I also notice it does not like if I have pass-through disks that is attached.
    I maybe can’t run a export if a virtual machine have that.

    But going to see tomorrow if it have run a export of the rest of my virtual machines.

    Like

    1. CJ says:

      A little update, it did do a nice update of the viritual machine (but not the machine that have pass-through disks but that is ok)
      But I can’t use : so maybe is a good idea next time add it make it easy to change port 🙂

      Liked by 1 person

  12. Love your script saves me a lot of time, i tweak it a bit for my usage to load a vm names from a file so i can change the vms i wanted to backup, and when i set up the scheduler i thought why not putting all the params in a config file and just give back the config path. it can be like another optional parameter if it is passed it overrides all the other options.

    Like

  13. Jorge says:

    how can i set a path location to save my backup, because every time that i run the script the command line say this:
    PS C:\Users\user1\Desktop\Hyper-V-Backup_v3-5> C:\Users\user1\Desktop\Hyper-V-Backup_v3-5\Hyper-V-Backup_v3-5.ps1
    cmdlet Hyper-V-Backup_v3-5.ps1 at command pipeline position 1
    Supply values for the following parameters:
    Backup:

    Like

    1. Mike Galvin says:

      Hi Jorge,

      You must add the command line switch “-BackupTo E:\Backups” for example. No quotes. I have actually just updated to add a few more features and update documentation.

      -Mike

      Like

  14. Michael says:

    Hi Mike

    Thanks for a great script. I have tried this script on a Windows 12016 host and it works perfectly. I then copied it to a Windows 10 host and ran it but it isnt doing anything. When I check the log files it says there are no VMs running to backup however I have about 9 VMs running. Any advice on this would be appreciated

    Like

    1. Mike Galvin says:

      Hi Michael,

      This might seem like a silly questions but: The 9 VMs that are running, are they on the Windows 10 machine? The script is designed to run on the Hyper-V host that the VMs are running on, not a remote host.

      -Mike

      Like

      1. Michael says:

        So after double checking everything it turns out the PC isnt running windows 10 but rather windows 8.1
        Im going to assume this script doesnt play well with Windows 8.1
        Back to the drawing board…

        Like

      2. Mike Galvin says:

        Ah I see. I haven’t tested it with Windows 8.1, but seeing as Windows Server 2012 R2 is based off of Windows 8.1 the script should work fine. It also could be the version of PowerShell running on your Windows 8.1 PC.

        -Mike

        Like

  15. tadeusznikitin says:

    thanks for that script. great job!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s