Kir*_*699 4 windows networking cmd ethernet netsh
我想通过dos将网络接口名称重命名为默认名称,例如“以太网”。
\n\n我知道netsh interface set interface name=\xe2\x80\x9dLocal Area Connection\xe2\x80\x9d newname=\xe2\x80\x9dEthernet\xe2\x80\x9d就是这样做的方法。
我正在运行一个脚本,它不知道接口的名称,对吗?因此,在设置接口的 IP 地址之前,我需要它将其重命名为默认值。
\n\n在不知道接口名称的情况下如何做到这一点?
\n\n有没有办法以某种方式获取接口的名称然后更改它?
\n(可能已弃用,请参阅底部)
\n获取所有 WLAN 接口名称:netsh wlan show interfaces
\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 "show Interfaces - 显示系统上的无线 LAN 接口列表。"
获取 LAN 接口名称:netsh lan show interfaces
\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 “show Interfaces - 显示系统上当前有线接口的列表。”
获取所有接口的名称:netsh interface show interface
\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 "show faces - 显示接口。 "
\n\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0需要“有线自动配置服务”,该服务通常不启动。
此批处理脚本将启动服务,获取(最后一个)LAN 接口名称,将其更改为新名称,再次停止有线自动配置服务。
\nsc.exe start dot3svc\n\nfor /f "tokens=1* delims=: " %%a in (\'netsh lan show interfaces\') do if %%a == Name set activeAdapter=%%b\necho %activeAdapter%\nnetsh interface set interface name="%activeAdapter%" newname="Ethernet"\n\nsc.exe stop dot3svc\nRun Code Online (Sandbox Code Playgroud)\n上述脚本的局限性:
\n对于 Wifi 接口,将上面的脚本 \'lan\' 更改为 \'wlan\' 并删除 sc.exe 服务启动/停止
\n在 PowerShell 中,选择正确的接口更容易。
\n这可能在大多数情况下有效:
\nGet-NetAdapter | Where-Object { $_.HardwareInterface -eq $True -and $_.MediaType -eq "802.3" } | Rename-NetAdapter -NewName "Ethernet"
上述命令的说明:
\nHardwareInterface跳过虚拟接口,例如 VMWare
\n MediaType==802.3仅显示“有线”接口,而不显示 Wifi、宽带或其他接口。
上述命令的简短版本。(在脚本文件中,使用上面的长版本)
\nGet-NetAdapter | ? HardwareInterface | ? MediaType -eq "802.3" | Rename-NetAdapter "Ethernet"
-
\n如果上述命令无法安全地选择一个所需的接口,请使用此命令列出所有接口及其详细参数。
\n Get-NetAdapter | Format-List -Property * -Force
\n也不一一列出它们(将“0”替换为“1”、“2”...):
\n (Get-NetAdapter)[0] | Format-List -Property * -Force
\n使用输出使命令更加具体。
例如
\nRealtek 制造的接口(Realtek 供应商 10ec,Intel:8086):
\nGet-NetAdapter | ? ComponentID -like "PCI\\VEN_10EC*"
非虚拟:
\nGet-NetAdapter | ? Virtual -eq $false
连接器存在:
\nGet-NetAdapter | ? ConnectorPresent
-
\n使用 WMI 对象还可以使用更多选项:
\nGet-WmiObject -Class Win32_NetworkAdapterConfiguration
有时使用 netsh 或 PS 重命名接口不起作用,唯一的方法是编辑注册表。
\ne.g. 如果您更改了网络适配器卡,并且新卡使用的名称为:以太网 2或类似名称,并且您希望将其重命名回“以太网”。
\n这是因为旧界面仍在注册表中,但这些工具看不到。
在这种情况下,使用 netsh 重命名接口将显示这个奇怪的无意义错误:
\n\n\n“您未连接是因为网络上存在重复的名称。如果加入域,请转至控制面板中的系统更改计算机名称,然后重试。如果加入工作组,请选择其他工作组名称。”
\n
使用 PowerShell 会显示更合适的错误:
\n\n\n尝试创建对象,但对象名称已存在。
\n
转到Control Panel\\Network and Internet\\Network Connections并尝试重命名那里的界面也不起作用。
在这种情况下,似乎唯一的方法是在这些注册表路径中找到与旧的、已删除的接口相对应的密钥
\nHKEY_LOCAL_MACHINE\\SYSTEM\\Setup\\Upgrade\\NetworkDriverBackup\\Control\\Network\\{4d36e972-e325-11ce-bfc1-08002be10318}\\\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\\nHKEY_LOCAL_MACHINE\\SYSTEM\\Setup\\Upgrade\\NetworkDriverBackup\\Control\\NetworkSetup2\\Interfaces\\\nRun Code Online (Sandbox Code Playgroud)\n删除旧界面的密钥(不是上面写的整个路径!),
\n重新启动,
\n然后使用 netsh、PowerShell 或在控制面板中手动重命名。现在应该可以了。
大约(或至少在)2013 年,该工具开始显示警告:
\n\n\n... Microsoft 可能会删除 Netsh 功能\n以...
\n
现在十年过去了,它仍然有效。如果您正在编写一个脚本而不是一次性修复,那么使用 PowerShell 方式可能会更谨慎。
\n| 归档时间: |
|
| 查看次数: |
20709 次 |
| 最近记录: |