如何从 PowerShell 获取当前计算机的 NetBIOS(又名“短”)域名?
$ENV:USERDOMAIN 显示当前用户的域,但我想要当前机器所属的域。
我发现您可以在 VBScript 中轻松地做到这一点,但显然 ADSystemInfo 在 PowerShell 中使用起来不太好。
更新
这是我的最终解决方案,结合了使用 Win32_NTDomain 的建议,但过滤到当前计算机的域
$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$domain = $wmiDomain.DomainName
在大多数情况下,默认 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
使用
env:
通过 PowerShell 获取环境设置
NetBIOS:
$env:userdomain
FQDN:
$env:userdnsdomain
查看所有值:
dir env: (no $)
import-module activedirectory
(Get-ADDomain -Identity (Get-WmiObject Win32_ComputerSystem).Domain).NetBIOSName
从这里
# 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)
OP 在“计算机域”之后,所以答案是
$GetComputerDomain
(如下),但我也会添加 $GetUserDomain 供参考。
$GetComputerDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()).Name
$GetUserDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name
我发现 wmi (gwmi) 选项非常慢,特别是当您查询 Win32_NTDomain 类时。我有一个多信任的域环境,当我只需要快速获得简单的信息时,它需要很长时间。
使用 Active Directory Cmdlet Get-ADDomain:
(Get-ADDomain -Current LocalComputer).NetBIOSName
这也可以通过使用.NET框架来完成(比WMI快得多)
PS > [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
会回来
HostName : SurfaceBook
DomainName : mydomain.com
NodeType : Hybrid
DhcpScopeName :
IsWinsProxy : False
这是另一种比 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
使用 ADSystemInfo COM 对象应该可以工作,Win32_NTDomain 查找不会出现延迟:
[System.__ComObject].InvokeMember("DomainShortName", [System.Reflection.BindingFlags]::GetProperty, $null, (New-Object -ComObject "ADSystemInfo"), $null)
此 COM 对象还提供其他与 AD 相关的属性:
https://learn.microsoft.com/en-us/windows/win32/adsi/iadsadsysteminfo-property-methods
[已编辑 - 最初包含 WinNTSystemInfo COM 对象的代码,但评论者指出这仅返回用户的短域 - 但 ADSystemInfo 确实返回计算机的短域]
下面的 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]
上面的链接有主题和对话。
使用
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)
}
}