Powershell: Get FQDN Hostname

后端 未结 15 1652
长发绾君心
长发绾君心 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:08

    to get the fqdn corresponding to the first IpAddress, it took this command:

    PS C:\Windows\system32> [System.Net.Dns]::GetHostByAddress([System.Net.Dns]::GetHostByName($env:computerName).AddressList[0]).HostName
    WIN-1234567890.fritz.box
    

    where [System.Net.Dns]::GetHostByName($env:computerName).AddressList[0] represents the first IpAddress-Object and [System.Net.Dns]::GetHostByAddress gets the dns-object out of it.

    If I took the winning solution on my standalone Windows, I got only:

    PS C:\Windows\system32> (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
    WIN-1234567890.WORKGROUP
    

    that's not what I wanted.

提交回复
热议问题