20

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

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

我发现你可以在 VBScript 中很容易地做到这一点,但显然ADSystemInfo在 PowerShell中使用起来不是很好。

更新

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

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

11 回答 11

21

在大多数情况下,默认的 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
于 2011-04-27T04:37:31.420 回答
9

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

网络BIOS: $env:userdomain

全名: $env:userdnsdomain

要查看所有值:

dir env:  (no $)
于 2013-01-31T03:56:41.127 回答
5
import-module activedirectory
(Get-ADDomain -Identity (Get-WmiObject Win32_ComputerSystem).Domain).NetBIOSName
于 2011-10-20T13:24:30.360 回答
3

这里

# 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)
于 2011-04-27T03:06:05.270 回答
3

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

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

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

于 2012-05-25T13:53:59.863 回答
1

使用 Active Directory Cmdlet Get-ADDomain:

(Get-ADDomain -Current LocalComputer).NetBIOSName
于 2015-09-03T00:07:55.647 回答
0

下面的 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- powershell 中的计算机名称?forum=ITCG

上面的链接有线程和对话。

于 2014-05-24T01:59:45.693 回答
0

这是另一种比 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
于 2022-02-15T14:04:28.413 回答
0

使用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)
  }
}
于 2018-05-02T18:59:26.387 回答
0

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

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

将返回

HostName      : SurfaceBook
DomainName    : mydomain.com
NodeType      : Hybrid
DhcpScopeName :
IsWinsProxy   : False
于 2019-12-18T14:12:07.277 回答
0

使用 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 确实返回计算机的短域]

于 2021-04-06T15:29:31.053 回答