Skip to content

Instantly share code, notes, and snippets.

@kagarlickij
Last active March 29, 2017 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagarlickij/2c8a6072720a1384bdff7dd27dd720e5 to your computer and use it in GitHub Desktop.
Save kagarlickij/2c8a6072720a1384bdff7dd27dd720e5 to your computer and use it in GitHub Desktop.
<#
.NOTES
===========================================================================
Created on: Jan 2017
Created by: Dmitriy Kagarlickij
Contact: dmitriy@kagarlickij.com
===========================================================================
.DESCRIPTION
This script should be executing with Administrator-level permissions
#>
$mainIfName = "Ethernet"
$vpnIfDescription = "TAP-Windows Adapter*"
$mainIfIndex = $((Get-NetAdapter | Where-Object {$_.Name -eq $mainIfName}).ifIndex)
$vpnIfIndex = $((Get-NetAdapter | Where-Object {$_.InterfaceDescription -like $vpnIfDescription}).ifIndex)
$mainIfMetric = $((Get-NetIPInterface | Where-Object {$_.ifIndex -eq $mainIfIndex -and $_.AddressFamily -eq 'IPv4'}).InterfaceMetric)
$vpnIfMetric = $((Get-NetIPInterface | Where-Object {$_.ifIndex -eq $vpnIfIndex -and $_.AddressFamily -eq 'IPv4'}).InterfaceMetric)
if ($vpnIfMetric -ge $mainIfMetric) {
Write-Output "VPN DNS will be fixed now"
Set-NetIPInterface -InterfaceIndex $vpnIfIndex -InterfaceMetric $($mainIfMetric-5)
$mainIfMetric = $((Get-NetIPInterface | Where-Object {$_.ifIndex -eq $mainIfIndex -and $_.AddressFamily -eq 'IPv4'}).InterfaceMetric)
$vpnIfMetric = $((Get-NetIPInterface | Where-Object {$_.ifIndex -eq $vpnIfIndex -and $_.AddressFamily -eq 'IPv4'}).InterfaceMetric)
if ($vpnIfMetric -lt $mainIfMetric) {
Write-Output "VPN DNS has been fixed successfully"
} else {
Write-Output "VPN DNS has been fixed with error"
}
} else {
Write-Output "VPN DNS is ok"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment