Adding Windows Server 2012 Hyper-V hosts to SCVMM the easy way

If you're looking to speed up a deployment and add new Hyper-V hosts faster, ditch the graphical tool and use PowerShell to automate the process.

Adding new Hyper-V hosts to System Center Virtual Machine Manager (SCVMM) is a fairly straightforward process with the graphical tool. The graphical user interface (GUI) will quickly add the host, install the agent and install Hyper-V if it's not already on the host. What more can you ask for? Automation. For new implementations with several new hosts and for quick disaster recovery, I prefer to use PowerShell and the Virtual Machine Manager (VMM) module to add Hyper-V hosts to SCVMM. It's quicker than the GUI, it's easily automated and I don't need to install the GUI tools.

What you need before you start

First, you need to import cmdlets from the Virtual Machine Manager module. I don't normally have the SCVMM management tools installed on my client -- as a consultant I never know what computer I'm going to use. So, I implicitly remote them to my client from the SCVMM server. This way I don't need to install anything and I can get to work. My previous article describes how to import VMM cmdlets, but the commands below provide a quick review:

PS C:\> $session=New-PSSession -ComputerName SCVMM1

PS C:\> Invoke-Command –Session $Session {Import-Module –Name VirtualMachineManager}

PS C:\> Import-PSSession Session $session -Module VirtualMachineManager

If you already have the management tools installed locally, you need to perform one step before you can use the SCVMM cmdlets. Version 3 of PowerShell dynamically imports most modules the first time you launch a cmdlet from that module. The Virtual Machine Manager module isn't one of them, so import the module yourself using the PowerShell command below before you attempt to run SCVMM cmdlets:

PS C:\> Import-Module -Name VirtualMachineManager

With the cmdlets in hand, you're ready to easily add -- or automate the process of adding -- new Hyper-V hosts to SCVMM.

Adding a new Hyper-V Host

The best-performing Hyper-V host is one running Server Core on Windows Server 2012. However, the following example works regardless of whether you're using Server Core or the GUI-based operating system. The Add-SCVHost cmdlet will perform all the work for you, including installing Hyper-V if it isn't already installed on the host.

Start by creating a variable that contains the server names of the hosts. You can fill this variable from a .csv file, a .txt file or even directly from Active Directory. In the following example, I typed the server names directly into the variable. One more thing -- one of my servers has Hyper-V installed, while the other does not. The Add-SCVHost cmdlet will have no problem figuring this out and handling the situation.

PS C:\> $Servers= 'SCHyperv', 'SCHyperv2'

To add the Hyper-V hosts to SCVMM, you will use the Add-SCVHost cmdlet. This cmdlet has the parameter –ComputerName for the hostname of the new Hyper-V host. The problem is that the parameter only takes a single value. In other words, just trying to use the cmdlet by itself won't let you install multiple Hyper-V hosts simultaneously. I'm going to show you how to get around that by using the pipeline and the Foreach-Object cmdlet:

PS C:\> $Servers | Foreach-Object{Add-SCVMHost -computername "$_" -Credential (Get-SCRunAsAccount -Name "VMMRunAs") -RunAsynchronously}

The Foreach-Object cmdlet will run Add-SCVHost for every host name that is sent across the pipeline. The special variable $_ is the placeholder for the individual host name.

Take notice of the –Credential parameter. Like the GUI management tool, you're required to specify the RunAs account to install the new host. The cmdlet Get-SCRunAsAccount will import this so that the installation can continue without error.

That's all there is to it! Soon you will have new Hyper-V hosts added to SCVMM and ready for virtual machines. Let me show how to check on the progress of the installation.

Checking the installation progress

Most people normally check the running job status in the graphical manager, but you can use the Get-SCJob cmdlet to provide the same information. This is very useful for automation scripts when you need to verify that the installation is complete before continuing the script. The following example and Figure 1 below displays the current running jobs and their current progress.

PS C:\> Get-SCJob | Where{$_.status -eq "running"} | Select-Object -Property ResultName, Progress

Check the status of the currently running jobs with the Get-SCJob cmdlet.

Figure 1. Checking the status of the currently running jobs.

You will want to check for completed jobs to find out when the installation is finished:

PS C:\> Get-SCJob | Where{$_.status -like "*complete*"} | Select-Object -Property ResultName, Progress

When the hosts have been added to SCVMM, I like to verify that they are working properly before I start to deploy VMs. You check the status of a host using the Get-SCVMHost cmdlet:

PS C:\> Get-SCVMHost | Select-Object -Property ManagedComputer, VirtualServerState, HyperVState, CommunicationState

Check the status of a host and ensure the job was completed using the Get-SCVMHost cmdlet.

Figure 2. Verifying the completed installation.

Using PowerShell and the VirtualMachineManager cmdlets will make scripting and automating the process of adding new hosts a breeze in the future.

Troubleshooting problems

I have run across a few issues adding Hyper-V hosts in the past. Here are a few quick pointers for dealing with problems if they arise.

You might receive a job error that describes a problem in which the host can't be added to SCVMM. There are two common causes for this, and you will want to check on them during your troubleshooting. The most common issue is with the local Windows firewall settings on the Hyper-V host.

Hyper-V hosts need to have the "File and Printer Sharing" ports open on the firewall for a successful installation. You can issue this firewall setting using a Group Policy, then wait for it to take effect, but I find it easier and faster to open them with PowerShell using the following command:

 PS C:\> Invoke-Command -ComputerName $Servers {Enable-NetFirewallRule -DisplayGroup *file*}

Another common problem I have seen is specific to adding Server Core as a new Hyper-V host. By default, Server Core doesn't include the feature for "File Server." In the past I have had to install this component, but this problem seems to have diminished with SCVMM SP1 and Server Core on Windows Server 2012. Just in case you think this is the issue, here is a fast way to install the component:

PS C:\> Invoke-Command -ComputerName $Servers {add-WindowsFeature FS-FileServer}

One last note: As you retire Hyper-V hosts you no longer need -- or if you need to troubleshoot a problematic one -- you can easily remove them from SCVMM using Remove-SCVMHost:

PS C:\> Remove-SCVMHost -VMHost SCHyperv -Credential (Get-SCRunAsAccount -Name "VMMRunAs")

Try using PowerShell and the Virtual Machine Manager cmdlets the next time you perform a management task. You will be amazed at all the cmdlet support available for SCVMM.

Dig Deeper on Virtual machine provisioning and configuration