重置之前测试无线适配器是否正常工作 [英] Test if Wireless Adapter is Working Before Resetting

查看:192
本文介绍了重置之前测试无线适配器是否正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法来证明网络适配器是否正常工作?也许某些IP(例如localhost(127.0.0.1))始终可用,无论我连接到哪个网络;仅显示我的无线网络适配器是否正常工作的一个?还是可以通过一些简单的诊断检查来确认这一点?

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?

我已将此问题标记为PowerShell,因为这是我的首选语言.但我可以找出与建议的任何其他解决方案集成的方法.

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.

到目前为止已尝试

我想到检查适配器的属性,发现有一个状态和一个IP;我发现,如果存在分配的IP或连接状态,这将证明所有组件都在工作;可悲的是这些属性是blankunknown,所以我不能使用它们.

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

背景

我遇到一个问题,我在对接笔记本计算机时使笔记本计算机休眠,然后将其重新置于联机状态,而不再对接,它将失去其无线连接,并需要重新启动适配器.这篇文章中提到了相同的问题:使用Command/Powershell脚本来重置网络适​​配器

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).

有时,我将在家庭网络中,只能ping通我的路由器,有时我将在办公室网络中可以进行ping通的机器,有时我会在其他地方. ..因此,通过ping某些外部设备来确定测试网络适配器是否需要重新启动的良好目标候选人比理想情况复杂.

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.

推荐答案

似乎WMI类Win32_NetworkAdapter具有Availability属性. https://msdn.microsoft.com/zh-我们/library/aa394216(v=vs.85).aspx

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

有一系列值可以表示有效";现在,我只剩下状态3;也就是说,凡是一切都按预期方式100%进行的工作/无需担心潜在的性能下降.视情况而定,可能值得修改.

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
}

这篇关于重置之前测试无线适配器是否正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆