簡體   English   中英

PowerShell中計算機的NetBIOS域

[英]NetBIOS domain of computer in PowerShell

如何從 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

OP 在“計算機域”之后,所以答案是$GetComputerDomain (如下),但我也會添加 $GetUserDomain 以供參考。

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

我發現 wmi (gwmi) 選項非常慢,尤其是當您查詢 Win32_NTDomain 類時。 我有一個多信任域環境,當我只需要快速獲取簡單信息時,它需要永遠。

這里

# 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)

使用 Active Directory Cmdlet Get-ADDomain:

(Get-ADDomain -Current LocalComputer).NetBIOSName

下面的 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 中的計算機名稱?論壇=ITCG

上面的鏈接有線程和對話。

使用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)
  }
}

這也可以通過使用 .NET 框架來完成(比 WMI 快得多)

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

將返回

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

使用 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

這是另一種比 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM