how to get the HostName from powershell?

微笑、不失礼 提交于 2019-12-24 03:00:49

问题


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.


回答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> 


来源:https://stackoverflow.com/questions/48806507/how-to-get-the-hostname-from-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!