Coz*_*zmo 4 domain-name-system powershell export windows-server-2008-r2
虽然我对 powerShell v3 比较陌生,但我可以用它做一些事情,但是,我没有想到的一件事是如何拉出我们的 DNS 管理器(Server 2008 R2)中列出的所有主机。我不需要设置或删除任何内容,只需将列表查询到文本文件中即可。令人惊讶的是,我没有找到一种方法来做到这一点。请问有人知道怎么做吗?
我以前使用过 DNSShell。 http://dnsshell.codeplex.com/。要获取区域中的所有 A 记录,您可以执行以下操作:
Get-DnsRecord -RecordType A -ZoneName FQDN -Server ServerName
Run Code Online (Sandbox Code Playgroud)
要将其放入文本文件中:
Get-DnsRecord -RecordType A -ZoneName FQDN -Server ServerName | % {Add-Content -Value $_ -Path filename.txt}
Run Code Online (Sandbox Code Playgroud)
我还没有看到提到的另一种方法:
Get-WmiObject -Namespace Root\MicrosoftDNS -Query "SELECT * FROM MicrosoftDNS_AType WHERE ContainerName='domain.com'"
Run Code Online (Sandbox Code Playgroud)
当您由于某种原因无法下载 DnsShell,或者您使用的是没有内置 Cmdlet 的旧版 Powershell,或者您的目标是旧版 Windows Server 时,请记住 WMI .
Windows Server 2012 Powershell v3 中可用的 DnsServer 模块具有以下可能对您有用的命令:
Get-DnsServerZone
Get-DnsServerResourceRecord
Run Code Online (Sandbox Code Playgroud)
第一个将为您提供所有区域第二个将为您提供您传递给它的任何区域的记录
它们基本上相当于 DNSCMD/EnumZones和/EnumRecords.
所以......你可以写这样的东西来获取所有区域的所有记录:
$Zones = @(Get-DnsServerZone)
ForEach ($Zone in $Zones) {
Write-Host "`n$Zone.ZoneName" -ForegroundColor "Yellow"
$Zone | Get-DnsServerResourceRecord
}
Run Code Online (Sandbox Code Playgroud)
另外,我相当确定 server 2012 现在为每个区域保留一个实际的区域文件?因此,您应该拥有所有区域的文件副本。
如果您使用的是 2008 R2,那么您可以使用我用来将所有区域备份到文件的脚本:
$zones = @( `
dnscmd /enumzones | `
select-string -pattern "\b(?i)((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b" | %{$_.Matches} | %{$_.Value};
);
ForEach ($domain in $zones) {
$backup = "dnscmd . /zoneExport $domain $domain";
Invoke-Expression $backup | Out-Null
Write-Host "Backing up $domain" -ForegroundColor "White"
};
ForEach ($item in (gci C:\Windows\System32\dns)) {
Write-Host "Renaming $item" -ForegroundColor "White"
Rename-item $item.fullname ([string]$item + ".dns")
}
Write-Host "Back up complete." -ForegroundColor "Cyan"
cmd /c pause | out-null
Run Code Online (Sandbox Code Playgroud)