Created
November 2, 2022 04:55
-
-
Save d3-X-t3r/1ba6908ff788e65dfee4a900d3eaccab to your computer and use it in GitHub Desktop.
[PowerShell][ConfigMgr] Metered Connection Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Log($msg) { Write-Host $msg -ForegroundColor Yellow } | |
Write-Log "Checking for metered network connections..." | |
[int]$MeteredAdapterCount = 0 | |
$UserCostEnum = @{ | |
0 = "Metering Disabled" | |
2 = "Metering Enabled" | |
} | |
#region Data Usage Service | |
$RegKey = "HKLM:\Software\Microsoft\DusmSvc\Profiles\*\`*" | |
$UserCost = Get-ItemProperty -Path $RegKey -Name UserCost -ErrorAction SilentlyContinue | |
ForEach($Profile in $UserCost) { | |
If($Profile.UserCost -ne $null) { | |
$AdapterName = (Get-NetAdapter | Where-Object {$_.InterfaceGuid -eq (Get-Item -Path $Profile.PSParentPath).PSChildName}).Name | |
Write-Log "User Data Usage cost is $($UserCostEnum[$Profile.UserCost]) for adapter `"$($AdapterName)`"." | |
Write-Log "Removing UserCost registry key for adapter `"$($AdapterName)`"." | |
Remove-ItemProperty -Path $Profile.PSPath -Name UserCost -Force -ErrorAction SilentlyContinue | |
Restart-Service DusmSvc -Force -ErrorAction SilentlyContinue | |
Get-NetAdapter -Name $AdapterName | Restart-NetAdapter | |
$MeteredAdapterCount++ | |
} | |
} | |
#endregion | |
#region Wireless Adapters | |
#Wireless adapters use netsh to change the settings. | |
$ProfileList = "$((netsh wlan show profiles) | Select-String -Pattern ' .*' | ? { $_ -NotMatch '<None>' })".Trim() | |
ForEach ($Profile in $ProfileList) { | |
$Config = $null | |
$Setting = $null | |
$Result = $null | |
$Config = (netsh wlan show profile name="$($Profile)") | |
$Setting = (($Config -match "(Cost\s+:+)") -Split ":")[1] -Replace "\s" | |
Write-Log "Wireless adapter profile $($Profile) is set to $($Setting)" | |
If ($Setting -and $Setting -ne 'Unrestricted') { | |
Write-Log "Setting $($Profile) to Unrestricted" | |
$Result = (netsh wlan set profileparameter name="$Profile" cost="Unrestricted") | |
$MeteredAdapterCount++ | |
} | |
} | |
#endregion | |
#region Mobile Broadband Adapters | |
#Mobile Broadband adapters use netsh to change the settings. | |
$ProfileList = [regex]::Matches((netsh mbn show profiles),'\{*\w{8}-\w{4}-\w{4}-\w{4}-\w{12}\}*') | % { $_.Value } | |
ForEach ($Profile in $ProfileList) { | |
$Config = $null | |
$Setting = $null | |
$Result = $null | |
$Config = (netsh mbn show profile name="$($Profile)") | |
$Setting = (($Config -match "(Cost\s+:+)") -Split ":")[1] -Replace "\s" | |
Write-Log "Mobile Broadband adapter profile $($Profile) is set to $($Setting)" | |
If ($Setting -and $Setting -ne 'Unrestricted') { | |
Write-Log "Setting $($Profile) to Unrestricted" | |
$Result = (netsh mbn set profileparameter name="$Profile" cost="Unrestricted") | |
$MeteredAdapterCount++ | |
} | |
} | |
#endregion | |
Write-Log "Total metered adapters found: $MeteredAdapterCount" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-NetMetered | |
{ | |
[bool]$metered = $False | |
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime] | |
$networkprofile = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile() | |
if ($null -eq $networkprofile) { | |
$metered = $false | |
} | |
$cost = $networkprofile.GetConnectionCost() | |
if ($null -eq $cost) { | |
$metered = $false | |
} | |
if ($cost.Roaming -or $cost.OverDataLimit) { | |
$metered = $true | |
} | |
if ($cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Fixed -or | |
$cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Variable) { | |
$metered = $true | |
} | |
if ($cost.NetworkCostType -eq [Windows.Networking.Connectivity.NetworkCostType]::Unrestricted) { | |
$metered = $false | |
} | |
if(!($metered)) { | |
$CCMNetworkCost = (Invoke-CimMethod -Namespace "root\ccm\ClientSDK" -ClassName "CCM_ClientUtilities" -MethodName GetNetworkCost).Value | |
Write-Verbose "ConfigMgr Cost: $($CCMNetworkCost)" | |
If($CCMNetworkCost -ne 1) { | |
$metered = $true | |
} | |
} | |
return $metered | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment