datausage.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 7ffaa495-f75b-40ac-921f-78ddc332707e .AUTHOR jeffmeal@microsoft.com .COMPANYNAME Microsoft .COPYRIGHT Copyright (c) Microsoft Corporation .TAGS .LICENSEURI https://opensource.org/licenses/MIT .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Sample script demonstrating how to get attributed network usage from Windows #> Param() [Windows.Networking.Connectivity.ConnectionProfile,Windows.Networking.Connectivity,ContentType=WindowsRuntime] | Out-Null [Windows.Networking.Connectivity.AttributedNetworkUsage,Windows.Networking.Connectivity,ContentType=WindowsRuntime] | Out-Null [Windows.Networking.Connectivity.NetworkUsageStates,Windows.Networking.Connectivity,ContentType=WindowsRuntime] | Out-Null [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime] | Out-Null Add-Type -AssemblyName System.Runtime.WindowsRuntime $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] Function Await($WinRtTask, $ResultType) { $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) $netTask = $asTask.Invoke($null, @($WinRtTask)) $netTask.Wait(-1) | Out-Null $netTask.Result } $connectionProfile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile() $states = new-object Windows.Networking.Connectivity.NetworkUsageStates $end = [System.DateTimeOffset]::Now $begin = $end.AddMonths(-1) $attributedUsage = Await($connectionProfile.GetAttributedNetworkUsageAsync($begin,$end,$states)) ([System.Collections.Generic.IReadOnlyList[Windows.Networking.Connectivity.AttributedNetworkUsage]]) $attributedUsage |Select-Object AttributionId,BytesSent,BytesReceived,@{Name='BytesTotal';Expression={$_.BytesReceived+$_.BytesSent}} |Sort-Object -Descending BytesTotal |Format-Table BytesTotal,BytesSent,BytesReceived,AttributionId |