2

How do I get the hostname for dur from powershell?

PS /home/thufir/powershell> 
PS /home/thufir/powershell> ./hostname.ps1                                                                              
google.com
localhost
PS /home/thufir/powershell> 
PS /home/thufir/powershell> cat ./hostname.ps1                                                                          
$hosts = ("google.com", "localhost")

foreach($i in $hosts) {
  $fqdn = [System.Net.Dns]::GetHostEntry($i).HostName
  write-host $fqdn
}
PS /home/thufir/powershell> 
PS /home/thufir/powershell> hostname                                                                                    
dur
PS /home/thufir/powershell> 

The FQDN for the system is actually dur.bounceme.net, which would be the preferred output.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 2
    Usually the environment variable $env:Computername holds the hostname. I'm not sure if this variable also works on Linux (I don't have a Linux VM with Powershell installed on it to test this on at the moment) – bluuf Feb 15 '18 at 12:01

1 Answers1

1

Thanks to bluuf for setting me on the right track:

PS /home/thufir/powershell> 
PS /home/thufir/powershell> [System.Net.Dns]::GetHostByName((hostname)).HostName                                        
dur.bounceme.net
PS /home/thufir/powershell> 
PS /home/thufir/powershell> hostname                                                                                    
dur
PS /home/thufir/powershell> 
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • Does it work for windows as well to get the hostname? It should, see the right track link.. – Timo Jun 08 '21 at 19:12