Solved

Scripting the shut down and startup of VM's in 2008 R2 Hyper-V

Posted on 2014-12-02
15
285 Views
Last Modified: 2014-12-03
Hello experts.  I'm looking to write a script to shut down running VM's (with verification preferably - return code or something), make a copy of the VHD file (in the same folder), restart the VM's, then copy the VHD files over to some NAS storage.  I'm comfortable with everything (using regular DOS batch files) but the scripted shutdown and startup of the VM's.  I don't know any PowerShell or VB or anything like that, so if something like that would be needed, I would need to have it called by my batch process and have it return a pass/fail somehow (for both shutdown then startup)...or have the whole thing written in PowerShell or VB or whatever.

The environment is Windows Server 2008 R2 with Hyper-V.  If it matters, one VM is also 2008 R2 and the other is Windows Server 2003 R2.

Thanks for your help!
Bob
0
Comment
Question by:breichard
  • 7
  • 6
  • 2
15 Comments
 
LVL 5

Expert Comment

by:A Karelin
ID: 40477889
0
 
LVL 24

Accepted Solution

by:
VB ITS earned 500 total points
ID: 40478111
This article looks like it contains the steps to do exactly what you are requesting for.

You'll just need to modify Step 4 and specify the path to your NAS - I don't see the need to create a copy of the VHD files on the host then copy it again over to the NAS.
0
 

Author Comment

by:breichard
ID: 40478320
Thanks for both your comments.  There are reasons why I want a copy of the whole VHD file - I won't bore you with the whole story.  The reason for copying locally first is to get the VMs back up as quickly as possible.  I'll take a look at these, hopefully today, and get back to you.

Thanks again,
Bob
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 5

Expert Comment

by:A Karelin
ID: 40478354
Using Windows Server Backup allows you to backup running virtual machines without interraption.
0
 

Author Comment

by:breichard
ID: 40478364
Understood, but it's not going to allow me to create a ready-to-go VHD file, is it?
0
 
LVL 24

Expert Comment

by:VB ITS
ID: 40478375
Have a look at this article as well breichard as it will export the VM which makes DR a piece of cake. Requires a bit more work in that you need to install the Hyper-V Management Library tools but I'd personally prefer importing a VM as opposed to recreating it and then attaching the VHD as in this scenario you would have to specify the RAM, vCPUs, reconfigure the NICs inside Windows, etc.
0
 

Author Comment

by:breichard
ID: 40478462
VB ITS - Can you later use Import Virtual Machine from within the Hyper-V manager?  Does part of this include a full copy of the VHD file (in case didn't care to import, just wanted to use the VHD for something else)?

Thanks, I hope to be able to work on this today.
0
 
LVL 24

Expert Comment

by:VB ITS
ID: 40478472
Yep, it exports the entire VM configuration which you can use to import into Hyper-V Manager on another Hyper-V host if you wanted (just make sure the other host is running the same version of Windows Server). You can still pull out the VHD files from the exported VM files if you want to make a clone of a VM for testing purposes or for whatever other reason you may have.
0
 

Author Comment

by:breichard
ID: 40479189
VB ITS, I decided to try the link from your first post above (first, at least).  When the powershell script is invoked, it gets an error when it tries to do the actual InitiateShutdown.  See the screenshot.  The server name and VM name are both TEST.
PS Error message
Any ideas what may be causing this?
0
 
LVL 24

Expert Comment

by:VB ITS
ID: 40479512
Did you modify the PowerShell script in Step 2 at all? Did you run it as an Administrator?
0
 

Author Comment

by:breichard
ID: 40479527
<slaps forehead> Ok, I feel like an idiot.  I didn't run the command prompt as an administrator.  The basics.  Thanks so much.  I'm going to do some testing before I close this out.
0
 

Author Comment

by:breichard
ID: 40479532
Do you know if the PS script would return an errorlevel (or can it be modified to do so)? I would like to test to make sure it was successful before proceeding with the copy.
0
 
LVL 24

Expert Comment

by:VB ITS
ID: 40479673
It's probably easier if you add in a pause between the PowerShell shutdown command and the copy command, then verify the size of the VHD file as errorlevels in PowerShell can be a bit tricky to work out.
0
 

Author Closing Comment

by:breichard
ID: 40480035
This is exactly what I needed.  Thanks VB ITS.
Bob
0
 
LVL 24

Expert Comment

by:VB ITS
ID: 40480038
Good to hear you got it all working. Glad I was of help.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

828 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