Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Get FQDN Hostname

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.

like image 928
slayedbylucifer Avatar asked Sep 04 '12 17:09

slayedbylucifer


People also ask

How do I find the FQDN of a remote server?

On the Windows Taskbar, click Start > Programs > Administrative Tools > Active Directory Domains and Trusts. In the left pane of the Active Directory Domains and Trusts dialog box, look under Active Directory Domains and Trusts. The FQDN for the computer or computers is listed.

How do I find the FQDN of an IP address?

Type "ipconfig" and press "Enter." This displays the IP address for your Windows server. Use this IP address to view the fully qualified domain name of the server.


2 Answers

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.
like image 183
perilbrain Avatar answered Sep 23 '22 09:09

perilbrain


How about: "$env:computername.$env:userdnsdomain"

This actually only works if the user is logged into a domain (i.e. no local accounts), logged into the same domain as the server, and doesn't work with disjointed name space AD configurations.

Use this as referenced in another answer:

$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain ; Write-Host $myFQDN 
like image 32
aquinas Avatar answered Sep 21 '22 09:09

aquinas