Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 559 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NetBIOS domain of computer in PowerShell

#1
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][1], but apparently [ADSystemInfo isn't very nice to use][2] in PowerShell.

*Update*

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

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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#2
From [Here][1]

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


[1]:

[To see links please register here]

Reply

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

Reply

#4
import-module activedirectory
(Get-ADDomain -Identity (Get-WmiObject Win32_ComputerSystem).Domain).NetBIOSName
Reply

#5
OP is after "computer domain" so the answer would be `$GetComputerDomain` (below) but I will add the $GetUserDomain also for reference.

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

I find the wmi (gwmi) option to be extremely slow, especially, when you are querying the Win32_NTDomain class. I have a multi-trusted domain environment and it takes forever when I just need that simple info quick.
Reply

#6
The below powershell command works great! I tested after trying various solutions.

If you use the following .Net command:

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

It works too, but it is using DNS to resolve, in my case, we have WINS setup to support an application that requires it, so can't use it. Below is what I ended up using as part of a script I use to check for WINS registration for each client:

$IPAddress = "<enterIPAddress>" (remove brackets, just enter IP address)

(nbtstat -A $IPAddress | ?{$_ -match '\<00\> UNIQUE'}).Split()[4]

[To see links please register here]


The above link has the thread and conversation.
Reply

#7
Use the Active Directory Cmdlet Get-ADDomain:

(Get-ADDomain -Current LocalComputer).NetBIOSName
Reply

#8
Use `env:` to get environment settings through PowerShell

NetBIOS: `$env:userdomain`

FQDN: `$env:userdnsdomain`

To see all the values:

dir env: (no $)

Reply

#9
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)
}
}
Reply

#10
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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through