There are many ways to use PowerShell for VMware management, so it seems only natural to use PowerShell for Hyper-V...
management as well. But Microsoft doesn't offer much in the way of PowerShell commands (or cmdlets) for Hyper-V.
If you run Windows Server 2008 R2, however, you can use a freeware library of PowerShell cmdlets to monitor and manage Hyper-V much as you would VMware. Known as the PowerShell Module for Hyper-V, this library includes cmdlets for monitoring virtual machine (VM) states, retrieving hardware configurations and acquiring other VM data. With these Hyper-V management shortcuts, you can also check on VM health, memory capacity and even functionality.
Installing and testing PowerShell for Hyper-V management
The PowerShell module includes an installer, but there are a few installation requirements. You must have version 2 of the .NET Framework and enable Windows PowerShell on your server. Also note that the installer uses PowerShell scripts. As such, you can complete the installation process only if you set PowerShell's execution policy to unrestricted (which Microsoft does not generally recommend). You can modify the policy by entering the Set-ExecutionPolicy Unrestricted cmdlet. If you prefer not to change your server's execution policy, you can manually register the various components.
Once you install the PowerShell Module, check whether your user account has administrative privileges on the server. Without these permissions, some Hyper-V management cmdlets may not work. To check permission level, enter the Test-Admin cmdlet. Figure 1 shows that Windows returns a simple true-or-false notification to indicate whether you have administrative privileges.
Monitoring the state of a VM
PowerShell can monitor the state of multiple VMs running on a Hyper-V server. Use the following cmdlets to retrieve VM statistics.
Get-VM. Windows returns the names of each VM installed on a server as well as basic state and uptime information for each VM.
Get-VMSummary. Enter this cmdlet, followed by the name of the machine you want to check, to get more detailed information about a specific VM. Windows returns quite a bit of information, including the number of virtual CPUs the VM uses, the CPU load history, the VM's operating system, when the VM was created and the VM's fully qualified domain name (see figure 2).
Testing VM functionality
You might simply want to know if a VM is functional at a given moment. In this situation, use the Test-VMHeartbeat cmdlet to see whether a VM is responsive. Windows returns an OK status to indicate that a specified machine still has a heartbeat (see figure 3).
Checking hardware configurations
Numerous cmdlets can retrieve hardware resource allocations for VMs. The following hardware-specific cmdlets helps improve Hyper-V management.
Get-VMSettingData. Windows returns detailed information on VM hardware settings. In figure 4, I formatted the output as a list. Otherwise, the output would be truncated. If you do not specify a VM, the cmdlet provides hardware configuration data for every VM on the server. To supplement your Hyper-V management strategy, you can export this information to a CSV file and work with it in Excel.
Get-VMMemory. This cmdlet provides the amount of memory that's reserved for each VM (see figure 5). This is handy for resource planning, because if you want to add VMs to a Hyper-V host, you need to know how much host memory has been reserved.
Get-VMCPUCount. Use this cmdlet to identify how many CPU cores have been assigned to a VM.
Get-VMProcessor. This cmdlet retrieves performance information rather than configuration information. Figure 6 shows the overall CPU load for the functioning VMs.
Remember, the PowerShell Module is a full-blown Hyper-V management solution. It can do far more than what I outlined above, so I encourage you to download the module and see for yourself exactly what's possible. The PowerShell cmdlets in this library can improve Hyper-V management in your infrastructure.
|Brien M. Posey, MCSE, has received Microsoft's Most Valuable Professional Award seven times for his work with Windows Server, IIS and Exchange Server. He has served as the CIO for a nationwide chain of hospitals and healthcare facilities, and was once a network administrator for Fort Knox. You can visit his personal website at www.brienposey.com.|