命令行禁用netbios?

Command line disable netbios?

我有一个以太网适配器和一个无线适配器,但我终生无法找出用于通过 TCP/IP 禁用系统上所有适配器的 Netbios 的命令行(或 powershell)。如果对此有任何意见,我将不胜感激。

根据 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

Select 您要使用提供给 Where Object 的过滤器禁用的对象,如下所示。我想关闭 LAN 上的 NetBios。

$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where Description -like "*Ethernet*" 
$adapter.SetTcpIPNetbios(0) | Select ReturnValue

ReturnValue
-----------
          0

虽然有许多可能的 return 代码,就像很多一样。确保 check the list here,不要懒惰地假设该功能适用​​于所有设备。您一定要先测试一下并了解后果。

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)

作为WMI v1 cmdlets were removed in PowerShell 6, the "modern way" to do this is through CIM cmdlets, with an example from 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 '}
    }
}

注意:这需要 运行 作为管理员。

使用以下命令获取非空的每个网络适配器的NetBIOS状态TcpipNetbiosOptions 属性:

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状态。

微软官方文档: