我有一个以太网适配器和一个无线适配器,我一生都无法弄清楚用于为系统上的所有适配器禁用 TCP/IP 上的 Netbios 的命令行(或 powershell)。我将不胜感激对此的任何意见。
5 回答
根据 Andre Viot 的博客:
$adapters=(gwmi win32_networkadapterconfiguration )
Foreach ($adapter in $adapters){
Write-Host $adapter
$adapter.settcpipnetbios(0)
}
应该在每个适配器上禁用 Netbios。您可能希望更加挑剔,并确保您在正确的界面上禁用了 Netbios,但是,因此我将首先运行Get-WmiObject Win32_NetworkAdapterConfiguration | Where IPAddress
,查看当前连接的适配器列表。
ServiceName DHCPEnabled Index Description
----------- ----------- ----- -----------
VMSMP True 14 Intel Wireless Adapter
VMSMP True 29 Intel Ethernet Adapter
使用提供给 Where Object 的过滤器选择要禁用的选项,如下所示。我想关闭 LAN 上的 NetBios。
$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where Description -like "*Ethernet*"
$adapter.SetTcpIPNetbios(0) | Select ReturnValue
ReturnValue
-----------
0
不过,有许多可能的返回码,比如很多。请务必查看此处的列表,不要懒惰地假设该功能适用于所有设备。您绝对应该首先对此进行测试并了解其后果。
http://www.alexandreviot.net/2014/10/09/powershell-disable-netbios-interface/
如果您尝试在没有连接的适配器上设置 NetBIOS 的配置,您可以更改注册表中的设置,而不是直接使用 SetTcpIPNetbios。
我遍历每个适配器端口(我有 16 个),然后在所有这些端口上关闭 NetBIOS:
$i = 'HKLM:\SYSTEM\CurrentControlSet\Services\netbt\Parameters\interfaces'
Get-ChildItem $i | ForEach-Object {
Set-ItemProperty -Path "$i\$($_.pschildname)" -name NetBiosOptions -value 2
}
从其他答案和评论中,我将其用作禁用 NetBIOS 的一行命令:
(Get-WmiObject Win32_NetworkAdapterConfiguration -Filter IpEnabled="true").SetTcpipNetbios(2)
由于在 PowerShell 6 中删除了 WMI v1 cmdlet,因此执行此操作的“现代方式”是通过 CIM cmdlet,例如powershell.one:
# define the arguments you want to submit to the method
# remove values that you do not want to submit
# make sure you replace values with meaningful content before running the code
# see section "Parameters" below for a description of each argument.
$arguments = @{
TcpipNetbiosOptions = [UInt32](12345) # replace 12345 with a meaningful value
}
# select the instance(s) for which you want to invoke the method
# you can use "Get-CimInstance -Query (ADD FILTER CLAUSE HERE!)" to safely play with filter clauses
# if you want to apply the method to ALL instances, remove "Where...." clause altogether.
$query = 'Select * From Win32_NetworkAdapterConfiguration Where (ADD FILTER CLAUSE HERE!)'
Invoke-CimMethod -Query $query -Namespace Root/CIMV2 -MethodName SetTcpipNetbios -Arguments $arguments |
Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
switch ([int]$this.ReturnValue)
{
0 {'Successful completion, no reboot required'}
1 {'Successful completion, reboot required'}
64 {'Method not supported on this platform'}
65 {'Unknown failure'}
66 {'Invalid subnet mask'}
67 {'An error occurred while processing an Instance that was returned'}
68 {'Invalid input parameter'}
69 {'More than 5 gateways specified'}
70 {'Invalid IP address'}
71 {'Invalid gateway IP address'}
72 {'An error occurred while accessing the Registry for the requested information'}
73 {'Invalid domain name'}
74 {'Invalid host name'}
75 {'No primary/secondary WINS server defined'}
76 {'Invalid file'}
77 {'Invalid system path'}
78 {'File copy failed'}
79 {'Invalid security parameter'}
80 {'Unable to configure TCP/IP service'}
81 {'Unable to configure DHCP service'}
82 {'Unable to renew DHCP lease'}
83 {'Unable to release DHCP lease'}
84 {'IP not enabled on adapter'}
85 {'IPX not enabled on adapter'}
86 {'Frame/network number bounds error'}
87 {'Invalid frame type'}
88 {'Invalid network number'}
89 {'Duplicate network number'}
90 {'Parameter out of bounds'}
91 {'Access denied'}
92 {'Out of memory'}
93 {'Already exists'}
94 {'Path, file or object not found'}
95 {'Unable to notify service'}
96 {'Unable to notify DNS service'}
97 {'Interface not configurable'}
98 {'Not all DHCP leases could be released/renewed'}
100 {'DHCP not enabled on adapter'}
default {'Unknown Error '}
}
}
注意:这需要以管理员身份运行。
使用以下命令获取每个具有非空TcpipNetbiosOptions
属性的网络适配器的 NetBIOS 状态:
Get-CimInstance -ClassName 'Win32_NetworkAdapterConfiguration' | Where-Object -Property 'TcpipNetbiosOptions' -ne $null | Select-Object -Property @('ServiceName', 'Description', 'TcpipNetbiosOptions');
对于每个网络适配器,值1
表示启用 NetBIOS,值2
表示禁用 NetBIOS。
使用以下命令为每个具有非空TcpipNetbiosOptions
属性的网络适配器禁用 NetBIOS:
Get-CimInstance -ClassName 'Win32_NetworkAdapterConfiguration' | Where-Object -Property 'TcpipNetbiosOptions' -ne $null | Invoke-CimMethod -MethodName 'SetTcpipNetbios' -Arguments @{ 'TcpipNetbiosOptions' = [UInt32](2) } -Confirm;
该-Confirm
参数需要对每次更改进行确认,如果一个人有多个网络适配器并且只想更改其中一些的 NetBIOS 状态,这将很有帮助。删除该-Confirm
参数以简单地禁用所有先前显示的网络适配器的 NetBIOS,从而加快进程。
ReturnValue
应显示值列表。值ReturnValue
表示0
操作成功。再次运行第一个命令以确认每个网络适配器的 NetBIOS 状态。
微软官方文档:
- https://docs.microsoft.com/powershell/scripting/learn/deep-dives/everything-about-null
- https://docs.microsoft.com/powershell/module/microsoft.powershell.core/where-object
- https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-object
- https://docs.microsoft.com/powershell/module/cimcmdlets/get-ciminstance
- https://docs.microsoft.com/powershell/module/cimcmdlets/invoke-cimmethod
- https://docs.microsoft.com/windows/win32/cimwin32prov/win32-networkadapterconfiguration
- https://docs.microsoft.com/windows/win32/cimwin32prov/settcpipnetbios-method-in-class-win32-networkadapterconfiguration