Powershell: Get FQDN Hostname

后端 未结 15 1644
长发绾君心
长发绾君心 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.
    0 讨论(0)
  • 2021-01-30 13:06

    How about: "$env:computername.$env:userdnsdomain"

    This actually only works if the user is logged into a domain (i.e. no local accounts), logged into the same domain as the server, and doesn't work with disjointed name space AD configurations.

    Use this as referenced in another answer:

    $myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
    

    Write-Host $myFQDN

    0 讨论(0)
  • 2021-01-30 13:06
    "$env:computername.$env:userdnsdomain" 
    

    will work if separated out like this

    "$env:computername"+"$env:userdnsdomain"
    
    0 讨论(0)
  • 2021-01-30 13:06

    If you have more than one network adapter and more than one adapter is active (f.e WLAN + VPN) you need a bit more complex check. You can use this one-liner:

    [System.Net.DNS]::GetHostByAddress(([System.Net.DNS]::GetHostAddresses([System.Environment]::MachineName) | Where-Object { $_.AddressFamily -eq "InterNetwork" } | Select-Object IPAddressToString)[0].IPAddressToString).HostName.ToLower()
    
    0 讨论(0)
  • 2021-01-30 13:08
    (Get-ADComputer $(hostname)).DNSHostName
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题