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:
  • 257 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell: Get FQDN Hostname

#1
I want to retrieve the FQDN name of windows server via powershell script. I have found 2 solution so far:

$server = Invoke-Command -ScriptBlock {hostname}

Above line will print just the short name of the server

$sysinfo = Get-WmiObject -Class Win32_ComputerSystem
$server = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain

Above two line will get me the FQDN but this looks really nasty code to retrieve just the hostname :(

So, My question is, is there an easier way to get the FQDN in powershell. I am a bash/perl coder and recently picked up powershell.. so finding it difficult.

Thanks.
Reply

#2
[System.Net.Dns]::GetHostByName((hostname)).HostName

`$env:computerName` returns NetBIOS name of the host, so that both previous examples return
netbioshostname.domainsuffix (not FQDN!)
instead of
dnshostname.domainsuffix (FQDN)

for example, host has
FQDN
aa-w2k12sv-storage.something.com
and NetBIOS name
aa-w2k12sv-stor (an easy case, I usually change NetBIOS name)

the hostname utility returns dnshostname, i.e., the first part of FQDN and code

[System.Net.Dns]::GetHostByName((hostname)).HostName

returns the right FQDN

Comment: never use the same NetBIOS and DNS names of AD domains and hosts. If your or 3rd party application writes to the log: "cannot connect to hostname.domainsuffix", what name it tries to resolve? If you see in the log "cannot connect to netbiosname.domainsuffix", no doubt, a lazy programmer added domain suffix to the NetBIOS name and you are sure, this is a bug, and can open a ticket to force them to fix the issue...
Reply

#3
Local Computer FQDN via dotNet class

[System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
or

[System.Net.Dns]::GetHostEntry([string]"localhost").HostName

Reference:

[Dns Methods (System.Net)]

note: GetHostByName method is obsolete

----------

Local computer FQDN via WMI query

$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
Write-Host $myFQDN

Reference:

[Win32_ComputerSystem class]

[Win32_ComputerSystem Class]: <http://msdn.microsoft.com/en-us/library/aa394102%28v=vs.85%29.aspx>

[Dns Methods (System.Net)]: <http://msdn.microsoft.com/en-us/library/System.Net.Dns_methods%28v=vs.110%29.aspx>
Reply

#4
"$env:computername.$env:userdnsdomain"

will work if separated out like this

"$env:computername"+"$env:userdnsdomain"
Reply

#5
I use the following syntax :

$Domain=[System.Net.Dns]::GetHostByName($VM).Hostname.split('.')
$Domain=$Domain[1]+'.'+$Domain[2]
it does not matter if the $VM is up or down...
Reply

#6
How about this

$FQDN=[System.Net.Dns]::GetHostByName($VM).Hostname.Split('.')
[int]$i = 1
[int]$x = 0
[string]$Domain = $null
do {
$x = $i-$FQDN.Count
$Domain = $Domain+$FQDN[$x]+"."
$i = $i + 1
} until ( $i -eq $FQDN.Count )
$Domain = $Domain.TrimEnd(".")
Reply

#7
Here is a way to determine the FQDN of a server based on the "Name" and "DistinguishedName". Works for multiple domains:

$server = Get-ADComputer serverName -Server domainName -Properties * | select Name, DistinguishedName
$domain = $server.DistinguishedName -split ","
$domain = $domain | ? {$_ -like 'DC=*'}
$domain = $domain -join "."
$domain = $domain -replace "DC="
$FQDN = $server.Name + "." + $domain
Reply

#8
I have the following add.. I need to separate out the dns suffix from the hostname.. and I only "know" the servers alias shortname... and want to know what the dns suffix is

#example:
#serveralias: MyAppServer.us.fred.com
#actualhostname: server01.us.fred.com
#I "know": "MyAppServer" .. I pass this on as an env var called myjumpbox .. this could also be $env:computername


$forname = $env:myjumpbox
$fqdn = [System.Net.Dns]::GetHostByName($forname).Hostname
$shortname = $fqdn.split('.')[0]
$domainname = $fqdn -split $fqdn.split('.')[0]+"."
$dnssuf = $domainname[1]
" name parts are- alias: " + $forname + " actual hostname: " + $shortname + " suffix: " + $dnssuf

#returns

name parts are- alias: MyAppServer actual hostname: server01 suffix: us.fred.com





Reply

#9
To get FQDN of local computer:

[System.Net.Dns]::GetHostByName($env:computerName)

or

[System.Net.Dns]::GetHostByName($env:computerName).HostName

To get FQDN of Remote computer:

[System.Net.Dns]::GetHostByName('mytestpc1')

or

For better formatted value use:

[System.Net.Dns]::GetHostByName('mytestpc1').HostName

* For remote machines make sure host is reachable.
Reply

#10
It can also be retrieved from the registry:

Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' |
% { $_.'NV HostName', $_.'NV Domain' -join '.' }
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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