ltstudiooo - Fotolia
Bulk manage a Hyper-V server group with PowerShell
With only a few steps, you can use SCVMM to bring a Hyper-V server under management. This is limited, however, so use the SCVMM Command Shell to bulk manage Hyper-V servers.
Microsoft makes it relatively easy to bring a new Hyper-V server under management in System Center Virtual Machine Manager. With a few clicks, the Virtual Machine Manager can enable the addition of Hyper-V hosts and clusters with an easy-to-follow wizard.
If, however, you want to add Hyper-V hosts at a larger scale in order to bulk manage a Hyper-V server group, it's better to skip the GUI and use the Virtual Machine Manager Command Shell instead.
Use the Add Resource Wizard
To bring a new Hyper-V server under management, log in to the Virtual Machine Manager (VMM) console, and then go to the VMs and Services workspace. From there, right-click on the desired host group, and then select the Add Hyper-V Hosts and Clusters command from the shortcut menu, as shown in Figure A. This prompts VMM to launch the Add Resource Wizard, which will guide you through the addition of the new host.
As simple as the process to manage a Hyper-V server might be, it's not scalable. If you only need to add a few Hyper-V hosts, the GUI works fine, but what if you need to bring hundreds, or even thousands of new hosts under management? In this situation, you'd be better off using the VMM Command Shell to add and manage a Hyper-V server group of that size.
Use the VMM Command Shell to manage a Hyper-V server
Add-SCVMHost is the cmdlet that brings a Hyper-V host under management. As a side note, you can use the Add-SCVMHostCluster cmdlet to bring a Hyper-V host cluster under management, which also works for adding VMware ESX host clusters.
The command syntax for the Add-SCVMHost cmdlet is fairly complicated, and its usage varies depending on the host that you want to add. For example, you would use the cmdlet in one way if you wanted to add a domain-joined Hyper-V host, but you'd need to use it another way if you were to add a host from a nontrusted domain. In this case, we'll refer to domain-joined hosts.
Capture the Run As account's credentials
If you've ever used the VMM GUI to add a host to VMM, then you know that doing so requires the use of a Run As account. Using PowerShell doesn't circumvent the need to use a Run As account, so you'll need to begin by capturing the Run As account's credentials. The easiest way to do this is to create a variable -- $RunAs, for example -- and use it to store the Run As account.
You might have seen PowerShell scripts that capture credentials with the Get-Credential cmdlet. When it comes to the Run As account, however, we don't need to get the account credentials.
System Center has a built-in command called Get-SCRunAsAccount that can capture the Run As account. This command looks like this:
$RunAs=Get-SCRunAsAccount -Name <account name>
If you don't know the name of your Run As account, you can find it in the VMM console. Go to the Settings workspace, and then click Run As Accounts. As you can see in Figure B, my Run As account is called RunAs. In this case, I would use the command:
$RunAs=Get-SCRunAsAccount -Name "RunAs"
You can see what this command looks like in Figure C. Following the command, I have output the contents of the $RunAs variable so you can see its contents.
Add the host
Once the $RunAs variable is set up, adding the host is relatively straightforward. Here is what the command looks like:
Add-SCVMHost <host FQDN> -Description <a description of the host> -RemoteConnectEnabled $True -RemoteConnectPort 5900 -Credential $RunAs
If I have a host named Hyper-V-1.poseylabs.com and I want to add it to VMM and have the ability to remote connect, the command I would use is:
Add-SCVMHost "Hyper-V-1.poseylabs.com" -Description "my Hyper-V server" -RemoteConnectEnabled $True -RemoteConnectPort 5900 -Credential $RunAs
Despite the fact that the Add-SCVMHost cmdlet is relatively easy to use, it might not seem like an efficient way to bulk add a large number of hosts. If you have a lot of hosts to add, then you can build a PowerShell script that reads the host names from a comma-separated values file, and then uses a loop to add the hosts one at a time. PowerShell is an essential tool to manage a Hyper-V server group at a large scale.