Powershell: Get FQDN Hostname

后端 未结 15 1678
长发绾君心
长发绾君心 2021-01-30 12:24

I want to retrieve the FQDN name of windows server via powershell script. I have found 2 solution so far:

$server =  Invoke-Command -ScriptBlock {hostname}
         


        
15条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 13:00

    To get FQDN of local computer:

    [System.Net.Dns]::GetHostByName($env:computerName)
    

    or

    [System.Net.Dns]::GetHostByName($env:computerName).HostName
    

    To get FQDN of Remote computer:

    [System.Net.Dns]::GetHostByName('mytestpc1')
    

    or

    For better formatted value use:

    [System.Net.Dns]::GetHostByName('mytestpc1').HostName
    
    • For remote machines make sure host is reachable.

提交回复
热议问题