Refresh WAN Miniport Adapters with PowerShell

Fustration can set in when users keep your workstation powered on and jump to a new network with an AOVPN (Always-on VPN). The adoption of hybrid work has made this network configuration commonplace in many businesses. Issues can arise when this scenario happens.

Resolutions

Flush the DNS

The workstation can cache DNS settings from the previous network, leading to blocked access and other unintended delays. You can use the handy command prompt to resolve this.

Open command prompt as an admin or deploy this as a batch script

ipconfig /flushdns

The Equivalent in PowerShell is Clear-DnsClientCache. So you can write something simple like this.

$domainToResolve = "example.com"

# Flush the DNS cache
Clear-DnsClientCache

try {
    Resolve-DnsName -Name $domainToResolve | Out-Null
    exit 0  # Success
} catch {
    exit 1  # Failure
}

Reset the WAN Mini port adapters

This solution works well when there is a persistent service using the physical network adapter (similar to how you can bind a virtual adapter to the physical adapter in a hypervisor). This is common when you are using a service creates and binds to the adapter to tunnel your traffic. When a user has an issue with network connectivity in the scenario described above, you can use PowerShell to reset the network adapter drivers, located in device manager.

$regex = '(?i)^(wan|wireguard).*'
# Find devices matching the regex in the net class
$netDevices = Get-PnpDevice -Class Net |?{$_.FriendlyName -match $regex}
$success = $true

$netDevices | ForEach-Object {

    $proc = Start-Process -FilePath pnputil.exe -ArgumentList "/remove-device", "$($_.InstanceId)", "/force" -Wait -PassThru -WindowStyle 'Hidden'

    if($proc.HasExited -and ($proc.ExitCode -eq 0)){
        Write-Output("SUCCESS - {0} was removed successfully" -f $_.FriendlyName)
    }else{
        Write-Error("FAIL - {0} was failed to remove" -f $_.FriendlyName)
        $success = $false
    }
}

# scan for hardware changes
$scan = Start-Process pnputil.exe -ArgumentList "/scan-devices" -Wait -WindowStyle "Hidden" -PassThru

if($success -and $scan.ExitCode -eq 0) { exit 0} else {exit 1}

This PowerShell script is designed to help resolve the network issues by automating the process of identifying and removing specific devices like WAN Miniports or WireGuard adapters. It uses a regular expression to find devices matching these names, then forces their removal using pnputil.exe. After each network device is processed, it checks whether the removal was successful and logs the outcome. Once all target devices are handled, the script triggers a hardware scan to refresh the system, ensuring the changes are applied. If everything succeeds, it exits with a 0, signaling a smooth operation; otherwise, it exits with a 1, indicating an issue.

Leave a Reply