Link to home
Start Free TrialLog in
Avatar of creative555
creative555

asked on

What is the best way to get computer's domain into a variable?

Hello,

What is the best way to get computer's domain into a variable?
So for example, all our computers are joined to the target. So I want to query that value and record domain FQDN and Domain NB into  variables. Similar to what I found to do for username. However, I need to get the domain from computer and not user. Unfortunately, there is no such a variable for a computer only for the user. Please advise.

$domainDNS = $env:USERDNSDOMAIN (similar to this)
$domainNB = $env:USERDOMAIN  
[string] $SQLName=$env:computername
[string] $SQLFQDN = $SQLName + "." + $domainDNS
Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Please see here: http://blog.riccardocelesti.it/find-computer-name-and-domain-using-powershell/


(Get-WmiObject Win32_ComputerSystem).Domain
Avatar of oBdA
oBdA

$domainDNS = (Get-WmiObject -Query "SELECT Domain FROM Win32_ComputerSystem").Domain
$domainNB = (Get-WmiObject -Query "Select DomainName FROM Win32_NTDomain  WHERE DnsForestName='$($domainDNS)'").DomainName

Open in new window

Just for something completely different, here's how we used to do it in vbScript (but in PowerShell):

$obj = New-Object -ComObject ADSystemInfo
$type = $obj.GetType()
$domainDNS = $type.InvokeMember( 'DomainDNSName', 'GetProperty', $null, $obj, $null )
$domainNB = $type.InvokeMember( 'DomainShortName', 'GetProperty', $null, $obj, $null )
$obj = $type = $null

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacob Durham
Jacob Durham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of creative555

ASKER

get-addomain commands worked best.

This didn't give me any output

PS Env:\> $domainNB = (Get-WmiObject -Query "Select DomainName FROM Win32_NTDomain  WHERE DnsForestName='$($domainDNS)'").DomainName

PS Env:\> $domainNB


this worked but only for FQDN and not NETBIOS. what would be the NB command? I didn't see NB name when I did the following:
Get-WmiObject Win32_ComputerSystem | select *


This looks like too much code if you can just use one line ;) Why would you use all those?

$obj = New-Object -ComObject ADSystemInfo
$type = $obj.GetType()
$domainDNS = $type.InvokeMember( 'DomainDNSName', 'GetProperty', $null, $obj, $null )
$domainNB = $type.InvokeMember( 'DomainShortName', 'GetProperty', $null, $obj, $null )
$obj = $type = $null
Nothing wrong with Get-AdDomain - however, it requires the Active Directory RSAT tools to be installed. Perhaps someone should've asked that question, but it's not an assumption that should be made for everyone's workstation. Few people will have it installed. It'll even be installed on few servers. :-)

Here is the proper other query:
$domainNB = ( Get-WMIObject Win32_NTDomain |? { $_.Status -eq 'OK' -and $_.DomainName } ).DomainName

Open in new window

Ah, I see now what @oBdA was doing, for multi-domain forests what he wrote should work.
That
$domainNB = (Get-WmiObject -Query "Select DomainName FROM Win32_NTDomain  WHERE DnsForestName='$($domainDNS)'").DomainName
obviously only works if you run the other command first, since it uses the domain's DNS name::
$domainDNS = (Get-WmiObject -Query "SELECT Domain FROM Win32_ComputerSystem").Domain
And then it works just fine.
Why more than one line? Because your question sounded like you needed a generally available solution, not one that requires that the ActiveDirectory module is installed (which, as Michael already pointed out), in most cases isn't.

Michael,
that's not really the "proper" query - Win32_NTDomain returns trusted domains as well, plus one for the local machine if run on a member server.
it will return multiple results if there's a trust with another domain.
I corrected myself. :-)
I think smth wrong with the quotes here. Maybe that is why I am not getting anything back. Sorry. I am newby.
Could you please correct.
Then it is makes sense since it doesn't require AD module.
thank you so much for expanding on this

'$($domainDNS)'").DomainName

$domainNB = (Get-WmiObject -Query "Select DomainName FROM Win32_NTDomain  WHERE DnsForestName='$($domainDNS)'").DomainName
There's nothing wring with the quotes. Copy and paste exactly as it is.
$domainDNS = (Get-WmiObject -Query "SELECT Domain FROM Win32_ComputerSystem").Domain
$domainNB = (Get-WmiObject -Query "SELECT DomainName FROM Win32_NTDomain WHERE DnsForestName='$($domainDNS)'").DomainName

Open in new window

Here's what happens with those quotes:
"SELECT DomainName FROM Win32_NTDomain WHERE DnsForestName='$($domainDNS)'"
^                                                          ^             ^^
+-- Opening string (WQL Query)                             |             |+-- Closing string (WQL query)
                                                           |             +--- Closing search string inside WQL query
                                                           +----------------- Opening search string inside WQL query

Open in new window