Powershell:获取 FQDN 主机名 [英] Powershell: Get FQDN Hostname
问题描述
我想通过 powershell 脚本检索 Windows 服务器的 FQDN 名称.到目前为止,我找到了 2 个解决方案:
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
上面的两行会给我 FQDN 但这看起来非常讨厌的代码只检索主机名:(
Above two line will get me the FQDN but this looks really nasty code to retrieve just the hostname :(
所以,我的问题是,有没有更简单的方法可以在 powershell 中获取 FQDN.我是一名 bash/perl 编码员,最近开始使用 powershell .. 所以觉得很难.
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.
谢谢.
推荐答案
How about: "$env:computername.$env:userdnsdomain"
How about: "$env:computername.$env:userdnsdomain"
这实际上仅在用户登录到域(即没有本地帐户)、登录到与服务器相同的域时有效,并且不适用于脱节的命名空间 AD 配置.
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
这篇关于Powershell:获取 FQDN 主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!