windows Powershell:获取 FQDN 主机名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12268885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Powershell: Get FQDN Hostname
提问by slayedbylucifer
I want to retrieve the FQDN name of windows server via powershell script. I have found 2 solution so far:
我想通过 powershell 脚本检索 Windows 服务器的 FQDN 名称。到目前为止,我已经找到了 2 个解决方案:
$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 :(
上面两行会给我 FQDN 但这看起来真的很讨厌代码来检索主机名:(
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.
所以,我的问题是,有没有更简单的方法可以在 powershell 中获取 FQDN。我是一名 bash/perl 编码员,最近开始使用 powershell .. 所以发现这很困难。
Thanks.
谢谢。
回答by perilbrain
To get FQDN of local computer:
获取本地计算机的 FQDN:
[System.Net.Dns]::GetHostByName($env:computerName)
or
或者
[System.Net.Dns]::GetHostByName($env:computerName).HostName
To get FQDN of Remote computer:
获取远程计算机的 FQDN:
[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.
- 对于远程机器,请确保主机可访问。
回答by aquinas
How about: "$env:computername.$env:userdnsdomain"
怎么样: "$env:computername.$env:userdnsdomain"
This actually onlyworks 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.
这实际上仅在用户登录到域(即没有本地帐户)、登录到与服务器相同的域时才有效,并且不适用于脱节的命名空间 AD 配置。
Use this as referenced in another answer:
使用另一个答案中的引用:
$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
Write-Host $myFQDN
写主机 $myFQDN
回答by user3632452
Local Computer FQDN via dotNet class
通过 dotNet 类的本地计算机 FQDN
[System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
or
或者
[System.Net.Dns]::GetHostEntry([string]"localhost").HostName
Reference:
参考:
note: GetHostByName method is obsolete
注意:GetHostByName 方法已过时
Local computer FQDN via WMI query
通过 WMI 查询的本地计算机 FQDN
$myFQDN=(Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
Write-Host $myFQDN
Reference:
参考:
回答by Alexander Petrovskiy
[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)
$env:computerName
返回主机的 NetBIOS 名称,以便前面的两个示例都返回 netbioshostname.domainsuffix(不是 FQDN!)而不是 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)
例如,主机有 FQDN aa-w2k12sv-storage.something.com 和 NetBIOS 名称 aa-w2k12sv-stor(一个简单的例子,我通常会更改 NetBIOS 名称)
the hostname utility returns dnshostname, i.e., the first part of FQDN and code
主机名实用程序返回 dnshostname,即 FQDN 和代码的第一部分
[System.Net.Dns]::GetHostByName((hostname)).HostName
returns the right FQDN
返回正确的 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...
注释:切勿使用相同的 AD 域和主机的 NetBIOS 和 DNS 名称。如果您或第 3 方应用程序写入日志:“无法连接到 hostname.domainsuffix”,它尝试解析什么名称?如果您在日志中看到“无法连接到netbiosname.domainsuffix”,无疑是一个懒惰的程序员在NetBIOS 名称后添加了域后缀,您确定这是一个错误,可以开票强制他们修复问题...
回答by Tyler Szabo
It can also be retrieved from the registry:
它也可以从注册表中检索:
Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' |
% { $_.'NV HostName', $_.'NV Domain' -join '.' }
回答by peter_fongsam
Here's the method that I've always used:
这是我一直使用的方法:
$fqdn= $(ping localhost -n 1)[1].split(" ")[1]
回答by SnakeNET
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...
$VM 是向上还是向下都没有关系......
回答by JABIR ABDUL RAHIMAN
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(".")
回答by StevenMMK
Here is a way to determine the FQDN of a server based on the "Name" and "DistinguishedName". Works for multiple domains:
这是一种根据“名称”和“可分辨名称”确定服务器 FQDN 的方法。适用于多个域:
$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
回答by CBB
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
我有以下添加.. 我需要从主机名中分离出 dns 后缀.. 我只“知道”服务器别名短名......并且想知道 dns 后缀是什么
#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