ltstudiooo - Fotolia

Tip

Prepare a Hyper-V configuration report with PowerShell

Generate a report with valuable Hyper-V configuration information, such as the virtual disk path, using a PowerShell script to ensure your server settings match the design documentation.

You can use Hyper-V Manager and System Center Virtual Machine Manager to check the configuration of Hyper-V servers one by one, but it would take a considerable amount of time. Luckily, there is a PowerShell script you can use to prepare a Hyper-V configuration report.

If you run several Hyper-V servers in a production environment, you must perform regular checks to ensure the servers are configured with the required settings. Before you check the configuration settings of the Hyper-V servers, you must decide what is important to include in the report. Note that we are only interested in building a Hyper-V configuration report for the host. The report doesn't include any VM configuration-specific information.

You might want to check whether the live migration settings on the Hyper-V servers are properly configured. Similarly, you might want to check the location of the VM configuration files, VM hard disk files, Hyper-V non-uniform memory access (NUMA) status, and the maximum number of storage and live migrations.

You must meet a few requirements before you execute the PowerShell script.

Prepare a Hyper-V text file. First, create a text file that includes the Hyper-V server names. The file should contain one Hyper-V server name per line, as shown in Figure A. Assume the file is called HServers.txt.

Hyper-V server names
Figure A. Text file showing Hyper-V server names

Ensure Hyper-V servers are reachable. Next, make sure the network can reach the production Hyper-V servers that are specified in the above text file.

Create a Hyper-V report folder. The PowerShell script below generates a Hyper-V configuration report under the C:\Hyper-VReport folder; create the folder before executing the script.

Execute the PowerShell script with elevated privileges. You must have administrative rights for each Hyper-V host to collect data and generate a report.

Run the PowerShell script using Get-VMHost

The below PowerShell script uses only one PowerShell cmdlet: Get-VMHost. When you run Get-VMHost on a stand-alone Hyper-V host, it lists all the configuration settings for the Hyper-V host in the PowerShell window.

Before executing the PowerShell script, copy the HServers.txt file to the C:\HyperVReport folder on the local system where you are executing the script.

$ReportFile = "C:\HyperVReport\HyperVReport.CSV"
Remove-item $ReportFile -ErrorAction SilentlyContinue
$STR = "Hyper-V Server, Virtual Disk Path, Virtual Machine Path, Maximum Storage Migrations, Maximum Live Migrations, Is Live Migration Enabled?, Authentication Type, Use Any Network for Migration?, Logical Processor Count, Memory Capacity, Numa Enabled?"
Add-Content $ReportFile $STR
$HyperVServerFile = "C:\HyperVReport\HServers.TXT"

Foreach ($ThisHyperVHost in Get-Content $HyperVServerFile)
{   

$ThisHost = $ThisHyperVHost

$Error.Clear()
$AllHProp = Get-VMHost -ComputerName $ThisHost
IF ( $Error.Count() -eq 0)
{

$VDiskPath = $AllHProp.VirtualHardDiskPath
$VVMPath = $AllHProp.VirtualMachinePath
$VMMaxMigration = $AllHProp.MaximumStorageMigrations
$VMMaxLiveMigration = $AllHProp.MaximumVirtualMachineMigrations
$IsLiveEnabled = $AllHProp.VirtualMachineMigrationEnabled
$LiveAuthType = $AllHProp.VirtualMachineMigrationAuthenticationType
$UseAnyNetforMigration = $AllHProp.UseAnyNetworkForMigration
$LogicalProcCount = $AllHProp.LogicalProcessorCount
$HMemCapacity = $AllHProp.MemoryCapacity
$HNumaStatus = $AllHProp.NumaSpanningEnabled

$STR =

$ThisHost+","+$VDiskPath+","+$VVMPath+","+$VMMaxMigration+","+$VMMaxLiveMigration+",
"+$IsLiveEnabled+","+$LiveAuthType+","+$UseAnyNetforMigration+","+$LogicalProcCount+",
"+$HMemCapacity+","+$HNumaStatus

Add-Content $ReportFile $STR

}
else
{

$STR = $ThisHost+", Error connecting to Hyper-V Host."
Add-Content $ReportFile $STR

}  

}

Write-Host "Script finished creating report file for Hyper-V configuration and can be found under C:\HyperVReport folder!"

Once the PowerShell script has finished executing against all the servers specified in the C:\HyperVReport\HServers.txt file, a Hyper-V configuration report file will be generated in the C:\HyperVReport folder on the local machine.

The report file includes the Hyper-V host name, VM disks path, VM configuration files path, the maximum number of storage migrations and live migrations configured on the Hyper-V host, the authentication mechanism used by live migration, the logical processors count, the memory capacity, and the Hyper-V NUMA status. A snippet of the report is shown in Figure B.

Hyper-V configuration report
Figure B. Hyper-V configuration report generated by the PowerShell script

I recommend executing the above PowerShell script every week or so just to ensure your Hyper-V servers are configured with the standard settings and that they match the Hyper-V design documentation.

Dig Deeper on IT systems management and monitoring