簡體   English   中英

更改 Windows 服務器 DNS 和 PowerShell 中子域記錄的 TTL

[英]Change TTL for subdomain records in Windows Server DNS with PowerShell

我正在嘗試使用 PowerShell 在 Windows 服務器 DNS 中批量編輯 DNS TTL。我真正無法理解的是我從Get-DnsServerResourceRecord獲得的記錄對象中沒有任何 FQDN。

我的 DNS 設置有一個主區域和許多子域。

primaryzone.tld
├── sub1
│  ├── record1
│  ├── record2
│  └── subsub1
│     ├── (same as parent)
│     ├── record1.1
│     └── record2.1
├── sub2
└── sub3

由於無法使用Get-DnsServerResourceRecord進行遞歸,我必須遍歷無法以編程方式收集的子域,以獲取所有記錄,而不僅僅是區域根級別的記錄:

$DNSServer = "dc.company.tld"
$Zone = "primaryzone.tld"
$ChildZone = "sub1"
$SubDomains = @("","subsub1")
ForEach ($SubDomain in $SubDomains){
    if ( $SubDomain -ne "" ) {
        $FullDomain = "$($SubDomain).$($ChildZone)"
    }
    else {
        $FullDomain = $ChildZone
    } 
    Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $Zone -Name "$($FullDomain)" 

這真的應該是那樣嗎?

如果我現在想更改subsub1下所有記錄的 TTL,我會這樣嘗試:

 Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $Zone -Name "$($FullDomain)"  |
    ForEach-Object{
            $newRecord = $_.Clone()
            $newRecord.TimeToLive = $ttl
            Set-DnsServerResourceRecord -ComputerName $DNSServer -NewInputObject $newRecord -OldInputObject $_ -ZoneName $Zone
    } 

它適用於subsub1 (same as parent)記錄,它具有primaryzone.tldHostName屬性,因此在subsub1.sub1中提供完整路徑。 record1.1HostName名是record1.1 ,它在樹中剝離了有關其 position 的任何信息。 這會導致此錯誤:

Set-DnsServerResourceRecord : Resource record in OldInputObject not found in primaryzone.tld zone on dc.company.tld server.
At C:\Users\me\Desktop\dns_ttl.ps1:24 char:13
+             Set-DnsServerResourceRecord -ComputerName $DNSServer -NewInputObject ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (dc.company.tld:root/Microsoft/...rResourceRecord) [Set-DnsServerResourceRecord], CimException
    + FullyQualifiedErrorId : WIN32 9714,Set-DnsServerResourceRecord

我將此解釋為Set-DnsServerResourceRecord未找到OldInputObject記錄,因為它不知道在哪里查找,除了區域的根級別。

為什么我可以為Get-DnsServerResourceRecord指定-Name subdomain ,但在使用Set-DnsServerResourceRecord將其寫回時它不采用-Name參數? 為什么不記錄對象在區域中不包含有關其絕對 (FQDN) position 的任何信息?

這么簡單的事情怎么會這么難呢?

編輯:

我根據@Cpt.Whale 的建議更改了我的腳本

$DNSServer = "dc.company.tld"
$TargetZone = "primary.tld"
$TTL = [System.TimeSpan]::FromMinutes(1)
$SubDomain = "sub.primary.tld" 

$Zones = Get-DnsServerZone -ComputerName $DNSServer | Where {
    $_.ZoneType -eq 'Primary' -and 
    $_.IsReverseLookupZone -eq $false -and
    $_.ZoneName -ne 'TrustAnchors' -and
    $_.ZoneName -eq $TargetZone
}


Foreach ($DnsZone in $Zones) {
  # Get all records in zone by not specifying -Name:
  $Records = $DnsZone | Get-DnsServerResourceRecord -ComputerName $DNSServer
  $Records | Foreach { 
    if ($_.HostName -match "$SubDomain$") {
        if ($_.TimeToLive -ne $TTL) {
            # This weird copy is due to awkward CIM references
            $oldRecord = $_
            $newRecord = $_.Clone()
            $newRecord.TimeToLive = $TTL

            # Update the TTL on the existing record:
            Set-DnsServerResourceRecord -ComputerName $DNSServer -Old $oldRecord -New $newRecord -ZoneName $DnsZone.ZoneName
            # Report changes
            "{0,-50} TTL changed from {1,12} to {2,12}" -f $_.HostName, $_.TimeToLive, $TTL
        }
     }
  }
}

有了這個我得到了完整的HostName ,但仍然是我上面描述的錯誤

DNS 記錄類型 cim object 沒有ZoneName屬性,但它確實保留了子域 - 它是記錄的HostName名的一部分。 DNS 幾乎不關心子域,除非它們是一個單獨的區域。

首先要仔細檢查您是否實際上只有一個區域:

$zones = Get-DnsServerZone -ComputerName $DNSServer | Where {
    $_.ZoneType -eq 'Primary' -and 
    $_.IsReverseLookupZone -eq $false -and
    $_.ZoneName -ne 'TrustAnchors'
}
$zones

然后,例如:

$TTL = [System.TimeSpan]::FromHours(2)  ## example TTL

Foreach ($DnsZone in $Zones) {
  # Get all records in zone by not specifying -Name:
  $records = $DnsZone | Get-DnsServerResourceRecord -ComputerName $DNSServer
  $records | Foreach { 
    # This weird copy is due to awkward CIM references
    $old = $_
    $new = $_.Clone()
      $new.TimeToLive = $TTL
    # Update the TTL on the existing record:
    Set-DnsServerResourceRecord -ComputerName $DNSServer -Old $old -New $new -ZoneName $DnsZone.ZoneName
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM