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

x
?
Solved

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

Posted on 2014-12-02
15
Medium Priority
?
310 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
[X]
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
  • 7
  • 6
  • 2
15 Comments
 
LVL 5

Expert Comment

by:A Karelin
ID: 40477889
0
 
LVL 24

Accepted Solution

by:
VB ITS earned 2000 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
Optimized for private cloud infrastructures and datacenters, Nano Server is minimalistic, yet super-efficient, OS for services such as Hyper-V and Hyper-V cluster. Learn how you can easily deploy Nano Server and unlock its power!
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

715 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