使用 Azure PowerShell 管理 Azure DNS 中的 DNS 記錄和記錄集Manage DNS records and recordsets in Azure DNS using Azure PowerShell

本文說明如何使用 Azure PowerShell 來管理 DNS 區域的 DNS 記錄。This article shows you how to manage DNS records for your DNS zone by using Azure PowerShell. 也可以使用跨平台 Azure CLIAzure 入口網站來管理 DNS 記錄。DNS records can also be managed by using the cross-platform Azure CLI or the Azure portal.

此文章中的範例假設您已安裝 Azure PowerShell、登入,並建立 DNS 區域The examples in this article assume you have already installed Azure PowerShell, signed in, and created a DNS zone.

簡介Introduction

在 Azure DNS 中建立 DNS 記錄前,您需要先了解 Azure DNS 如何將 DNS 記錄組織成 DNS 記錄集。Before creating DNS records in Azure DNS, you first need to understand how Azure DNS organizes DNS records into DNS record sets.

記錄名稱Record names

在 Azure DNS 中,記錄是使用相對名稱來指定。In Azure DNS, records are specified by using relative names. 「完整」網域名稱 (FQDN) 包含區域名稱,而「相對」名稱不含區域名稱。A fully qualified domain name (FQDN) includes the zone name, whereas a relative name does not. 比方說,區域 'contoso.com' 中的相對記錄名稱 'www' 就給出完整記錄名稱 'www.contoso.com'。For example, the relative record name 'www' in the zone 'contoso.com' gives the fully qualified record name 'www.contoso.com'.

「頂點」記錄是 DNS 區域根目錄 (或「頂點」) 的 DNS 記錄。An apex record is a DNS record at the root (or apex) of a DNS zone. 例如,在 DNS 區域 'contoso.com' 中,頂點記錄也具有 'contoso.com' 的完整名稱 (這有時候稱為「裸」網域)。For example, in the DNS zone 'contoso.com', an apex record also has the fully qualified name 'contoso.com' (this is sometimes called a naked domain). 依照慣例,相對名稱 '@' 用來代表頂點記錄。</span><span class="sxs-lookup"><span data-stu-id="fbd7c-107">By convention, the relative name '@' is used to represent apex records.

記錄類型Record types

每一筆 DNS 記錄都有名稱和類型。Each DNS record has a name and a type. 記錄根據所包含的資料而組織成各種類型。Records are organized into various types according to the data they contain. 最常見的類型為 'A' 記錄,可將名稱對應到 IPv4 位址。The most common type is an 'A' record, which maps a name to an IPv4 address. 另一個一般類型為 'MX' 記錄,可將名稱對應到郵件伺服器。Another common type is an 'MX' record, which maps a name to a mail server.

Azure DNS 支援所有常見的 DNS 記錄類型:A、AAAA、CAA、CNAME、MX、NS、PTR、SOA、SRV 和 TXT。Azure DNS supports all common DNS record types: A, AAAA, CAA, CNAME, MX, NS, PTR, SOA, SRV, and TXT. 請注意,SPF 記錄是使用 TXT 記錄表示Note that SPF records are represented using TXT records.

記錄集Record sets

有時候,您需要以指定的名稱和類型建立多筆 DNS 記錄。Sometimes you need to create more than one DNS record with a given name and type. 例如,假設 'www.contoso.com' 網站裝載於兩個不同的 IP 位址。For example, suppose the 'www.contoso.com' web site is hosted on two different IP addresses. 網站需要兩筆不同的 A 記錄,每個 IP 位址各一筆。The website requires two different A records, one for each IP address. 以下是記錄集的範例:Here is an example of a record set:

www.contoso.com.        3600    IN    A    134.170.185.46
www.contoso.com.        3600    IN    A    134.170.188.221

Azure DNS 使用「記錄集」來管理所有 DNS 記錄。Azure DNS manages all DNS records using record sets. 記錄集 (也稱為「資源」記錄集) 是指一個區域中,具有相同名稱和相同類型的 DNS 記錄集合。A record set (also known as a resource record set) is the collection of DNS records in a zone that have the same name and are of the same type. 大部分的記錄集只包含單一記錄。Most record sets contain a single record. 不過,像以上的記錄集包含多筆記錄的例子也屢見不鮮。However, examples like the one above, in which a record set contains more than one record, are not uncommon.

例如,假設您已經在區域 'contoso.com' 中建立了 A 記錄 'www',指向 IP 位址 '134.170.185.46' (上述第一項記錄)。For example, suppose you have already created an A record 'www' in the zone 'contoso.com', pointing to the IP address '134.170.185.46' (the first record above). 若要建立第二筆記錄,您會將該記錄新增至現有的記錄集,而非建立額外的記錄集。To create the second record you would add that record to the existing record set, rather than create an additional record set.

SOA 和 CNAME 記錄類型是例外狀況。The SOA and CNAME record types are exceptions. 對於這些類型,DNS 標準不允許具有相同名稱的多筆記錄,因此這些記錄集只能包含單一記錄。The DNS standards don't permit multiple records with the same name for these types, therefore these record sets can only contain a single record.

如需在 Azure DNS 的 DNS 記錄的詳細資訊,請參閱 DNS 區域與記錄For more information about DNS records in Azure DNS, see DNS zones and records.

建立新的 DNS 區域Create a new DNS record

如果新記錄的名稱和類型與現有記錄相同,您需要將它新增至現有的記錄集If your new record has the same name and type as an existing record, you need to add it to the existing record set. 否則,如果新記錄的名稱和類型與現有記錄不同,則必須建立新的記錄集。If your new record has a different name and type to all existing records, you need to create a new record set.

在新的記錄集中建立 'A' 記錄Create 'A' records in a new record set

您可以使用 New-AzureRmDnsRecordSet Cmdlet 來建立記錄集。You create record sets by using the New-AzureRmDnsRecordSet cmdlet. 建立記錄集時,您必須指定記錄集名稱、區域、存留時間 (TTL) 和記錄類型,與要建立的記錄。When creating a record set, you need to specify the record set name, the zone, the time to live (TTL), the record type, and the records to be created.

將記錄加入至記錄集的參數,根據記錄集的類型而所有不同。The parameters for adding records to a record set vary depending on the type of the record set. 例如,使用 'A' 類型的記錄集時,您需要使用參數 -IPv4Address 來指定 IP 位址。For example, when using a record set of type 'A', you need to specify the IP address using the parameter -IPv4Address. 若是其他記錄類型,則使用其他變數。Other parameters are used for other record types. 請參閱其他記錄類型範例,以了解詳細資訊。See Additional record type examples for details.

下列範例會在 DNS 區域 'contoso.com' 中建立具有相對名稱 'www' 的記錄集。The following example creates a record set with the relative name 'www' in the DNS Zone 'contoso.com'. 記錄集的完整名稱是 'www.contoso.com'。The fully-qualified name of the record set is 'www.contoso.com'. 記錄類型為 'A',且 TTL 為 3600 秒。The record type is 'A', and the TTL is 3600 seconds. 此記錄集都會包含一筆記錄,其中 IP 位址為 '1.2.3.4'。The record set contains a single record, with IP address '1.2.3.4'.

New-AzureRmDnsRecordSet -Name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -IPv4Address "1.2.3.4") 

若要在區域頂點 (在此案例中為 'contoso.com') 建立記錄集,請使用記錄集名稱 '@' (不包括引號):</span><span class="sxs-lookup"><span data-stu-id="c3e97-129">To create a record set at the 'apex' of a zone (in this case, 'contoso.com'), use the record set name '@' (excluding quotation marks):

New-AzureRmDnsRecordSet -Name "@" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -IPv4Address "1.2.3.4") 

如果您需要建立包含多筆記錄的記錄集,請先建立本機陣列並新增記錄,然後將陣列傳遞給 New-AzureRmDnsRecordSet,如下所示:If you need to create a record set containing more than one record, first create a local array and add the records, then pass the array to New-AzureRmDnsRecordSet as follows:

$aRecords = @()
$aRecords += New-AzureRmDnsRecordConfig -IPv4Address "1.2.3.4"
$aRecords += New-AzureRmDnsRecordConfig -IPv4Address "2.3.4.5"
New-AzureRmDnsRecordSet -Name www –ZoneName "contoso.com" -ResourceGroupName MyResourceGroup -Ttl 3600 -RecordType A -DnsRecords $aRecords

記錄集中繼資料可用來將應用程式特定資料與每一個資料集產生關聯 (以索引鍵值組的形式)。Record set metadata can be used to associate application-specific data with each record set, as key-value pairs. 下列範例示範如何建立具有兩個中繼資料項目 ('dept=finance' 與 'environment=production') 的記錄集。The following example shows how to create a record set with two metadata entries, 'dept=finance' and 'environment=production'.

New-AzureRmDnsRecordSet -Name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -IPv4Address "1.2.3.4") -Metadata @{ dept="finance"; environment="production" } 

Azure DNS 也支援 'empty' 記錄集,此可作為建立 DNS 記錄前保留 DNS 名稱的預留位置。Azure DNS also supports 'empty' record sets, which can act as a placeholder to reserve a DNS name before creating DNS records. 空記錄集會顯示在 Azure DNS 控制面板中,但不會出現在 Azure DNS 名稱伺服器上。Empty record sets are visible in the Azure DNS control plane, but do appear on the Azure DNS name servers. 下列範例會建立空記錄集:The following example creates an empty record set:

New-AzureRmDnsRecordSet -Name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords @()

建立其他類型的記錄Create records of other types

參閱如何建立 'A' 記錄的詳細資訊後,下列範例會示範如何建立 Azure DNS 所支援其他記錄類型的記錄。Having seen in detail how to create 'A' records, the following examples show how to create records of other record types supported by Azure DNS.

在每個案例中,我們會說明如何建立包含單一記錄的記錄集。In each case, we show how to create a record set containing a single record. 可將 'A' 記錄的先前範例加以調整,即可建立含多個記錄的其他類型記錄集、具中繼資料的其他類型記錄集,或建立空的記錄集。The earlier examples for 'A' records can be adapted to create record sets of other types containing multiple records, with metadata, or to create empty record sets.

我們沒有提供 SOA 記錄集的建立範例,因為已與每一個 DNS 區域完成 SOA 建立與刪除,且無法個別建立或刪除 SOA。We do not give an example to create an SOA record set, since SOAs are created and deleted with each DNS zone and cannot be created or deleted separately. 然而,可以對 SOA 進行修改,如稍後範例所示However, the SOA can be modified, as shown in a later example.

建立含有單一記錄的 AAAA 記錄集Create an AAAA record set with a single record

New-AzureRmDnsRecordSet -Name "test-aaaa" -RecordType AAAA -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Ipv6Address "2607:f8b0:4009:1803::1005") 

建立含有單一記錄的 CAA 記錄集Create a CAA record set with a single record

New-AzureRmDnsRecordSet -Name "test-caa" -RecordType CAA -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Caaflags 0 -CaaTag "issue" -CaaValue "ca1.contoso.com") 

建立含有單一記錄的 CNAME 記錄集Create a CNAME record set with a single record

注意

DNS 標準在區域頂點不允許 CNAME 記錄 (-Name '@'),也不允許包含一個記錄以上的記錄集。

如需詳細資訊,請參閱 CNAME 記錄

New-AzureRmDnsRecordSet -Name "test-cname" -RecordType CNAME -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Cname "www.contoso.com") 

建立含有單一記錄的 MX 記錄集Create an MX record set with a single record

在此範例中,我們使用記錄集名稱 '@' 在區域頂點建立 MX 記錄 (在此案例中為 'contoso.com')。In this example, we use the record set name '@' to create an MX record at the zone apex (in this case, 'contoso.com').

New-AzureRmDnsRecordSet -Name "@" -RecordType MX -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Exchange "mail.contoso.com" -Preference 5) 

建立含有單一記錄的 NS 記錄集Create an NS record set with a single record

New-AzureRmDnsRecordSet -Name "test-ns" -RecordType NS -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Nsdname "ns1.contoso.com") 

建立含有單一記錄的 PTR 記錄集Create a PTR record set with a single record

在此情況下,'my-arpa-zone.com' 代表表示您 IP 範圍的 ARPA 反向對應區域。In this case, 'my-arpa-zone.com' represents the ARPA reverse lookup zone representing your IP range. 此區域中的每個 PTR 記錄集都與此 IP 範圍內的一個 IP 位址相對應。Each PTR record set in this zone corresponds to an IP address within this IP range. 記錄名稱 '10' 是此記錄所代表的這個 IP 範圍內 IP 位址的最後一個八位元。The record name '10' is the last octet of the IP address within this IP range represented by this record.

New-AzureRmDnsRecordSet -Name 10 -RecordType PTR -ZoneName "my-arpa-zone.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Ptrdname "myservice.contoso.com") 

建立含有單一記錄的 SRV 記錄集Create an SRV record set with a single record

建立 SRV 記錄集時,指定記錄集名稱中的 _服務_通訊協定When creating an SRV record set, specify the _service and _protocol in the record set name. 在區域頂點建立一筆 SRV 記錄集時,不需要在記錄集名稱中包含 '@'。</span><span class="sxs-lookup"><span data-stu-id="c3e97-156">There is no need to include '@' in the record set name when creating an SRV record set at the zone apex.

New-AzureRmDnsRecordSet -Name "_sip._tls" -RecordType SRV -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Priority 0 -Weight 5 -Port 8080 -Target "sip.contoso.com") 

建立含有單一記錄的 TXT 記錄集Create a TXT record set with a single record

下列範例示範如何建立 TXT 記錄。The following example shows how to create a TXT record. 如需 TXT 記錄中,所支援字串長度上限的相關資訊,請參閱 TXT 記錄For more information about the maximum string length supported in TXT records, see TXT records.

New-AzureRmDnsRecordSet -Name "test-txt" -RecordType TXT -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -Ttl 3600 -DnsRecords (New-AzureRmDnsRecordConfig -Value "This is a TXT record") 

取得記錄集Get a record set

若要擷取現有的記錄集,使用 Get-AzureRmDnsRecordSetTo retrieve an existing record set, use Get-AzureRmDnsRecordSet. 此 cmdlet 會傳回 Azure DNS 中代表記錄集的本機物件。This cmdlet returns a local object that represents the record set in Azure DNS.

如同 New-AzureRmDnsRecordSet,提供的記錄集名稱必須是相對名稱,表示它不能包含區域名稱。As with New-AzureRmDnsRecordSet, the record set name given must be a relative name, meaning it must exclude the zone name. 您也必須指定記錄類型,以及包含記錄集的區域。You also need to specify the record type, and the zone containing the record set.

下列範例示範如何擷取記錄集。The following example shows how to retrieve a record set. 在此範例中,使用 -ZoneName-ResourceGroupName 參數來指定區域。In this example, the zone is specified using the -ZoneName and -ResourceGroupName parameters.

$rs = Get-AzureRmDnsRecordSet -Name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"

或者,您也可以使用 -Zone 參數傳遞的區域物件來指定區域。Alternatively, you can also specify the zone using a zone object, passed using the -Zone parameter.

$zone = Get-AzureRmDnsZone -Name "contoso.com" -ResourceGroupName "MyResourceGroup"
$rs = Get-AzureRmDnsRecordSet -Name "www" -RecordType A -Zone $zone

列出記錄集List record sets

您也可以使用 Get-AzureRmDnsZone,透過省略 -Name 及/或 -RecordType 參數,在區域中列出記錄集。You can also use Get-AzureRmDnsZone to list record sets in a zone, by omitting the -Name and/or -RecordType parameters.

下列範例會傳回區域中的所有記錄集︰The following example returns all record sets in the zone:

$recordsets = Get-AzureRmDnsRecordSet -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"

下列範例示範將記錄集名稱省略時,如何透過指定記錄類型來擷取指定類型的所有記錄集:The following example shows how all record sets of a given type can be retrieved by specifying the record type while omitting the record set name:

$recordsets = Get-AzureRmDnsRecordSet -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"

若要擷取所有記錄類型中,具有指定名稱的所有記錄集,您必須擷取所有記錄集,然後篩選結果︰To retrieve all record sets with a given name, across record types, you need to retrieve all record sets and then filter the results:

$recordsets = Get-AzureRmDnsRecordSet -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" | where {$_.Name.Equals("www")}

在以上所有範例中,您可以使用 -ZoneName-ResourceGroupName 參數來指定區域 (如上所示),或透過指定區域物件來指定:In all the above examples, the zone can be specified either by using the -ZoneName and -ResourceGroupNameparameters (as shown), or by specifying a zone object:

$zone = Get-AzureRmDnsZone -Name "contoso.com" -ResourceGroupName "MyResourceGroup"
$recordsets = Get-AzureRmDnsRecordSet -Zone $zone

將記錄新增至現有的記錄集Add a record to an existing record set

若要將記錄新增至現有的記錄集,請遵循下列三個步驟︰To add a record to an existing record set, follow the following three steps:

  1. 取得現有記錄集Get the existing record set

    $rs = Get-AzureRmDnsRecordSet -Name www –ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -RecordType A
    
  2. 將新記錄新增至本機記錄集。Add the new record to the local record set. 這是一種離線作業。This is an off-line operation.

    Add-AzureRmDnsRecordConfig -RecordSet $rs -Ipv4Address "5.6.7.8"
    
  3. 將對變更的認可傳回給 Azure DNS 服務。Commit the change back to the Azure DNS service.

    Set-AzureRmDnsRecordSet -RecordSet $rs
    

使用 Set-AzureRmDnsRecordSet,用指定的記錄集取代 Azure DNS 中現有的記錄集 (與其中所包含的所有記錄)。Using Set-AzureRmDnsRecordSet replaces the existing record set in Azure DNS (and all records it contains) with the record set specified. Etag 檢查 是用來確保並行變更不會遭到覆寫。Etag checks are used to ensure concurrent changes are not overwritten. 您可以使用選擇性的 -Overwrite 參數來停用這些檢查。You can use the optional -Overwrite switch to suppress these checks.

作業的此序列也可以「經由管道輸送」,亦即使用管道傳遞記錄集物件,而不是以參數進行傳遞。This sequence of operations can also be piped, meaning you pass the record set object by using the pipe rather than passing it as a parameter:

Get-AzureRmDnsRecordSet -Name "www" –ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -RecordType A | Add-AzureRmDnsRecordConfig -Ipv4Address "5.6.7.8" | Set-AzureRmDnsRecordSet

上述範例顯示如何將 'A' 記錄新增至類型 'A' 的現有記錄集。The examples above show how to add an 'A' record to an existing record set of type 'A'. 作業的類似序列也可用來將記錄新增至其他類型的記錄集,可透過每一個記錄類型專屬的其他變數取代 Add-AzureRmDnsRecordConfig-Ipv4Address 變數。A similar sequence of operations is used to add records to record sets of other types, substituting the -Ipv4Address parameter of Add-AzureRmDnsRecordConfig with other parameters specific to each record type. 每個記錄類型的參數對 New-AzureRmDnsRecordConfigcmdlet 而言是相同的,如以上其他記錄類型範例所示。The parameters for each record type are the same as for the New-AzureRmDnsRecordConfig cmdlet, as shown in Additional record type examples above.

類型 'CNAME' 或 'SOA' 的記錄集不能包含多個記錄。Record sets of type 'CNAME' or 'SOA' cannot contain more than one record. 這個條件是起因於 DNS 標準。This constraint arises from the DNS standards. 而非 Azure DNS 的限制。It is not a limitation of Azure DNS.

從現有的記錄集移除記錄Remove a record from an existing record set

從記錄集移除記錄的程序,與將記錄新增至現有記錄集的程序類似︰The process to remove a record from a record set is similar to the process to add a record to an existing record set:

  1. 取得現有記錄集Get the existing record set

    $rs = Get-AzureRmDnsRecordSet -Name www –ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -RecordType A
    
  2. 從本機記錄集物件中移除記錄。Remove the record from the local record set object. 這是一種離線作業。This is an off-line operation. 要移除的記錄必須完全符合現有的資料錄,包括所有參數。The record that's being removed must be an exact match with an existing record across all parameters.

    Remove-AzureRmDnsRecordConfig -RecordSet $rs -Ipv4Address "5.6.7.8"
    
  3. 將對變更的認可傳回給 Azure DNS 服務。Commit the change back to the Azure DNS service. 使用選擇性 -Overwrite 參數來停止對並行變更的 Etag 檢查Use the optional -Overwrite switch to suppress Etag checks for concurrent changes.

    Set-AzureRmDnsRecordSet -RecordSet $Rs
    

使用上述序列,將最後一筆記錄從記錄集中移除並不會刪除記錄集,而是會留下空的記錄集。Using the above sequence to remove the last record from a record set does not delete the record set, rather it leaves an empty record set. 若要完全移除記錄集,請參閱刪除記錄集To remove a record set entirely, see Delete a record set.

與將記錄新增至記錄集的方式類似,移除記錄集的作業序列也可以管道輸送︰Similarly to adding records to a record set, the sequence of operations to remove a record set can also be piped:

Get-AzureRmDnsRecordSet -Name www –ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" -RecordType A | Remove-AzureRmDnsRecordConfig -Ipv4Address "5.6.7.8" | Set-AzureRmDnsRecordSet

藉由將特定類型的適當參數傳遞至 Remove-AzureRmDnsRecordSet,以支援不同的記錄類型。Different record types are supported by passing the appropriate type-specific parameters to Remove-AzureRmDnsRecordSet. 每個記錄類型的參數對 New-AzureRmDnsRecordConfigcmdlet 而言是相同的,如以上其他記錄類型範例所示。The parameters for each record type are the same as for the New-AzureRmDnsRecordConfig cmdlet, as shown in Additional record type examples above.

修改現有記錄集Modify an existing record set

修改現有記錄集的步驟,與從記錄集新增或移除記錄時所採取的步驟相類似:The steps for modifying an existing record set are similar to the steps you take when adding or removing records from a record set:

  1. 使用 Get-AzureRmDnsRecordSet擷取現有的記錄集。Retrieve the existing record set by using Get-AzureRmDnsRecordSet.
  2. 透過下列方式修改本機記錄集︰Modify the local record set object by:
    • 新增或移除記錄Adding or removing records
    • 變更現有記錄的參數Changing the parameters of existing records
    • 變更記錄集中繼資料和存留時間 (TTL)Changing the record set metadata and time to live (TTL)
  3. 使用 Set-AzureRmDnsRecordSet Cmdlet 來認可您所做的變更。Commit your changes by using the Set-AzureRmDnsRecordSet cmdlet. 這會以指定的記錄集取代 Azure DNS 中現有的記錄集。This replaces the existing record set in Azure DNS with the record set specified.

使用 Set-AzureRmDnsRecordSet 時,會使用 Etag 檢查 來確保不會覆寫並行變更。When using Set-AzureRmDnsRecordSet, Etag checks are used to ensure concurrent changes are not overwritten. 您可以使用選擇性的 -Overwrite 參數來停用這些檢查。You can use the optional -Overwrite switch to suppress these checks.

更新現有記錄集中的記錄To update a record in an existing record set

在此範例中,我們會變更現有 'A' 記錄的 IP 位址:In this example, we change the IP address of an existing 'A' record:

$rs = Get-AzureRmDnsRecordSet -name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"
$rs.Records[0].Ipv4Address = "9.8.7.6"
Set-AzureRmDnsRecordSet -RecordSet $rs

修改 SOA 記錄To modify an SOA record

您無法在區域頂點 (-Name "@" (包含引號)) 從自動建立的 SOA 記錄集中新增或移除記錄。You cannot add or remove records from the automatically created SOA record set at the zone apex (-Name "@", including quote marks). 不過,您可以修改 SOA 記錄 (「主機」除外) 和記錄集 TTL 內的任何參數。However, you can modify any of the parameters within the SOA record (except "Host") and the record set TTL.

下列範例示範如何變更 SOA 記錄的 Email 屬性:The following example shows how to change the Email property of the SOA record:

$rs = Get-AzureRmDnsRecordSet -Name "@" -RecordType SOA -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"
$rs.Records[0].Email = "admin.contoso.com"
Set-AzureRmDnsRecordSet -RecordSet $rs

在區域頂點修改 NS 記錄To modify NS records at the zone apex

系統會自動使用每個 DNS 區域在區域頂點建立 NS 記錄集。The NS record set at the zone apex is automatically created with each DNS zone. 此記錄集包含指派給區域的 Azure DNS 名稱伺服器的名稱。It contains the names of the Azure DNS name servers assigned to the zone.

您可以將其他名稱伺服器新增至此 NS 記錄集,以支援使用多個 DNS 提供者的共同裝載網域。You can add additional name servers to this NS record set, to support co-hosting domains with more than one DNS provider. 您也可以修改此記錄集的 TTL 和中繼資料。You can also modify the TTL and metadata for this record set. 不過,您無法移除或修改預先填入的 Azure DNS 名稱伺服器。However, you cannot remove or modify the pre-populated Azure DNS name servers.

請注意,這只適用於區域頂點的 NS 記錄集。Note that this applies only to the NS record set at the zone apex. 區域中的其他 NS 記錄集 (如用於委派子區域) 可以修改,沒有任何限制。Other NS record sets in your zone (as used to delegate child zones) can be modified without constraint.

下列範例顯示如何將其他的名稱伺服器新增至在區域頂點的 NS 記錄集:The following example shows how to add an additional name server to the NS record set at the zone apex:

$rs = Get-AzureRmDnsRecordSet -Name "@" -RecordType NS -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"
Add-AzureRmDnsRecordConfig -RecordSet $rs -Nsdname ns1.myotherdnsprovider.com
Set-AzureRmDnsRecordSet -RecordSet $rs

若要修改記錄集中繼資料To modify record set metadata

記錄集中繼資料可用來將應用程式特定資料與每一個資料集產生關聯 (以索引鍵值組的形式)。Record set metadata can be used to associate application-specific data with each record set, as key-value pairs.

下列範例示範如何修改現有記錄集的中繼資料︰The following example shows how to modify the metadata of an existing record set:

# Get the record set
$rs = Get-AzureRmDnsRecordSet -Name www -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"

# Add 'dept=finance' name-value pair
$rs.Metadata.Add('dept', 'finance') 

# Remove metadata item named 'environment'
$rs.Metadata.Remove('environment')  

# Commit changes
Set-AzureRmDnsRecordSet -RecordSet $rs

刪除記錄集Delete a record set

您可以使用 Remove-AzureRmDnsRecordSet Cmdlet 來刪除記錄集。Record sets can be deleted by using the Remove-AzureRmDnsRecordSet cmdlet. 刪除記錄集時,也會刪除記錄集內的所有記錄。Deleting a record set also deletes all records within the record set.

注意

您無法在區域頂點 (-Name '@') 刪除 SOA 和 NS 記錄集。 Azure DNS 在區域建立時就自動建立了這些項目,並且會在區域刪除時自動刪除這些項目。

下列範例說明如何刪除記錄集。The following example shows how to delete a record set. 在此範例中,記錄集名稱、記錄集類型、區域名稱和資源群組是個別明確指定。In this example, the record set name, record set type, zone name, and resource group are each specified explicitly.

Remove-AzureRmDnsRecordSet -Name "www" -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"

或者,可以透過名稱、類型,和使用物件指定的區域,來指定記錄集︰Alternatively, the record set can be specified by name and type, and the zone specified using an object:

$zone = Get-AzureRmDnsZone -Name "contoso.com" -ResourceGroupName "MyResourceGroup"
Remove-AzureRmDnsRecordSet -Name "www" -RecordType A -Zone $zone

至於第三個選項,記錄集本身即可透過記錄集物件進行指定︰As a third option, the record set itself can be specified using a record set object:

$rs = Get-AzureRmDnsRecordSet -Name www -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup"
Remove-AzureRmDnsRecordSet -RecordSet $rs

將記錄集的刪除方式指定為使用記錄集物件時,則會使用 Etag 檢查,來確保不會刪除並行變更。When you specify the record set to be deleted by using a record set object, Etag checks are used to ensure concurrent changes are not deleted. 您可以使用選擇性的 -Overwrite 參數來停用這些檢查。You can use the optional -Overwrite switch to suppress these checks.

記錄集物件也可以經由管道輸送,而不是當做參數傳遞:The record set object can also be piped instead of being passed as a parameter:

Get-AzureRmDnsRecordSet -Name www -RecordType A -ZoneName "contoso.com" -ResourceGroupName "MyResourceGroup" | Remove-AzureRmDnsRecordSet

確認提示Confirmation prompts

New-AzureRmDnsRecordSetSet-AzureRmDnsRecordSetRemove-AzureRmDnsRecordSet cmdlets 皆支援確認提示。The New-AzureRmDnsRecordSet, Set-AzureRmDnsRecordSet, and Remove-AzureRmDnsRecordSet cmdlets all support confirmation prompts.

如果 $ConfirmPreferencePowerShell 喜好設定變數的值為 Medium 或更低,則每個 cmdlet 會提示確認。Each cmdlet prompts for confirmation if the $ConfirmPreference PowerShell preference variable has a value of Medium or lower. 因為 $ConfirmPreference 的預設值為 High,使用預設 PowerShell 設定時不會出現這些提示。Since the default value for $ConfirmPreference is High, these prompts are not given when using the default PowerShell settings.

您可以使用 -Confirm 參數覆寫目前 $ConfirmPreference 設定。You can override the current $ConfirmPreference setting using the -Confirm parameter. 如果您指定 -Confirm-Confirm:$True,此 cmdlet 在執行前會提示您進行確認。If you specify -Confirm or -Confirm:$True , the cmdlet prompts you for confirmation before it runs. 如果您指定 -Confirm:$False,此 cmdlet 不會提示您進行確認。If you specify -Confirm:$False , the cmdlet does not prompt you for confirmation.

如需 -Confirm$ConfirmPreference 的詳細資訊,請參閱有關喜好設定變數For more information about -Confirm and $ConfirmPreference, see About Preference Variables.

後續步驟Next steps

深入了解 Azure DNS 中的區域和記錄Learn more about zones and records in Azure DNS.
了解使用 Azure DNS 時,如何保護區域和記錄Learn how to protect your zones and records when using Azure DNS.
檢閱 Azure DNS PowerShell 參考文件Review the Azure DNS PowerShell reference documentation.