alphaspirit - Fotolia


Generate a Hyper-V report to track clusters, nodes with PowerShell

It's difficult to navigate all the information related to Hyper-V nodes and clusters, so use PowerShell commands and scripts to generate reports and automate information retrieval.

Information retrieval across numerous Hyper-V clusters and nodes can be tedious, but PowerShell commands and scripts make it easy to generate a Hyper-V report that contains all the information you need.

You can use PowerShell to perform several types of operational tasks in Hyper-V, such as checking the Hyper-V replication status between Hyper-V primary and secondary servers. You can also use PowerShell to interact with and manage Hyper-V clusters.

Follow the steps below to generate a Hyper-V report that retrieves information from the nodes that host VM resources in a Hyper-V cluster and to verify that the nodes in a Hyper-V cluster are hosting the VMs they're supposed to.

First, use the Get-ClusterNode PowerShell cmdlet to list the VMs each Hyper-V cluster node hosts. To list VMs from a single Hyper-V node, enter the command below.

Get-ClusterNode -Name <NodeName> | Get-ClusterResource

This command targets VMs on a single Hyper-V cluster node, but you need a more advanced script to retrieve more information.

Collect all the cluster information for a Hyper-V report

The below PowerShell script collects the same information from all the cluster nodes in a particular Hyper-V cluster. All you need to do is modify the cluster name in the $HVCluster variable before you execute the script.

Script to target cluster nodes
Figure A. Retrieve cluster node information with this PowerShell script.

The above script collects all the nodes from PRODCLUSTER, which the script specifies in the $HVCluster variable; executes the Get-ClusterNode command against each retrieved node; and generates a Hyper-V report in C:\Temp\VMsPerNode.CSV. This Hyper-V report contains the names of the nodes and the names of the VMs each node hosts.

Hyper-V node report
Figure B. Generate a Hyper-V report with a PowerShell script.

Target Hyper-V clusters by text file

The script above collects the VMs each node hosts in a single Hyper-V cluster. With the next script, you can target a list of Hyper-V clusters specified in a text file and then collect a Hyper-V report for each cluster.

Target clusters from a text file
Figure C. Target a list of Hyper-V clusters from a text file with a PowerShell script.

The above script collects each Hyper-V cluster that C:\Temp\HVClust.TXT specifies, executes the Get-ClusterNode PowerShell cmdlet for each node in the Hyper-V cluster in question and then adds the result to the C:\Temp\VMsPerNode.CSV file.

Hyper-V report with cluster names
Figure D. Use a PowerShell script to collect cluster names and add them to the Hyper-V report.

Use a Hyper-V report to verify production VMs

The two PowerShell scripts above collect Hyper-V cluster nodes and the VMs they host and then generate a Hyper-V report based on that information. This process is tedious if you must also verify that the production VMs are running on the right Hyper-V nodes in a Hyper-V cluster.

For example, if you planned to run only three VMs on Node1 during capacity planning and Node1 ends up hosting an extra VM, it might affect the performance of the other VMs that Node1 hosts. 

A third PowerShell scriptcan resolve this. The script in Figure F uses a predefined file called C:\Temp\VMNode.CSV, shown below, which contains the Hyper-V node and VM mapping.

Hyper-V node and VM file
Figure E. Examine the file that contains the Hyper-V node and VM name mapping information.

Once you have the VMNode.CSV file ready, the PowerShell script below executes against a specified Hyper-V cluster and verifies that the VMs are running on the correct Hyper-V nodes.

Script that verifies VMs
Figure F. Use a PowerShell script to verify the VMs are running on the correct nodes.

After this script finishes, the resulting file should resemble Figure G. This Hyper-V report includes node names, VM names and statuses that indicate whether the VMs are running on the correct Hyper-V node or not.

Hyper-V report results
Figure G. Generate a Hyper-V report that contains the node names, VM names and statuses.

With PowerShell, you can use commands and scripts to automate information retrieval. By tracking Hyper-V cluster and node information, you can ensure VMs remain properly configured once you deploy them in production.

Dig Deeper on IT systems management and monitoring

Software Quality
App Architecture
Cloud Computing
Data Center