我正在试图找到一个Powershell命令,它将为所有网络适配器提供连接状态,对于连接等于“无网络访问”的适配器,禁用并重新启用适配器。
首先,我需要获得正确的状态,我尝试了以下命令,但它只显示物理连接。请注意,即使连接状态显示“无网络访问”,它们的状态也处于上升状态。
Get-NetAdapter -physical
Name InterfaceDescription ifIndex Status MacAddress
---- -------------------- ------- ------ ----------
Ethernet 5 Remote NDIS based Internet Sharing...#6 26 Up XX-XX-XX-XX-X...
Ethernet 11 Remote NDIS based Internet Sharing...#7 23 Up XX-XX-XX-XX-X...
Ethernet 10 Remote NDIS based Internet Sharing...#8 54 Up XX-XX-XX-XX-X...
Ethernet 12 Remote NDIS based Internet Sharing...#4 17 Up XX-XX-XX-XX-X...
Ethernet 6 Remote NDIS based Internet Sharing...#5 15 Up XX-XX-XX-XX-X...
Ethernet 4 Remote NDIS based Internet Sharing...#3 13 Up XX-XX-XX-XX-X...
Ethernet 9 Remote NDIS based Internet Sharin...#11 12 Up XX-XX-XX-XX-X...
Ethernet 3 Remote NDIS based Internet Sharing...#2 10 Up XX-XX-XX-XX-X...
Ethernet 7 Remote NDIS based Internet Sharing...#9 7 Up XX-XX-XX-XX-X...
Ethernet Intel(R) Ethernet Connection (2) I21... 6 Up XX-XX-XX-XX-X...
Ethernet 8 Remote NDIS based Internet Sharin...#10 4 Up XX-XX-XX-XX-X...但是,我试图针对的是连接性状态,如网络部分所示

发布于 2020-02-24 02:24:26
在@postanote应答上扩展,调用Get-NetConnectionProfile一次并存储结果更有效。我还使用了-eq而不是-match来确保可预测的结果。此外,如果它超过1比1的关系,结果可能是不可预测的。他的回答在windows 10上行不通。
#get profiles and adapters
$Profiles = Get-NetConnectionProfile
$Adapters = Get-NetAdapter -Physical
#loop on adapters
$Results = $Adapters | ForEach-Object {
#get the current adapter
$Adapter = $PSItem
#find the associated profiles
$AdapterProfiles = $Profiles | where {$Adapter.ifIndex -eq $_.InterfaceIndex}
#output the merged results by looping on the profiles
$AdapterProfiles |
Select-Object @{n='Name';e={$Adapter.Name}},
@{n='DeviceName';e={$Adapter.DeviceName}},
@{n='InterfaceDescription';e={$Adapter.InterfaceDescription}},
@{n='Status';e={$adapter.Status}},
IPv4Connectivity,
NetworkCategory
}
#view the output in a nice interactive table
$results | Out-GridView替代产出表:
$results | Format-Table产出:
Name DeviceName InterfaceDescription Status IPv4Connectivity NetworkCategory
---- ---------- -------------------- ------ ---------------- ---------------
Wi-Fi \Device\{FC0C77D5-E521-46C9-A11D-A1E018ED8DEA} Intel(R) Dual Band Wireless-AC 7265 Up Internet Private发布于 2020-02-24 03:17:59
使用WMI来完成这个任务。
get-wmiobject win32_networkadapter | select netconnectionid, name, InterfaceIndex, netconnectionstatus查看网络连接状态。你会在那里找到一个价值。这就是连接状态。
断开连接
1
连接中
2
已连接
3.
断开
4.
硬件不存在
5
硬件禁用
6
硬件故障
7
媒体断开
8
鉴证
9
认证成功
10
认证失败
11
无效地址
12
所需证书
https://stackoverflow.com/questions/60368227
复制相似问题