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.
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.
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.
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.
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.
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.
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.
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.