Measuring Hyper-V Lab Network Traffic with PowerShell

In a recent project / study, I had the need to measure the individual, as well as combined, outbound network traffic from a fixed set of of servers. In this example, a domain controller (DC01), a ConfigMgr Management Point (CM01), and a ConfigMgr Distribution Point (DP01). What can I say, PowerShell to the rescue.

The Script

This is a script for Hyper-V, which measures outbound traffic on a set of virtual machines, both for the individual virtual machines and summarized for the set of virtual machines. Modify line 3-5 and 9 to match your environment. The default script setting logs the traffic every 10 seconds.

# Script to measure outbound traffic on a set of Hyper-V VMs, both individual and summarized for all VMs

$Logfile = "C:\Setup\Lab-Networkinfo.log"
$TimeInBetweenTests = 10 # Seconds
$NumberOfTests = '1440' # One test per 10 second = 4 hours

# VMs to measure outbound traffic for. 
# In this example a Domain Controller, a ConfigMgr Site Server, and a ConfigMgr DP
$VMsToMeasure = "2Pint-DC01","2Pint-CM01","2Pint-DP01"

# Enable Resource Metering on the selected VMs
Get-VM $VMsToMeasure | Enable-VMResourceMetering

Function TimeStamp {
    $(Get-Date -UFormat "%D %T")
} 

Function GetOutboundTraffic{
    Param($VMName)
    $networkinfo = Measure-VM -Name $VMName | `
    Select-Object -property @{Expression = {"{0:N2}" -f(($_.NetworkMeteredTrafficReport | `
    Where-Object direction -Eq 'outbound' | `
    Measure-Object -property TotalTraffic -sum).Sum / 1024) };Label="Outbound Network Traffic (GB)"}
    return $networkinfo.'Outbound Network Traffic (GB)'
}

# Remove any existing logfile
If (test-path $Logfile){ Remove-Item $Logfile -Force }

# Reset the metering counters for all measured VMs
Get-VM -Name $VMsToMeasure | Reset-VMResourceMetering

$i = 1
do {

    # Set TimeStamp to measurement of first VM in each test
    $Time = $(TimeStamp)
    
    [System.Collections.ArrayList]$AggregateOutboundTraffic = @()
    # Measure each VM
    Foreach($VM in $VMsToMeasure){
        
        $OutboundTraffic = (GetOutboundTraffic -VMName $VM)
        $Time + " $VM Outbound Network Traffic (GB) : $OutboundTraffic" | Out-File -FilePath $Logfile -Append -Encoding ascii

        $obj = [PSCustomObject]@{

            # Add values to arraylist
            VMName = $VM
            TimeStamp = $Time 
            OutboundTraffic = $OutboundTraffic
            
        }
        
        # Add all the values
        $AggregateOutboundTraffic.Add($obj)|Out-Null

    }

    # Log total traffic
    $TotalOutboundTraffic = ($AggregateOutboundTraffic | Measure-Object -Property OutboundTraffic -Sum ).sum
    $Time + " All VMs Total Outbound Network Traffic (GB) : $TotalOutboundTraffic" | Out-File -FilePath $Logfile -Append -Encoding ascii

    # Sleep in between tests
    Start-Sleep -Seconds $TimeInBetweenTests
$i++
}
while ($i -le $NumberOfTests)
Sample log file from the script.
About the author

Johan Arwidmark

0 0 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Ram
Ram
1 year ago

Hi – How can,I do the same for Physical Machine?


>