Powershell: Get FQDN Hostname

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

    Here is a way to determine the FQDN of a server based on the "Name" and "DistinguishedName". Works for multiple domains:

    $server = Get-ADComputer serverName -Server domainName -Properties * | select Name, DistinguishedName
    $domain = $server.DistinguishedName -split ","
    $domain = $domain | ? {$_ -like 'DC=*'}
    $domain = $domain -join "."
    $domain = $domain -replace "DC="
    $FQDN = $server.Name + "." + $domain
    
    0 讨论(0)
  • 2021-01-30 13:21

    I have the following add.. I need to separate out the dns suffix from the hostname.. and I only "know" the servers alias shortname... and want to know what the dns suffix is

    #example:
    #serveralias:     MyAppServer.us.fred.com
    #actualhostname:  server01.us.fred.com 
    #I "know":        "MyAppServer" .. I pass this on as an env var called myjumpbox .. this could also be $env:computername
    
    
    $forname                 = $env:myjumpbox
    $fqdn                    = [System.Net.Dns]::GetHostByName($forname).Hostname
    $shortname               = $fqdn.split('.')[0]
    $domainname              = $fqdn -split $fqdn.split('.')[0]+"."
    $dnssuf                  = $domainname[1]  
    " name parts are- alias: " + $forname + " actual hostname: " + $shortname + " suffix: " + $dnssuf
    
    #returns
    
    name parts are- alias: MyAppServer actual hostname: server01 suffix: us.fred.com
    
    0 讨论(0)
  • 2021-01-30 13:21

    A cleaner format FQDN remotely

    [System.Net.Dns]::GetHostByName('remotehost').HostName
    
    0 讨论(0)
提交回复
热议问题