0

The FQDN for this machine:

thufir@dur:~$ 
thufir@dur:~$ hostname --fqdn
dur.bounceme.net
thufir@dur:~$ 

Yes...working directly with powershell gives the FQDN of dur.bounceme.net okay:

thufir@dur:~/powershell$ 
thufir@dur:~/powershell$ pwsh
PowerShell v6.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /home/thufir/powershell> 
PS /home/thufir/powershell> [System.Net.Dns]::GetHostByName((hostname)).HostName                                        
dur.bounceme.net
PS /home/thufir/powershell> 

but what if I want to iterate over an array? How do I get the FQDN to show as dur.bounceme.net?

thufir@dur:~/powershell$ 
thufir@dur:~/powershell$ ./hostname.ps1 
dur.bounceme.net
beginning loop
google.com
Exception calling "GetHostEntry" with "1" argument(s): "No such device or address"
At /home/thufir/powershell/hostname.ps1:14 char:3
+   $fqdn = [System.Net.Dns]::GetHostEntry($i).HostName
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ExtendedSocketException

google.com
localhost
end
thufir@dur:~/powershell$ 

script:

#!/usr/bin/pwsh -Command


#hostname is a reserved variable name?

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


"beginning loop"

$hosts = ("google.com", "hostname", "localhost")

foreach($i in $hosts) {
  $fqdn = [System.Net.Dns]::GetHostEntry($i).HostName
  write-host $fqdn
}


"end"

I've tried removing quote marks from around hostname and prepending the dollar sign $. This is a reserved word?

Bonus points for explaining the terminology involved.

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    What result do you expect to get about _hostname_? – vonPryz Feb 16 '18 at 06:50
  • @vonPryz dur.bounceme.net is the FQDN. I updated the question a bit. How do I get the FQDN output from powershell as a script? Please elaborate -- not sure what you're getting at. – Thufir Feb 16 '18 at 07:00
  • I'm probably nitpicking, but *`dur.bounceme.net`* is ***not*** a FQDN. FQDN's end in dot like *`dur.bounceme.net.`*.The dot indicates the top of the DNS tree. Your hostname allows search domains to be appended, like *`dur.bounceme.net.example.com`*. Also see [Fully Qualified Domain Name](https://en.wikipedia.org/wiki/Fully_qualified_domain_name) on Wikipedia; or W. Richard Stevens' [TCP/IP Illustrated](https://www.amazon.com/dp/0201633469) for the win! – jww Feb 16 '18 at 07:48
  • @jww err, are you saying that `google.com` isn't a FQDN but that `google.com.` is? – Thufir Feb 16 '18 at 07:57
  • @Thufir - yes, exactly. But also see issues like [hostnamed does not like fqdns with trailing dots](https://github.com/systemd/systemd/issues/6369). Systemd has the resolver screwed up. I don't think Poettering understands what a FQDN means and how he changed the behaviors. I've been waiting for a security vulnerability to surface because of it. – jww Feb 16 '18 at 08:03

2 Answers2

1

You are using hostname as a string and that string is not in your hosts file, like localhost is, it will fail.

If you are after default localhost names, then they are:

'127.0.0.1'
$env:COMPUTERNAME
'localhost'

So, you shoud do this

$TargetHosts = ('stackoverflow.com','google.com', $env:COMPUTERNAME,'localhost','127.0.0.1')

foreach($TargetHost in $TargetHosts) 
{ ( $fqdn = [Net.Dns]::GetHostEntry($TargetHost).Hostname ) }

stackoverflow.com
google.com
WS01
WS01
WS01

See also this post about use the native Resolve-DnsName cmdlet vs the .NET libraries.

Why not just use the built-in DNS cmdlets? Or is there a particular reason you are traveling down the raw .Net path? Code project, homework assignment, curiosity?

powershell how to resolve name to IP address using Windows method

postanote
  • 15,138
  • 2
  • 14
  • 25
  • curiosity. this script generates an error for the $env:COMPUTERNAME for me -- probably because it's Linux and not windows (?). I'll update the question. thx. – Thufir Feb 16 '18 at 07:43
  • 1
    Correct, $env:COMPUTERNAME is a Windows PoSH default variable from the $env:\ PSDrive. On OSX/*NIX, in PS terminal, that is not a thing. The iteration of what I show does work on OSX/*NIX, as long as you do not use the $env:COMPUTERNAME or hostname. hostname on OSX, does return the local computer name, it does not when doing what is being tried here. So for OSX/*NIX hosts, you are left with localhost and 127.0.0.1. I just did all this in OSX to validate. – postanote Feb 16 '18 at 08:20
  • maybe going off track here. The OS command `hostname --fqdn` returns a string. Can that output not be passed or piped to `GetHostByName`? Along with other arbitrary strings in an array. – Thufir Feb 17 '18 at 12:28
1

It seems that there is confusion about what hostname does and what's the difference between a command and a string. Let's see the first part that works:

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

Powershell parses this as

Run command hostname, 
Call GetHostByName(), pass hostname's output as a parameter to the call
from that result, show the HostName attribute

Whilst in the foreach loop, the parameters are passed as strings. Thus in the hostname case:

$i <-- hostname
[System.Net.Dns]::GetHostEntry($i).HostName

is being parsed as

Call GetHostEntry("hostname")
from that result, show the HostName attribute
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • so when PS runs the command hostname this returns an object? That object is a parameter for the call to GetHostByName? If so, then: how do I get the object returned by the command `hostname`? – Thufir Feb 16 '18 at 07:45
  • 1
    @Thufir Yes, running the _command_ hostname will return a string object that contains your _computer's_ hostname. (Maybe it doesn't help that the word _hostname_ is a [homonym](https://en.wikipedia.org/wiki/Homonym).) When you pass a list of strings, there is no way to know that one is supposed to be name for executable to be called and others are just strings. – vonPryz Feb 16 '18 at 07:59
  • Stuck on *command* meaning. You mean the OS command on the console? Just `hostname` on the OS. – Thufir Feb 17 '18 at 12:23