如何从powershell获取HostName?

时间:2018-02-15 11:46:35

标签: linux powershell powershell-core

如何从powershell获取dur的{​​{3}}?

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,这将是首选输出。

1 个答案:

答案 0 :(得分:1)

感谢bluuf让我参与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>