NetMetered.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
    ===========================================================================
     Created on: 25/12/2015 9:48 PM
     Created by: Wil Taylor (wilfridtaylor@gmail.com)
     Organization:
     Filename: NetMetered.psm1
    -------------------------------------------------------------------------
     Module Name: NetMetered
    ===========================================================================
#>


<#
    .SYNOPSIS
    Returns if current connection is a metered connection or not.
 
    .DESCRIPTION
    This cmdlet checks if connection is metered or not.
 
    This is based on the example in https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj835821.aspx
 
    .EXAMPLE
    Check if connected to a metered connection
 
    If(Test-NetMetered) { Write-Host "Metered" }
#>

function Test-NetMetered
{
    [void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime]
    $networkprofile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile()
    
    if ($networkprofile -eq $null)
    {
        Write-Warning "Can't find any internet connections!"
        return $false
    }
    
    $cost = $networkprofile.GetConnectionCost()
    
    
    if ($cost -eq $null)
    {
        Write-Warning "Can't find any internet connections with a cost!"
        return $false
    }
    
    if ($cost.Roaming -or $cost.OverDataLimit)
    {
        return $true
    }
    
    if ($cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Fixed -or
    $cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Variable)
    {
        return $true
    }
    
    if ($cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Unrestricted)
    {
        return $false
    }
    
    throw "Network cost type is unknown!"
    
}
Export-ModuleMember Test-NetMetered