2

如何从 powershell获取主机名dur

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> 

系统的 FQDN 实际上是dur.bounceme.net,这将是首选输出。

4

1 回答 1

1

感谢 bluuf 让我走上正轨

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> 
于 2018-02-15T12:25:39.050 回答