Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<#
.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