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.
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:
So, you shoud do this
See also this post about use the native Resolve-DnsName cmdlet vs the .NET libraries.
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:
Powershell parses this as
Whilst in the foreach loop, the parameters are passed as strings. Thus in the hostname case:
is being parsed as