Tuesday, November 17, 2009

Deleting a Hyper-V Virtual Switch using WMI and Powershell

For those of you who don’t know, I work for a company called Crutchfield who is an online and mail order consumer electronics retailer providing products for the home and car. It is that time of year again when we are very busy finishing up all systems and application work that is necessary to support our sales operations during the holiday season. This year, one of our final tasks is to complete the upgrades to our nine Hyper-V hosts to Windows Server 2008 R2 and Hyper-V Server 2008 R2.

Last week, we performed upgrades on our Hyper-V Server 2008 hosts to R2 that serve our Test\Dev environments. The upgrades were very straight forward and uneventful, which is what anyone hopes for when performing any upgrade. However, after the upgrade was complete, it appeared that either a virtual switch had either lost its configuration or failed. It turned out that it was one of those problematic Broadcom NIC’s that we all love so much that failed. So, I unbound the virtual switch that was configured to use the NIC from all VM’s and attempted to delete the switch. I was complimented with a nice error message, informing me that it couldn’t be deleted. So, I did what any good admin would do when the GUI fails us and turned to my trusty command line.

So, when administering Hyper-V using powershell, I use the Hyper-V library that is available from codeplex. Unfortunately, the powershell library doesn’t provide a cmdlet for deleting a virtual switch, so I leveraged a WMI class called “MSVM_VirtualSwitchManagementService”. Then, I used the “Choose-VMSwitch” powershell cmdlet to select the virtual switch for deletion and then deleted the switch. I searched online and found plenty of info on creating a virtual switch using powershell, but nothing regarding how to delete it, so I will walk you through deleting a virtual switch I created called “Demo Virtual Switch”.

In the steps that follow I will show you how to create the WMI object reference, select the virtual switch and finally deleted the selected switch. For this demo I am running the scripts local to the host with the virtual switch that I want to delete. With that said, let’s get started!

1. You need to start Powershell and set the execution policy to unrestricted to allow the scripts that follow to execute.

Set-ExecutionPolicy Unrestricted

2. Load the Hyper-V powershell library if it hasn’t already been done.

3. Now, like I said before, there isn’t a Powershell cmdlet for deleting a virtual switch. So, we will need to create an object reference to the WMI Class “MSVM_VirtualSwitchManagementService” so the switch can be deleted. Use the following syntax.

$HVSwitchObj = Get-WMIObject –class “MSVM_VirtualSwitchManagementService” –namespace “root\virtualization” –computer “.”


4. Now, we need to select the virtual switch that we want to delete. There is a powershell cmdlet called Choose-VMSwitch that will allow us to make this selection. The following syntax will provide a list of all the virtual switches that exist on the host and provide the ability to select the switch that is to be deleted. Enter your switch selection by typing the associated ID number from the list and hit enter.

$HVSwitch = Choose-VMSwitch

5. Finally, we delete the switch. The final syntax uses the WMI class to delete the virtual switch selected by the powershell cmdlet.

$HVSwitchObj.DeleteSwitch($HVSwitch)


If you now pull up "Hyper-V Manager" and select “Virtual Network Manager”, you will see that the virtual switch has been removed and problem resolved. At this point I was able to replace my failed NIC, create a new virtual switch and bind the new switch to my virtual machines.

I hope this helps!

Thursday, November 5, 2009

Guide to Upgrading to Virtual Machine Manager 2008 R2

It has been several months now since the release of System Center Virtual Machine Manager 2008 R2, but it has been my experience that folks haven't been rushing to upgrade their production environments. However, with the recent release of Windows Server 2008 R2 and the requirement to manage all the new Hyper-V features, I have been very busy helping folks with their upgrade task to VMM 2008 R2. Microsoft did a great job on creating a very clean upgrade with little post installation steps required.

In the steps that follow, I will be focusing on the in-place upgrade from VMM 2008 RTM to VMM 2008 R2 RTM. We will be looking at this in three parts, Pre-Upgrade, Upgrade and Post-Upgrade Tasks.

Pre-Upgrade:
1. Make sure that any jobs that are currently running, have completed before attempting the upgrade. All jobs that ran prior to the upgrade will appear in the job history, but can not be restarted.

2. Make a back up of the VMM database to insure you can restore VMM 2008 in the event of a failure during the upgrade. VMM includes the built-in ability to perform this backup. Go to the "Administration" View and then select "General" or "Managed Computers" and on the right side of the screen you will see an option for "Backup Virtual Machine Manager". Select this option and you will be prompted to provide a path to store the backup.














Upgrade:
3. On the server hosting VMM, run setup.exe with "ELEVATED PRIVILEGES" from the VMM 2008 R2 installer location. The setup will start with a splash screen presenting options to install different components including (VMM Server, VMM Admin Console, Self-Service Portal, Local Agent and Configure Operations Manager.
















If you select any of the options that are currently installed, then the installer will automatically detect those bits and then proceed by asking if you would like to upgrade those components. If you select yes, then the install will start the upgrade process. Should you choose one of the options not currently installed, exp. PRO or Self-Service Portal, then the upgrade will proceed with upgrading the currently installed components and once completed, it will install the additional feature(s).

After the upgrade is complete and the server has been rebooted you will have to perform a few manual steps to insure that your host and library servers are properly communicating with the new VMM 2008 R2 instance. Part of the upgrade to VMM 2008 R2 was an update to the VMM database and this requires an update to the agents to insure the hosts and libraries can be properly managed from VMM.

4. Start the VMM Admin Console and open the "Hosts" view. Under "All Hosts" you will see that all hosts and libraries are now reporting a status of "Needs Attention". If your agents haven't been updated and you don't see this message, then you can do a couple of things. First, you can wait for the normal host refresh window to occur that will update the status. Second, you can Right-Click on the host and select "Refresh" forcing the host agent to report to the VMM server.












5. Now that the agents are reporting a status "Needs Attention" you need to update the agent. In order to update the agent you can do this two ways. First, you can Right-Click your hosts while in "Hosts" view and select "Update Agent". Second, you can go to "Administration" view and select "Managed Computers", then Right-Click your hosts and select "Update Agent". You can select multiple hosts at a time for agent updates, but I generally limit this to 10-15 hosts at a time. After a few moments the job will complete and the agent will be updated with a status of "OK" letting you know the agent version is consistent with the database.













6. Finally, you will need to repeat these steps on any additional VMM 2008 servers you may have and you will need to update any VMM Admin Consoles with the R2 version.

So, thats it, upgrade complete!

If you need additional information regarding Virtual Machine Manager 2008 or Virtual Machine Manager 2008 R2, then please refer to the VMM TechNet site that provides lots of great information on VMM Planning, Deployment, Operations and Security.

http://technet.microsoft.com/en-us/library/cc917964.aspx