Collecting Hyper-V information using a PowerShell script

Spread the words

Collecting Hyper-V infrastructure information is useful when you do an audit or even for day to day administration. The easiest way to do that is to use scripts.
Here is a way to collect the information using PowerShell. We can get these pieces of information using the Get-Cluster, Get-VM or any other Hyper-V related Cmdlets. I’m just sending them into an html file.

Here is the full script to download

When you run Get-VM | select * you’re getting plenty of information. I’m collecting them with the script.

Get-VM | Select *

Same thing with Get-Cluster or Get-ClusterNode. I’m using Get-WmiObject to query Windows servers to get the OS information.

You can add or remove information you would like to collect on this script if you want.

Creating the functions:

First of all, create the functions. You can use whatever name you want like “function something”. And we’re going to use these PowerShell functions on the script.

Function fHTMLheader – Creating an html result file.

Function fHTMLheader
{
param($File)
$Date = (Get-Date).ToString('yyyy/mm/dd')
Add-Content $File "<html>"
Add-Content $File "<head>"
Add-Content $File "<meta http-equiv='Content Type' content='text/html; charset=iso-8859-1'>"
Add-Content $File '<title>VM OS Information</title>'
Add-Content $File '<STYLE TYPE="text/css">' 
Add-Content $File  "<!--" 
Add-Content $File  "td {" 
Add-Content $File  "font-family: Tahoma;" 
Add-Content $File  "font-size: 11px;" 
Add-Content $File  "border-top: 2px solid #999999;" 
Add-Content $File  "border-right: 2px solid #999999;" 
Add-Content $File  "border-bottom: 2px solid #999999;" 
Add-Content $File  "border-left: 2px solid #999999;" 
Add-Content $File  "}" 
Add-Content $File  "body {" 
Add-Content $File  "margin-left: 5px;" 
Add-Content $File  "margin-top: 5px;" 
Add-Content $File  "margin-right: 5px;" 
Add-Content $File  "margin-bottom: 5px;" 
Add-Content $File  "" 
Add-Content $File  "table {" 
Add-Content $File  "border: thin solid #000000;" 
Add-Content $File  "}" 
Add-Content $File  "-->" 
Add-Content $File  "</style>" 
Add-Content $File "</head>" 
Add-Content $File "<body>" 
Add-Content $File  "<table width='100%'>" 
Add-Content $File  "<tr bgcolor='#2F0B3A'>" 
Add-Content $File  "<td colspan='30' height='20' align='center'>" 
Add-Content $File  "<font face='tahoma' color='#FFFF00' size='4'><strong>VM OS Information -  $date</strong></font>" 
Add-Content $File  "</td>" 
Add-Content $File  "</tr>" 
Add-Content $File  "</table>" 
    }

Function fCluster – Writing the cluster name.

Function fCluster
{
Param ($File, $cname)
Add-Content $File  "<table width='100%'>" 
Add-Content $File  "<tr colspan='1' height='20' align='center' bgcolor='#000000'>" 
Add-Content $File  "<td width = '100%' color='#000000' size='2' align=center><font color='#FFFC00'><strong>$cname</strong></font></td>" 
Add-Content $File  "</tr>" 
Add-Content $File  "</table>" 
}

Function fNode – Writing the node name.

Function fNode
{
Param ($File, $nName)
Add-Content $File  "<table width='100%'>" 
Add-Content $File  "<tr height='20' bgcolor='#000000'>" 
Add-Content $File  "<td width = '64%' size='3' align=center><font color='White'><strong>$nName</strong></Font></td>"
Add-Content $File  "</tr>"
Add-Content $File  "</table>"
}

Function fVMTable – Table VM details.

Function fVMTable 
{ 
Param($File) 
Add-Content $File  "<table width='100%'>"
Add-Content $File "<tr bgcolor=#BE81F7>" 
Add-Content $File "<td width='10%' align=center>VM</td>"
Add-Content $File "<td width='5%' align=center>Up-Time</td>"
Add-Content $File "<td width='5%' align=center>OS Version</td>"
Add-Content $File "<td width='5%' align=center>State</td>"
Add-Content $File "<td width='5%' align=center>IntegrationService Version</td>"
Add-Content $File "<td width='5%' align=center>Status</td>"
Add-Content $File "<td width='5%' align=center>Heartbeat</td>"
Add-Content $File "<td width='5%' align=center>Replication State</td>"
Add-Content $File "<td width='5%' align=center>Generation</td>"
Add-Content $File "<td width='5%' align=center>Path</td>"
Add-Content $File "<td width='5%' align=center>Creation Time</td>"
Add-Content $File "<td width='5%' align=center>Clustered</td>"
Add-Content $File "</tr>"
}

Function fHostSub – sub title for host details.

Function fHostSub
{
Param ($File, $nName)
Add-Content $File  "<table width='100%'>" 
Add-Content $File  "<tr height='20' bgcolor='#000000'>" 
Add-Content $File  "<td width = '64%' size='3' align=center><font color='White'><strong>Host Report -$nName</strong></Font></td>" 
Add-Content $File  "</tr>"
Add-Content $File  "</table>"
}

Function fHostStatus – Table Hyper V details.

Function fHostStatus 
{
Param($File) 
Add-Content $File  "<table width='100%'>"
Add-Content $File "<tr bgcolor=#BE81F7>" 
Add-Content $File "<td width='10%' align=center>Hyper-V Server</td>"
Add-Content $File "<td width='10%' align=center>Total VMs</td>"
Add-Content $File "<td width='10%' align=center>OS Version</td>"
Add-Content $File "</tr>" 
}

Function fVMInfo – VM details

Function fVMInfo
{
Param($File, $vmname, $Uptime, $VMOs, $VMState, $ICversion, $VMStatus, $HeartBeat, $Replicated, $Gene, $VMPath, $CreationTime, $Clustered)
Add-Content $File "<tr bgcolor=#FFFFFF>"
Add-Content $File "<td width='10%' align=center>$vmname</td>"
Add-Content $File "<td width='5%' align=center>$uptime</td>"
Add-Content $File "<td width='5%' align=center>$VMOs</td>"
Add-Content $File "<td width='5%' align=center>$VMState</td>"
Add-Content $File "<td width='5%' align=center>$ICversion</td>"
Add-Content $File "<td width='5%' align=center>$VMStatus</td>"
Add-Content $File "<td width='5%' align=center>$HeartBeat</td>"
Add-Content $File "<td width='5%' align=center>$Replicated</td>"
Add-Content $File "<td width='5%' align=center>$Gene</td>"
Add-Content $File "<td width='5%' align=center>$VMPath</td>"
Add-Content $File "<td width='5%' align=center>$CreationTime</td>"
Add-Content $File "<td width='5%' align=center>$Clustered</td>"
}

Function fHostInfo – Hyper V details

Function fHostInfo
{
Param($File, $hName, $totalVMs, $hOS)
    Add-Content $File "<tr bgcolor=#FFFFFF>"
    Add-Content $File "<td width='10%' align=center>$hName</td>"
    Add-Content $File "<td width='10%' align=center>$totalVMs</td>"
    Add-Content $File "<td width='10%' align=center>$hOS</td>"
}

Function fCreateReport – Create the dash board

Function fCreateReport
{
Param($Object, $Type)
If ($Type -match "cluster")
    {
    Write-Host ("Collecting Information from cluster"+$Object) -ForegroundColor Yellow -BackgroundColor Blue
    fCluster $Result ("Cluster - "+$Object)
    $Nodes = Get-Cluster $Object | Get-clusterNode | where {$_.state -eq "Up"}
    $nodecount = $Nodes.length
    $hostInfo = Get-Cluster $Object | Get-ClusterNode | Select Name, @{Label="VM"; Expression={[int]""}}
    For ($a=0; $a -lt $nodecount; $a++)
        {
        Write-Host ("Processing VM on Host"+$Nodes.Name[$a]) -ForegroundColor Yellow -BackgroundColor Blue
        $VMlist = Get-VM -ComputerName $Nodes.Name[$a]
        $VMCount = $hostInfo[$a].VM = $VMlist.count
        If ($VMCount -ge "1")
            {
            fNode $Result $Nodes.Name[$a]
            fVMTable $Result
            For ($b=0; $b -lt $VMCount; $b++)
                {
                Write-Host ("Processing VM - "+$vmlist.Name[$b]) -ForegroundColor Green
                $vmInfo = Get-VM -vmname $VMlist.Name[$b] -ComputerName $Nodes.Name[$a] | SELECT Name, Uptime, State, IntegrationServicesVersion, Status, HeartBeat, ReplicationState, Generation, Path, CreationTime, IsClustered
                
                $error.clear()
                try {$VMos = (Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $VMlist.Name[$b]).caption}
                catch {"The RPC server is unavailable"}
                if ($error)
                {
                $VMos = "NA"}

                fVMInfo $Result $vmInfo.Name $VmInfo.Uptime $VMOs $VmInfo.State $VmInfo.IntegrationServicesVersion $VmInfo.Status $VmInfo.HeartBeat $VmInfo.ReplicationState $VmInfo.Generation $VmInfo.Path $VmInfo.CreationTime $VmInfo.IsClustered
                Write-Host "Finished Processing VM - " $vmInfo.Name -ForegroundColor Yellow -BackgroundColor DarkGreen
                }
            Write-Host "Finished Processing VMs On Hyper-V Cluster Node - "$Nodes.Name[$a] -ForegroundColor White -BackgroundColor Blue
            }
        Else
            {
            Write-Host "No VM found on Hyper-V server - " $Nodes.Name[$a] -ForegroundColor Black -BackgroundColor DarkRed
            }
        }
    fHostSub $Result $Object
    fHostStatus $Result
    for ($c = 0; $c -lt $nodecount; $c++)
        {
        $hOS = (Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Nodes.Name[$c]).caption
        fHostInfo $Result $hostInfo[$c].name $hostInfo[$c].VM $hOS
        }

    Write-Host "Finished Processing Cluster $Object" -ForegroundColor Black -BackgroundColor Cyan
    }
}

Function fHtmlFooter – Footer html file.

Function fHtmlFooter 
{ 
Param($File)  
Add-Content $File "</body>" 
Add-Content $File "</html>" 
}

The script:
The script is creating a list of available cluster and tagging them as “cluster”. Then starting the fCreateReport function if they match the tag. If not the script exit with a warning.

Import-Module -Name Failoverclusters, Hyper-V
$Result = "Hyper-V_Info.html"
New-Item -ItemType File $Result -Force

fHTMLheader $Result
If (Get-Cluster)
    {
    Write-Host "Checking Server for Hyper-V role" -ForegroundColor Black -BackgroundColor Cyan
    $Liste = (Get-Cluster).Name
    fCreateReport $Liste "Cluster"
    }
ElseIf (Get-Cluster)
    {
    Write-Host "Please run the script from a Hyper-V Cluster node" -ForegroundColor Black -BackgroundColor Red
    Write-Host "()()()EXITING SCRIPT()()()" -ForegroundColor Yellow -BackgroundColor Red
    }

Thank you for sharing!

Be the first to comment on "Collecting Hyper-V information using a PowerShell script"

Leave a comment

Your email address will not be published.


*