Get-vNetworkTraffic

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche

First I developed the Get-vNetworkTrafficPerfCounter, but then I noticed that perfmon queries are too slow and so I developed the Get-vNetworkTrafficWMI.

Get-vNetworkTraffic with WMI (faster than Perf-Counters) - v1.8

<#
.Synopsis
   Get-vNetworkTraffic (WMI)
.DESCRIPTION
   Shows the Traffic of the vNICs of the VMs on a Hyper-V Node
.EXAMPLE
   Get-vNetworkTrafficWMI -ComputerName <ComputerName>
.EXAMPLE
   Get-vNetworkTrafficWMI -ComputerName HyperVNode01 -VMName VM01 -Unit MB/s -SortBy Adapter
#>
 
Function Get-vNetworkTrafficWMI {
    [CmdletBinding()]
 
    param(
        [Parameter(Position=0,mandatory=$false,HelpMessage="ComputerName of the remote Hyper-V Node")]
        [string] $ComputerName = $env:COMPUTERNAME,
 
        [Parameter(Position=1,mandatory=$false,HelpMessage="VMName")]
        [string] $VMName,
 
        [Parameter(Position=2,mandatory=$false,HelpMessage="Unit/sec")]
        [ValidateSet('MB/s','KB/s')]
        [string] $Unit= "MB/s",
 
        [Parameter(Position=3,mandatory=$false,HelpMessage="Sort by")]
        [ValidateSet('Adapter','AllTraffic','Receive','Sent')]
        [string] $SortBy = "AllTraffic"
    )
 
 
    process {
        If ($VMName -eq $null) {
            $WMICounters = (Get-WmiObject "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -notmatch "__TEAMNIC") -and ($_ -notmatch "__DEVICE")} | select Name, BytesReceivedPersec, BytesSentPersec
        } Else {
            $WMICounters = (Get-WmiObject "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -match $VMName) -and ($_.Name -notmatch "__TEAMNIC") -and ($_ -notmatch "__DEVICE")} | select Name, BytesReceivedPersec, BytesSentPersec
        }
 
        $PSObject = @()
 
        Foreach ($Counter in $WMICounters) {
            #Write-Host $Counter -ForegroundColor Green #Enable for Debugging
 
            If ($Unit -eq "MB/s") {
                $ReceiveRoundedValueIn = [math]::Round(($Counter.BytesReceivedPersec/1024/1024),2)
                $SentRoundedValueIn = [math]::Round(($Counter.BytesSentPersec/1024/1024),2)
            } ElseIf ($Unit -eq "KB/s") {
                $ReceiveRoundedValueIn = [math]::Round(($Counter.BytesReceivedPersec/1024),2)
                $SentRoundedValueIn = [math]::Round(($Counter.BytesSentPersec/1024),2)
            } Else {
                $ReceiveRoundedValueIn = [math]::Round(($Counter.BytesReceivedPersec/1024),2)
                $SentRoundedValueIn = [math]::Round(($Counter.BytesSentPersec/1024),2)
            }
 
            $PSObjectReceive = New-Object PSCustomObject -Property @{
                Adapter = ($Counter.Name -split "_")[0]
                Receive = $ReceiveRoundedValueIn
                Sent = $SentRoundedValueIn
                AllTraffic = $ReceiveRoundedValueIn + $SentRoundedValueIn
                Unit = $Unit
                #FullCounterName = $Counter
            }
            $PSObject += $PSObjectReceive
        }
        $PSObject | Select Adapter, AllTraffic, Receive, Sent, Unit| Sort $SortBy -Descending
    }
}


Get-vNetworkTraffic with Perf-Counters v0.9

Function Get-vNetworkTrafficPerfCounter {
    [CmdletBinding()]
 
    param(
        [Parameter(Position=0,mandatory=$true,HelpMessage="Your Help-Message")]
        [string] $ComputerName = "hostname",
        [Parameter(Position=1,mandatory=$false,HelpMessage="Your Help-Message")]
        [string] $SortBy = "Value"
    )
 
 
    process {
        $Counters = (Get-Counter -List "Hyper-V Virtual Network Adapter" -ComputerName $ComputerName).PathsWithInstances | ? {(($_ -notmatch "__TEAMNIC") -and ($_ -notmatch "__DEVICE")) -and (($_ -like "*\Bytes Received/sec") -or ($_ -like "*\Bytes Sent/sec"))} 
        $PSObject = @()
 
        Foreach ($Counter in $Counters) {
            Write-Host $Counter -ForegroundColor Green #Enable for Debugging
            $CountersReceive =  $Counter | ? {$_ -like "*\Bytes Received/sec"} | Sort
            $CountersSent = $Counter | ? {$_ -like "*\Bytes Sent/sec"} | Sort
 
            If ($CountersReceive -ne $Null) {
                $Value = (Get-Counter $CountersReceive -ComputerName $ComputerName).CounterSamples.CookedValue
                $RoundedValueInKBs = [math]::Round(($Value/1024),2)
 
                $PSObjectReceive = New-Object PSCustomObject -Property @{
                    Adapter = ($CountersReceive -split "\(" -split "_")[1]
                    Value = $RoundedValueInKBs
                    Direction = "KBytes Sent/sec"
                    FullCounterName = $Counter
                }
 
                $PSObject += $PSObjectReceive
            } ElseIf ($CountersSent -ne $Null) {
                $Value = (Get-Counter $CountersSent -ComputerName $ComputerName).CounterSamples.CookedValue
                $RoundedValueInKBs = [math]::Round(($Value/1024),2)
 
                $PSObjectSent = New-Object PSCustomObject -Property @{
                    Adapter = ($CountersSent -split "\(" -split "_")[1]
                    Value = $RoundedValueInKBs
                    Direction = "KBytes Receive/sec"
                    FullCounterName = $Counter         
                }
 
                $PSObject += $PSObjectSent
            }
        }
        $PSObject | Select Adapter, Value, Direction, FullCounterName | Sort $SortBy -Descending
    }
}