Test if Wireless Adapter is Working Before Resetting

三世轮回 提交于 2020-02-05 02:14:07

问题


Is there a simple way to prove if a network adapter is working? Perhaps some IP like localhost (127.0.0.1) which is always available regardless of which network I'm connected to; only one that only shows if my wireless network adapter's working? Or perhaps there's some simple diagnostic check to confirm this?

I've tagged this question as PowerShell as that's my preferred language; but I can figure out ways to integrate with any other solutions which may be suggested.

Tried so far

I thought of checking the adapter's properties and found there is a status and an IP; I figured that if there were an assigned IP or a connected status that would prove that all's working; sadly those properties are blank and unknown, so I can't use them.

$adapter = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like '*Wireless*'}
$adapter.Status #returns 2; i.e. unknown
$adapter.NetworkAddresses #is blank

Background

I have an issue where I hibernate my laptop whilst docked then bring it back online no longer docked it loses its wireless connection and requires that the adapter be restarted. The same issue is mentioned in this post: Command/Powershell script to reset a network adapter

I'm hoping to use the above code to automatically resolve the issue by scheduling a task to run when my computer comes out of suspension (e.g. https://superuser.com/a/149924/156700).

Sometimes I'll be on my home network, where the only device to ping is my router, sometimes I'll be on my office network where there's a range of machines I could ping, and sometimes I'll be elsewhere... so determining a good target candidate to test whether my network adapter needs a restart by pinging some external device is more complex than ideal.

I want to run a test before resetting so that I only reset when required. It will also be useful to check once a reset has completed should I wish to queue other tasks which require network presence to complete.


回答1:


It seems the WMI class Win32_NetworkAdapter has an Availability property. https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx

There are a range of values which could represent "working"; for now I've gone with only status 3; i.e. where everything's working 100% as expected / there's no concerns about potential degredation. That may be something worth amending depending on scenario.

function Test-NetworkAdapter {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$AdapterNameMask     
        ,
        [Parameter(Mandatory = $false)]
        [int[]]$HealthyStatusses = @(3) #100% working on full power; for list of other possible values, see https://msdn.microsoft.com/en-us/library/aa387884(v=vs.85).aspx
    )
    process {
       Get-WmiObject -Class Win32_NetworkAdapter `
        | Where-Object {$_.Name -like $AdapterNameMask} `
        | Select-Object @{Name='Working';Expression={$healthyStatusses -contains $_.Availability}}
    }
 }
function Reset-NetworkAdapter {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$AdapterNameMask 
    )
    process {
        Get-WmiObject -Class Win32_NetworkAdapter `
        | Where-Object {$_.Name -like $AdapterNameMask} `
        | %{ #in case multiple matches, loop through all
            $_.Disable()
            $_.Enable()    
        }
    }
}

[string]$wirelessAdapterMask = '*Wireless*'
#I could probably improve this to cope better should there be multiple matches / only resetting those with issues... but for now this meets my requirement
if (-not (Test-NetworkAdapter $wirelessAdapterMask)) {
    Reset-NetworkAdapter $wirelessAdapterMask
}


来源:https://stackoverflow.com/questions/38737616/test-if-wireless-adapter-is-working-before-resetting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!