NetBIOS domain of computer in PowerShell

2020-03-01 05:53发布

How can I get the NetBIOS (aka 'short') domain name of the current computer from PowerShell?

$ENV:USERDOMAIN displays the domain of the current user, but I want the domain that the current machine is a member of.

I've discovered you can do it pretty easily in VBScript, but apparently ADSystemInfo isn't very nice to use in PowerShell.

Update

Here's my final solution incorporating the suggestion of using Win32_NTDomain, but filtering to the current machine's domain

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

9条回答
虎瘦雄心在
2楼-- · 2020-03-01 06:24

Use env: to get environment settings through PowerShell

NetBIOS: $env:userdomain

FQDN: $env:userdnsdomain

To see all the values:

dir env:  (no $)
查看更多
孤傲高冷的网名
3楼-- · 2020-03-01 06:30

Use the Active Directory Cmdlet Get-ADDomain:

(Get-ADDomain -Current LocalComputer).NetBIOSName
查看更多
放我归山
4楼-- · 2020-03-01 06:32

This can also be done by using .NET framework (which is much faster than WMI)

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

Will return

HostName      : SurfaceBook
DomainName    : mydomain.com
NodeType      : Hybrid
DhcpScopeName :
IsWinsProxy   : False
查看更多
成全新的幸福
5楼-- · 2020-03-01 06:34

In most cases, the default NetBIOS domain name is the leftmost label in the DNS domain name up to the first 15 bytes (NetBIOS names have a limit of 15 bytes). The NetBIOS domain name may be changed during the installation of the Active Directory, but it cannot be changed.

The WIN32_ComputerSystem WMI object gives informations on a Windows computer

PS C:\> Get-WmiObject Win32_ComputerSystem

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

So the domain Name is given by :

PS C:\> (gwmi WIN32_ComputerSystem).Domain

But in domain installation, the DNS name is given. In this case, you can use nbtstat -n command to find the NetBIOS domain name which is displayed like this <DOMAIN><1B>.

The PowerShell Command may be :

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

Here is another way using WMI

PS C:\> (gwmi Win32_NTDomain).DomainName
查看更多
走好不送
6楼-- · 2020-03-01 06:34

Using NetGetJoinInformation and 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)
  }
}
查看更多
萌系小妹纸
7楼-- · 2020-03-01 06:39

From 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)
查看更多
登录 后发表回答