可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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}
Above line will print just the short name of the server
$sysinfo = Get-WmiObject -Class Win32_ComputerSystem $server = “{0}.{1}” -f $sysinfo.Name, $sysinfo.Domain
Above two line will get me the FQDN but this looks really nasty code to retrieve just the hostname :(
So, My question is, is there an easier way to get the FQDN in powershell. I am a bash/perl coder and recently picked up powershell.. so finding it difficult.
Thanks.
回答1:
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
回答2:
To get FQDN of local computer:
[System.Net.Dns]::GetHostByName(($env:computerName))
or
[System.Net.Dns]::GetHostByName(($env:computerName)) | FL HostName | Out-String | %{ "{0}" -f $_.Split(':')[1].Trim() };
To get FQDN of Remote computer:
[System.Net.Dns]::GetHostByName("mytestpc1")
or
For better formatted value use:
[System.Net.Dns]::GetHostByName("mytestpc1") | FL HostName | Out-String | %{ "{0}" -f $_.Split(':')[1].Trim() };
- For remote machines make sure host is reachable.
回答3:
Local Computer FQDN via dotNet class
[System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
or
[System.Net.Dns]::GetHostEntry([string]"localhost").HostName
Reference:
Dns Methods (System.Net)
note: GetHostByName method is obsolete
Local computer FQDN via WMI query
$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain Write-Host $myFQDN
Reference:
Win32_ComputerSystem class
回答4:
[System.Net.Dns]::GetHostByName((hostname)).HostName
$env:computerName
returns NetBIOS name of the host, so that both previous examples return netbioshostname.domainsuffix (not FQDN!) instead of dnshostname.domainsuffix (FQDN)
for example, host has FQDN aa-w2k12sv-storage.something.com and NetBIOS name aa-w2k12sv-stor (an easy case, I usually change NetBIOS name)
the hostname utility returns dnshostname, i.e., the first part of FQDN and code
[System.Net.Dns]::GetHostByName((hostname)).HostName
returns the right FQDN
Comment: never use the same NetBIOS and DNS names of AD domains and hosts. If your or 3rd party application writes to the log: "cannot connect to hostname.domainsuffix", what name it tries to resolve? If you see in the log "cannot connect to netbiosname.domainsuffix", no doubt, a lazy programmer added domain suffix to the NetBIOS name and you are sure, this is a bug, and can open a ticket to force them to fix the issue...
回答5:
I use the following syntax :
$Domain=[System.Net.Dns]::GetHostByName($VM).Hostname.split('.') $Domain=$Domain[1]+'.'+$Domain[2]
it does not matter if the $VM is up or down...
回答6:
"$env:computername.$env:userdnsdomain"
will work if separated out like this
"$env:computername"+"$env:userdnsdomain"
回答7:
How about this
$FQDN=[System.Net.Dns]::GetHostByName($VM).Hostname.Split('.') [int]$i = 1 [int]$x = 0 [string]$Domain = $null do { $x = $i-$FQDN.Count $Domain = $Domain+$FQDN[$x]+"." $i = $i + 1 } until ( $i -eq $FQDN.Count ) $Domain = $Domain.TrimEnd(".")
回答8:
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
回答9:
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