Get-vNetworkTraffic: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Get-vNetworkTraffic with WMI (faster than Perf-Counters) - v1.7 == <source lang="powershell"> Function Get-vNetworkTrafficWMI { [CmdletBinding()] p…“)
 
 
(4 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
== Get-vNetworkTraffic with WMI (faster than Perf-Counters) - v1.7 ==
+
<span style="font-size:20px;"><span style="color:red">'''-> This function/script is moved to [https://github.com/R-Studio/PSTools GitHub]!'''</span></span><br>
 +
 
 +
First I developed the ''Get-vNetworkTrafficPerfCounter'', but then I noticed that perfmon queries are too slow and so I developed the ''Get-vNetworkTrafficCIM''.
 +
 
 +
== Get-vNetworkTraffic with CIM (faster than Perf-Counters) - v2.1 ==
 
<source lang="powershell">
 
<source lang="powershell">
Function Get-vNetworkTrafficWMI {
+
<#
 +
.Synopsis
 +
  Get-vNetworkTrafficCIM
 +
.DESCRIPTION
 +
  Shows the Traffic of the vNICs of the VMs on a Hyper-V Node
 +
.EXAMPLE
 +
  Get-vNetworkTrafficCIM -ComputerName <ComputerName>
 +
.EXAMPLE
 +
  Get-vNetworkTrafficCIM -ComputerName HyperVNode01 -VMName VM01 -Unit MB/s -SortBy Adapter
 +
#>
 +
 
 +
Function Get-vNetworkTrafficCIM {
 
     [CmdletBinding()]
 
     [CmdletBinding()]
  
Zeile 23: Zeile 38:
 
     process {
 
     process {
 
         If ($VMName -eq $null) {
 
         If ($VMName -eq $null) {
             $WMICounters = (Get-WmiObject "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -notmatch "__TEAMNIC") -and ($_ -notmatch "__DEVICE")} | select Name, BytesReceivedPersec, BytesSentPersec
+
             $WMICounters = (Get-CimInstance "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -notlike "*__TEAMNIC*") -and ($_.Name -notlike "*__DEVICE*")} | select Name, BytesReceivedPersec, BytesSentPersec
 
         } Else {
 
         } Else {
             $WMICounters = (Get-WmiObject "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -match $VMName) -and ($_.Name -notmatch "__TEAMNIC") -and ($_ -notmatch "__DEVICE")} | select Name, BytesReceivedPersec, BytesSentPersec
+
             $WMICounters = (Get-CimInstance "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -match $VMName) -and ($_.Name -notlike "*__TEAMNIC*") -and ($_.Name -notlike "*__DEVICE*")} | select Name, BytesReceivedPersec, BytesSentPersec
 
         }
 
         }
  
Zeile 62: Zeile 77:
 
== Get-vNetworkTraffic with Perf-Counters v0.9 ==
 
== Get-vNetworkTraffic with Perf-Counters v0.9 ==
 
<source lang="powershell">
 
<source lang="powershell">
Function Get-vNetworkTraffic {
+
Function Get-vNetworkTrafficPerfCounter {
 
     [CmdletBinding()]
 
     [CmdletBinding()]
  

Aktuelle Version vom 26. Februar 2020, 10:29 Uhr

-> This function/script is moved to GitHub!

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

Get-vNetworkTraffic with CIM (faster than Perf-Counters) - v2.1

<#
.Synopsis
   Get-vNetworkTrafficCIM
.DESCRIPTION
   Shows the Traffic of the vNICs of the VMs on a Hyper-V Node
.EXAMPLE
   Get-vNetworkTrafficCIM -ComputerName <ComputerName>
.EXAMPLE
   Get-vNetworkTrafficCIM -ComputerName HyperVNode01 -VMName VM01 -Unit MB/s -SortBy Adapter
#>
 
Function Get-vNetworkTrafficCIM {
    [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-CimInstance "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -notlike "*__TEAMNIC*") -and ($_.Name -notlike "*__DEVICE*")} | select Name, BytesReceivedPersec, BytesSentPersec
        } Else {
            $WMICounters = (Get-CimInstance "Win32_PerfFormattedData_NvspNicStats_HyperVVirtualNetworkAdapter" -ComputerName $ComputerName) | ? {($_.Name -match $VMName) -and ($_.Name -notlike "*__TEAMNIC*") -and ($_.Name -notlike "*__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
    }
}