Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Hyper-V Server 2012 R2 Backup VMs

Posted on 2014-09-11
Medium Priority
Last Modified: 2014-11-12
I am looking for a script that will automatically shutdown VMs so I can copy the VHDs to an alternate location and then start them back up.

Any suggestions?
Question by:BSModlin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 10

Expert Comment

ID: 40317838
I found this script on this site that should help you.

I personally use a program called Hyperoo which is excellent at doing backups real time at block level which is also great value for the price.

1.      Information
Virtual Machine Automated Backup System

There are three files:


Currently I am running this on version 2.0 of Powershell, according to some of the information I have read it should run on version 1.0 but I have not tested it.

Keep in mind that Windows installs Powershell in the %SystemRoot%\system32\WindowsPowerShell\v1.0 folder even if it is version 2.0 ... to determine the version you are running, start the Powershell cmd prompt and then issue the following command:

If you are running version 2.0 it will show Major as 2 (see below)

PS C:\> $Host.Version

Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

Now to the meat and potatoes ...


This is a cmd file that can be set to run with the Windows scheduler.

In my example, I created a C:\Powershell folder which contains all of my scripts and cmd files.

The cmd file calls Powershell with a script file called VMStartStop.ps1, this script file is passed the name of a text file that contains a list of the virtual machine names you want shut down or started up (in this case it is called "hosts.txt"). The 1 at the end of the line is passed to the script file to instruct it to Shut down the virtuals.

Next the cmd file will simply copy the vhd files from the host server to a drive on another server/computer.

Once the copy is completed, the cmd file once again calls Powershell with a script file called VMStartStop.ps1, which is passed the name of the text file that contains a list of the virtual machine names you want started up (in this case it is called "hosts.txt"). The 0 (zero) at the end of the line is passed to the script file to instruct it to Start the virtuals.


This is a simple text file that contains a list of the virtual server names you want used in the VMStartStop.ps1 script. Simply list each virtual server name on a separate line in the text file.


This is a script file I found on the internet, I can take no credit or responsibilty for this script file. However, it does work well and I am happy with the results.

2.      VMStartStop.ps1
I cannot take credit or responsibility for the following script, however, I have had very good success with this script.

Copy and paste the following information into notepad and save the file with a .ps1 extension.

# ---------- SCRIPT STARTS HERE--------------

$waitstart = 200
$waitshutdown = 120

if ($args[1] -match "0") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "Starting $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$result = $vm.requeststatechange(2)
if ($result.returnvalue -match "0") {
start-sleep -s $waitstart
write-host "" 
write-host "$guest is started" -foregroundcolor green
write-host "" 
else {
write-host "" 
write-host "unable to start $guest" -foregroundcolor red
write-host "" 

if ($args[1] -match "1") {
$inputfile=get-content $args[0]
foreach ($guest in $inputfile) {
write-host "shutting down $guest"
$vm = gwmi -namespace root\virtualization -query "select * from msvm_computersystem where elementname='$guest'"
$vmname = $
$vmshut = gwmi -namespace root\virtualization -query "SELECT * FROM Msvm_ShutdownComponent WHERE SystemName='$vmname'"
$result = $vmshut.InitiateShutdown("$true","no comment")
if ($result.returnvalue -match "0") {
start-sleep -s $waitshutdown
write-host "" 
write-host "no error while shutting down $guest"
write-host "shutdown of $guest completed" -foregroundcolor green
write-host ""}

else {
write-host "" 
write-host "unable to shutdown $guest" -foregroundcolor red
write-host "" 

else {
write-host "USAGE: to shutdown VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 1" -foregroundcolor yellow
write-host "USAGE: to start VMs," -nonewline; write-host ".\managehyperV.ps1 c:\hosts.txt 0" -foregroundcolor yellow

# ---------- SCRIPT ENDS HERE--------------

3.      hosts.txt
You will need to create a simple text file containing a list of the virtual machine names you want started and/or stopped. In my case I called the file "hosts.txt"

for example:

Virtual Srv1
Virtual Srv2
VirtXP 1

4.      BackupVM.bat
This is a simple cmd/batch file that calls Powershell with the VMStartStop.ps1 script file and passes the hosts.txt (list of virtual machines) and a single parameter that instructs the script to shutdown the virtual machine or start it up.

REM Hyper-V Virtual Machine Backup

REM Shutdown virtual machines
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\Powershell\VMStartStop.ps1 c:\Powershell\Hosts.txt 1

REM Copy virtual machines, /Y overwrites the file if it already exists

copy /Y "D:\Virtual Hard Disks\V1\Virtual Srv1.vhd" "\\computername\k$\Virtual Servers\V1\Virtual Srv1.vhd"
copy /Y "D:\Virtual Hard Disks\V2\Virtual Srv2.vhd" "\\computername\k$\Virtual Servers\V2\Virtual Srv2.vhd"
copy /Y "D:\Virtual Hard Disks\V3\VirtXP 1.vhd" "\\computername\k$\Virtual Servers\V3\VirtXP 1.vhd"

REM Start Up virtual machines

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe c:\Powershell\VMStartStop.ps1 c:\Powershell\Hosts.txt 0

REM Finished!
LVL 59

Expert Comment

by:Cliff Galiher
ID: 40317850
Perhaps I am missing something. But why would you script this? Scripts are fragile and you get no reporting on failures. And for hat you want, hyper-V Replica does this without the extra manual effort.

Author Comment

ID: 40317875
I just did some quick reading and it seems like I would require 2 "sites" for the replication.... I want to copy the VHDs to a regular Windows File Share... Not another Hyper-V Host.  Is this possible using hyper-V Replica?
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

LVL 59

Accepted Solution

Cliff Galiher earned 2000 total points
ID: 40317884
You don't need two AD sites. The two hyper-v machines can exist on the LAN.

Replica requires the destination to be a hyoer-V machine.

But if you only want to copy to a file share, why nit just us a backup product. At the very least, windows server backup is free, supports Hyper-V and will pause VMs if necessary, and can back up to a file share. Still no need for a script.

Author Comment

ID: 40317909
I was unaware that Windows Server Backup would Pause VMs automatically and perform backups.... Do you have a useful link for this?
LVL 59

Expert Comment

by:Cliff Galiher
ID: 40318119

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your vDisk VHD file gets deleted from the image store accidentally or on purpose, you won't be able to remove the vDisk from the PVS console. There is a known workaround that is solid.
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
In this Micro Tutorial viewers will learn how to use Windows Server Backup to create full image of their system. Tutorial shows how to install Windows Server Backup Feature on Windows 2012R2 and how to configure scheduled Bare Metal Recovery backup.…
In this video tutorial I show you the main steps to install and configure  a VMware ESXi6.0 server. The video has my comments as text on the screen and you can pause anytime when needed. Hope this will be helpful. Verify that your hardware and BIO…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question