【问题标题】:NetBIOS domain of computer in PowerShellPowerShell 中计算机的 NetBIOS 域
【发布时间】:2011-08-13 11:24:57
【问题描述】:

如何从 PowerShell 获取当前计算机的 NetBIOS(又名“短”)域名?

$ENV:USERDOMAIN 显示当前用户的域,但我想要当前机器所属的域。

我发现 pretty easily in VBScript 可以做到这一点,但显然是在 PowerShell 中 ADSystemInfo isn't very nice to use

更新

这是我的最终解决方案,包含使用Win32_NTDomain 的建议,但过滤到当前机器的域

$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$domain = $wmiDomain.DomainName

【问题讨论】:

标签: powershell active-directory wmi netbios


【解决方案1】:

来自Here

# Retrieve Distinguished Name of current domain.
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Root = $Domain.GetDirectoryEntry()
$Base = ($Root.distinguishedName)

# Use the NameTranslate object.
$objTrans = New-Object -comObject "NameTranslate"
$objNT = $objTrans.GetType()

# Invoke the Init method to Initialize NameTranslate by locating
# the Global Catalog. Note the constant 3 is ADS_NAME_INITTYPE_GC.
$objNT.InvokeMember("Init", "InvokeMethod", $Null, $objTrans, (3, $Null))

# Use the Set method to specify the Distinguished Name of the current domain.
# Note the constant 1 is ADS_NAME_TYPE_1779.
$objNT.InvokeMember("Set", "InvokeMethod", $Null, $objTrans, (1, "$Base"))

# Use the Get method to retrieve the NetBIOS name of the current domain.
# Note the constant 3 is ADS_NAME_TYPE_NT4.
# The value retrieved includes a trailing backslash.
$strDomain = $objNT.InvokeMember("Get", "InvokeMethod", $Null, $objTrans, 3)

【讨论】:

  • 仅供参考,我在 Windows 10 20H2 上的 Windows PowerShell 上尝试了此操作,第一行失败,异常调用“GetCurrentDomain”,参数为“0”:“当前安全上下文未与 Active Directory 关联域或森林。”
【解决方案2】:

在大多数情况下,默认的 NetBIOS 域名是 DNS 域名中最左边的标签,最多前 15 个字节(NetBIOS 名称的限制为 15 个字节)。 NetBIOS 域名可能会在安装 Active Directory 期间更改,但不能更改。

WIN32_ComputerSystem WMI 对象提供有关 Windows 计算机的信息

PS C:\> Get-WmiObject Win32_ComputerSystem

Domain              : WORKGROUP
Manufacturer        : Hewlett-Packard
Model               : HP EliteBook 8530w (XXXXXXXXX)
Name                : ABCHPP2
PrimaryOwnerName    : ABC
TotalPhysicalMemory : 4190388224

所以域名由:

PS C:\> (gwmi WIN32_ComputerSystem).Domain

但在域安装中,给出了 DNS 名称。在这种情况下,您可以使用nbtstat -n 命令找到NetBIOS 域名,显示如下<DOMAIN><1B>

PowerShell 命令可能是:

nbtstat -n | Select-String -Pattern "^ *(.*) *<1B>.*$" | % {$_ -replace '^ *(.*) *<1B>.*$','$1'}

这是使用 WMI 的另一种方式

PS C:\> (gwmi Win32_NTDomain).DomainName

【讨论】:

  • 我已经尝试过这种方法,但它不起作用。 “域”属性不是我所追求的短/NetBIOS 名称。包含完整的 AD 域名
  • 我试过了,对我来说它显示为 (而不是 )
  • 根据documentation可以是、、、、,所以可以把正则表达式改成“^(。 ) *.*$"
  • Win32_NTDomain WMI 对象怎么样
  • Win32_NTDomain 看起来像个赢家。请注意,它返回多个条目,包括一个用于本地计算机和可能其他受信任域(如果有的话)
【解决方案3】:
import-module activedirectory
(Get-ADDomain -Identity (Get-WmiObject Win32_ComputerSystem).Domain).NetBIOSName

【讨论】:

    【解决方案4】:

    OP 在“计算机域”之后,所以答案是 $GetComputerDomain(如下),但我也会添加 $GetUserDomain 以供参考。

    $GetComputerDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()).Name
    $GetUserDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name
    

    我发现 wmi (gwmi) 选项非常慢,尤其是在查询 Win32_NTDomain 类时。我有一个多信任的域环境,当我只需要快速的简单信息时,它需要很长时间。

    【讨论】:

    • 抱歉,这不是我想要的结果。它们都返回一个完整的 DNS 样式的域名。我追求的是短/NetBIOS 风格的域名。
    【解决方案5】:

    使用env:通过PowerShell获取环境设置

    NetBIOS:$env:userdomain

    FQDN:$env:userdnsdomain

    查看所有值:

    dir env:  (no $)
    

    【讨论】:

    • 我的问题中已经提到了这个答案。它不会返回计算机的域(这就是我要问的)
    • USERDOMAINUSERDNSDOMAIN。第二个完成了我的任务。
    • 谢谢!这就是我正在寻找的答案。
    【解决方案6】:

    下面的 powershell 命令效果很好!我尝试了各种解决方案后进行了测试。

    如果您使用以下 .Net 命令:

     [System.Net.Dns]::GetHostByAddress('192.168.1.101').hostname
    

    它也可以,但是它使用 DNS 来解析,在我的例子中,我们有 WINS 设置来支持需要它的应用程序,所以不能使用它。下面是我最终用作检查每个客户端的 WINS 注册的脚本的一部分:

    $IPAddress = "<enterIPAddress>" (remove brackets, just enter IP address)
    
    (nbtstat -A $IPAddress | ?{$_ -match '\<00\>  UNIQUE'}).Split()[4]
    

    http://social.technet.microsoft.com/Forums/en-US/f52eb2c7-d55d-4d31-ab4e-09d65d366771/how-to-process-cmd-nbtstat-a-ipaddress-output-and-display-the-computer-name-in-powershell?forum=ITCG

    以上链接有话题和对话。

    【讨论】:

      【解决方案7】:

      使用 Active Directory Cmdlet Get-ADDomain:

      (Get-ADDomain -Current LocalComputer).NetBIOSName
      

      【讨论】:

      • 这看起来像是@Sacha 的回答的一个变种
      • 是的,类似。我的击键次数更少:)
      • 哈!好吧,我想您确实只有有限的数量可以用完keysleft.com :-)
      • 天呐,希望我90岁还没有工作!大声笑
      【解决方案8】:

      使用 NetGetJoinInformation 和 P/Invoke:

      Add-Type -MemberDefinition @"
      [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
      public static extern uint NetApiBufferFree(IntPtr Buffer);
      [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
      public static extern int NetGetJoinInformation(
        string server,
        out IntPtr NameBuffer,
        out int BufferType);
      "@ -Namespace Win32Api -Name NetApi32
      
      function GetDomainName {
        $pNameBuffer = [IntPtr]::Zero
        $joinStatus = 0
        $apiResult = [Win32Api.NetApi32]::NetGetJoinInformation(
          $null,               # lpServer
          [Ref] $pNameBuffer,  # lpNameBuffer
          [Ref] $joinStatus    # BufferType
        )
        if ( $apiResult -eq 0 ) {
          [Runtime.InteropServices.Marshal]::PtrToStringAuto($pNameBuffer)
          [Void] [Win32Api.NetApi32]::NetApiBufferFree($pNameBuffer)
        }
      }
      

      【讨论】:

        【解决方案9】:

        这也可以通过使用 .NET 框架(比 WMI 快得多)来完成

        PS > [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
        

        会回来

        HostName      : SurfaceBook
        DomainName    : mydomain.com
        NodeType      : Hybrid
        DhcpScopeName :
        IsWinsProxy   : False
        

        【讨论】:

          【解决方案10】:

          使用 ADSystemInfo COM 对象应该可以工作,Win32_NTDomain 查找没有延迟:

          $ADSystemInfo = New-Object -ComObject "ADSystemInfo"
          $ADSystemInfo.GetType().InvokeMember("DomainShortName", "GetProperty", $null, $ADSystemInfo, $null)
          

          此 COM 对象还提供其他与 AD 相关的属性:

          https://docs.microsoft.com/en-us/windows/win32/adsi/iadsadsysteminfo-property-methods

          [已编辑 - 最初包含 WinNTSystemInfo COM 对象的代码,但评论者指出这仅返回用户的短域 - 但 ADSystemInfo 确实返回计算机的短域]

          【讨论】:

          • 看起来返回的是用户的域,而不是机器的域
          • @DavidGardiner 今天检查过,WinNTSystemInfo 的 DomainName 属性确实返回了用户的域,而不是机器的域。但是 ADSystemInfo DomainShortName 属性是您想要的。我已经更新了答案以反映这一点。
          【解决方案11】:

          这是另一种比 Win32_NTDomain 更快的方法,用于获取计算机的 NetBIOS 域。

          # Get the computer system CIM/WMI
          $computersystem = Get-CimInstance Win32_ComputerSystem
          
          # Create a Windows Identity Principal object based on the name and domain in Win32_ComputerSystem
          $ComputerPrincipal = [System.Security.Principal.WindowsIdentity]::new("$($computersystem.name)@$($computersystem.domain)")
          
          # Split the NetBIOS name on \ and get the first value which should be the domain
          ($ComputerPrincipal.Name -split "\\")[0]
          
          
          # Bonus point, the WindowsIdentity Principal has a bunch of other useful information.
          # Like quick enumeration of the groups it's in (but needs to be translated from SID to NTAccount format).
          $ComputerPrincipal.Groups.Translate([System.Security.Principal.NTAccount]).value
          

          【讨论】:

            猜你喜欢
            • 2017-01-01
            • 2017-06-12
            • 2020-09-01
            • 2018-09-09
            • 1970-01-01
            • 2012-07-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多