This report gathers information about a Hyper-V server and the VMs running on that server.
$ReportHT = [Ordered] @{}
$HostDetails = Get-CimInstance -ClassName Win32_ComputerSystem $ReportHT.HostName = $HostDetails.Name $ReportHT.Maker = $HostDetails.Manufacturer $ReportHT.Model = $HostDetails.Model
$ReportHT.PSVersion = $PSVersionTable.PSVersion.ToString()
$OS = Get-CimInstance -Class Win32_OperatingSystem $ReportHT.OSEdition = $OS.Caption $ReportHT.OSArch = $OS.OSArchitecture $ReportHT.OSLang = $OS.OSLanguage $ReportHT.LastBootTime = $os.LastBootUpTime $Now = Get-Date $UTD = [float] ("{0:n3}" -f (($Now – $OS.LastBootUpTime).Totaldays)) $ReportHT.UpTimeDays = $UTD
$PHT = @{ ClassName = 'MSvm_Processor' Namespace = 'Root/Virtualization/v2' } $Proc = Get-CimInstance @PHT $ReportHT.CPUCount = ($Proc | Where-Object ElementName -Match 'Logical Processor').Count
$Cname = '\.processor(_total)\% processor time' $CPU = Get-Counter -Counter $Cname $ReportHT.HostCPUUsage = $CPU.CounterSamples.CookedValue
$Memory = Get-Ciminstance -Class Win32_ComputerSystem $HostMemory = [float]("{0:n2}" -f ($Memory.TotalPhysicalMemory/1GB)) $ReportHT.HostMemoryGB = $HostMemory
$Sum = 0 Get-VM | Foreach-Object {$sum += $_.MemoryAssigned + $Total} $Sum = [float] ( "{0:N2}" -f ($Sum/1gb) ) $ReportHT.AllocatedMemoryGB = $Sum
$Reportobj = New-Object -TypeName PSObject -Property $ReportHT
$Report = "Hyper-V Report for: $(hostname)`n" $Report += "At: [$(Get-Date)]"
$Report += $Reportobj | Out-String
$VMs = Get-VM -Name * $VMHT = @() # to be an array of hash tables
Foreach ($VM in $VMs) { # Create VM Report hash table for this VM $VMReport = [ordered] @{} # Add VM's Name $VMReport.VMName = $VM.VMName # Add Status $VMReport.Status = $VM.Status # Add current VM uptime $VMReport.Uptime = $VM.Uptime # Add VM CPU $VMReport.VMCPU = $VM.CPUUsage # Replication mode and status $VMReport.ReplMode = $VM.ReplicationMode $VMReport.ReplState = $Vm.ReplicationState # Create an object from Hash table and add to array $VMR = New-Object -TypeName PSObject -Property $VMReport $VMHT += $VMR }
$Report += $VMHT | Format-Table | Out-String
$Report
This recipe creates a report that describes both the Hyper-V host and the VM on the host. In the first part, you build a hash table containing information about the host itself which you add to the report. Then, you report on the VMs running on this server (HV1
).
In step 1, you create an ordered hash table ($ReportHT
). You create an ordered hash table so that the order of rows is maintained. With step 2, you add basic host details to the hash table.
In step 3, you add details of the PowerShell version running on the host. With step 4, you add operating system details to the hash table along with (in step 5) a count of processors available in the host. Then you add current host CPU usage in step 6, and total host physical memory in step 7. In step 8, you calculate the total amount of memory assigned to VMs and add it to the hash table.
In step 9, you create a summary report object with properties coming from the hash table. In step 10, you create a basic report header to which, in step 11, you convert the report object to a string and add it the report.
With step 12 and step 13, you retrieve details of each of the VMs assigned to HV1
, and with step 14, you add those details to the report.
Finally, in step 15, you view the report, which looks like this:
In this recipe, you create and use two hash tables. The first hash table holds details about the VM host overall. If you were reporting on multiple Hyper-V servers, you could create hash tables relating to each server and then convert those two hash tables into objects to report on. If you are going to expand this recipe to cover multiple servers, using objects to report from is easier.
This recipe chose some basic performance and usage information to report. As ever with performance analysis, you could add more. For example, you could add network throughput, storage usage information to the report header. And you could expand the information reported on each VM to include more details. An important thing to consider is when to stop adding details to your reports.
18.188.139.172