Showing posts with label Hyper-V. Show all posts
Showing posts with label Hyper-V. Show all posts

Friday, October 05, 2012

How to start/stop Hyper-V machines with PowerShell

Being a Hyper-V fan, I try to keep all of my VM’s named and line up correctly.  The problem is that with so many versions of SharePoint, SQL, OS.  The management of those VM’s can get a bit tricky.  For instance, I have 3 SharePoint farms that I might need to run at one point or another.  The problem that I had, was that I wanted to start a farm or a set of vm’s with a single script.  Well, trying to start up all of your vm’s at once creates a problem when you try to start a SharePoint farm.

The dependencies for SharePoint is that the SQL server needs to be up and running as well as the Domain controller.  For the SQL Server to start, it needs the Domain controller to be up and running.  So somehow, you need to start the AD, wait for it to be up and running before starting the SQL, and then finally when both are running, then you can start your SharePoint server.  This sequence does not allow you to get a cup of coffee while all of the vm’s are starting.  You need to monitor the state of each server before  moving to the next one.

The workflow will be something like this:  AD –>  SQL –> SharePoint

Now, enter PowerShell.  With PowerShell, I have several scripts that will perform this workflow correctly.  If you are still running Windows Server 2008-R2 as your desktop machine (and who isn't, right?), you can download the Powershell Management Library for Hyper-V in Codeplex.  This module is necessary, even if you are running PowerShell v3.

hyperV

Here is my script to start up the VM machines.  Noticed that I’m using the HearBeatTimeOut.  This parameter waits for the OS to be up and running before the next command is executed!
Import-Module "C:\Program Files\modules\HyperV\HyperV.psd1"

start-vm "DemoAD" -HeartBeatTimeOut 300
start-vm "DemoSQL2008" -HeartBeatTimeOut 300
start-vm "DemoWFE1" -HeartBeatTimeOut 300

and the script to stop them.  It is always nice to use the reason parameter. This parameter is needed so that I don’t have to explain WHY it was shutdown when starting the VM’s.
Import-Module "C:\Program Files\modules\HyperV\HyperV.psd1"

shutdown-vm "DemoWFE1" -force -reason "end of day"
shutdown-vm "DemoSQL2008" -force -reason "end of day"
shutdown-vm "DemoAD" -force -reason "end of day"

now, where is that cup of coffee… :)

Wednesday, December 03, 2008

How to get Hyper-V to read corrupted VHD files

We have a lots of projects that are virtualized.  When we finish with a project it is common practice to de-hydrate the servers and put them away.  For one of our customer's project, we followed this process over a year ago.  Lots of things have changed in the past year. First, we moved from one building to a different building.  On the process, we added new servers, clean up some old AD's, change OS's (Windows 2008 DataCenter... oh yeah.!), etc...

Now trying to hydrate back online this one project's environment with all of the infrastructure changed was a challenge. For this enterprise application we have Oracle, App Servers, Web Servers, Test Servers, etc.

Out of all of the VHD's that we needed to bring back, only 2 were recognized by Hyper-V.! Doing a check on the rest of those VHD's gave me the file or directory is corrupted and readable message... :(

image

This is how to check for integrity using the Hyper-V. Add the vhd file to the IDE controller and then hit the Inspect button.

image 

[Main Instruction]
An error occurred when attempting to retrieve the virtual hard disk "F:\hydrate\Project1-Agent.vhd" on server XXX.

[Content]
The file or directory is corrupted and unreadable.

At this point, if we try to recreate all of the settings/configurations stored on those servers will take days.! Some entries in the TechNet forums mentioned that they got it working by re-opening the files on VirtualPC. This is the way I got my corrupted vhd's to work:

First, I create a new Virtual Machine with a new HD.  Then went to Settings|Hard Disk 1 and loaded this corrupted vhd file.

image

There you will find the Virtual Disk Wizard. Click on it, go through the prompts and select the option to Edit an existing virtual disk.

Then choose to Compact it.  The reason for this, is that if you select the Convert it to a fixed-size virtual hard disk, the wizard will make the new hard disk EXACTLY the same size of what content is written at the moment in it.  Which means, if you have allocated 20GB to this disk initially, and have used 12GB of it.  Now instead of having a 20GB with 8GB free, you ended with a fixed 12GB hard drive with NO SPACE LEFT on it.

image

On the next screen, I save the file under a different name for precaution. Now click next and wait. It will take about 1hr for a 28GB file.  Once this process completed, I copied the new file back to our Windows 2008 server, load it on Hyper-V and it was recognized.!

Success.