PowerShell Set-NetAdapterBinding
最后修改:2025 年 2 月 15 日
本教程涵盖了 PowerShell 中的 Set-NetAdapterBinding cmdlet。它用于启用或禁用网络适配器协议绑定。这有助于管理网络连接和优化性能。
网络适配器绑定基础知识
网络适配器绑定决定了哪些协议和服务可以使用网络适配器。每个适配器可以为不同的协议设置多个绑定。Set-NetAdapterBinding cmdlet 用于修改这些绑定。这对于安全和性能调优很有用。
Set-NetAdapterBinding 的基本用法
最简单的用法是启用或禁用特定适配器上的绑定。您需要适配器名称和组件 ID。-Enabled 参数接受 $true 或 $false 值。此示例禁用了适配器上的 IPv6。
Set-NetAdapterBinding -Name "Ethernet" -ComponentID "ms_tcpip6" -Enabled $false
此命令会禁用以太网适配器上的 IPv6 绑定。组件 ID "ms_tcpip6" 标识 IPv6 协议。默认情况下,不返回任何输出。
在所有适配器上启用绑定
您可以跨所有网络适配器修改绑定。使用通配符 (*) 作为 -Name 参数的值。此示例在所有适配器上启用链路层拓扑发现。请谨慎进行全局更改。
Set-NetAdapterBinding -Name "*" -ComponentID "ms_lltdio" -Enabled $true
这会在所有网络适配器上启用 LLTD 协议。组件 ID "ms_lltdio" 指的是链路层拓扑发现。使用 Get-NetAdapterBinding 验证更改。
PS C:\> Get-NetAdapterBinding -Name "Ethernet" -ComponentID "ms_lltdio" Name DisplayName ComponentID Enabled ---- ----------- ----------- ------- Ethernet Link-Layer Topology Discovery Responder ms_lltdio True
使用管道禁用绑定
PowerShell 的管道可以简化绑定管理。首先使用 Get-NetAdapter 获取适配器,然后通过管道传递给 Set-NetAdapterBinding。此示例禁用无线适配器上的 NetBIOS over TCP/IP。
Get-NetAdapter -Name "Wi-Fi*" | Set-NetAdapterBinding -ComponentID "ms_netbios" -Enabled $false
这会禁用所有名称以 "Wi-Fi" 开头的适配器上的 NetBIOS。管道直接将适配器对象传递给绑定 cmdlet。这种方法对于批量操作非常高效。
更改前检查当前绑定
在进行更改之前,最好先验证当前设置。将 Get-NetAdapterBinding 与 Set-NetAdapterBinding 结合使用。此示例显示了检查然后禁用绑定的过程。
$bindings = Get-NetAdapterBinding -Name "Ethernet" -ComponentID "ms_server"
if ($bindings.Enabled) {
Set-NetAdapterBinding -Name "Ethernet" -ComponentID "ms_server" -Enabled $false
}
此脚本首先检查是否启用了“文件和打印机共享”绑定。仅当启用时,它才会禁用该绑定。ms_server 组件 ID 代表此服务。
使用脚本启用多个绑定
您可以在单个脚本中管理多个绑定。创建组件 ID 数组并循环遍历它们。此示例在特定适配器上启用了多个协议。
$components = @("ms_tcpip6", "ms_lltdio", "ms_rspndr")
foreach ($comp in $components) {
Set-NetAdapterBinding -Name "Ethernet" -ComponentID $comp -Enabled $true
}
此脚本在以太网适配器上启用 IPv6、LLTD 和链路层拓扑发现响应器。数组包含每个协议的组件 ID。foreach 循环顺序处理每个绑定。
来源
在本文中,我们介绍了 PowerShell 中的 Set-NetAdapterBinding cmdlet。
作者
列出 所有 PowerShell 教程。