使用 PowerShell 管理 Azure 內容傳遞網路

重要

Microsoft (傳統) 的 Azure 內容傳遞網路標準將於 2027 年 9 月 30 日淘汰。 為了避免任何服務中斷,請務必在 2027 年 9 月 30 日之前將您的 Microsoft (傳統) 的 Azure 內容傳遞網路標準設定檔移轉到 Azure Front Door 標準或進階層。 如需詳細資訊,請參閱 Microsoft (傳統) 的 Azure 內容傳遞網路標準淘汰

PowerShell 提供了最具彈性的方法來管理您的 Azure 內容傳遞網路設定檔和端點。 您可以用互動方式使用 PowerShell 或透過撰寫指令碼來自動進行管理工作。 本教學課程會示範數個您可透過 PowerShell 完成的最常見工作,以管理 Azure 內容傳遞網路設定檔和端點。

必要條件

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱安裝 Azure PowerShell (部分機器翻譯)。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

若要使用 PowerShell 來管理 Azure 內容傳遞網路設定檔和端點,您必須安裝 Azure PowerShell 模組。 若要了解如何安裝 Azure PowerShell 及使用 Connect-AzAccount Cmdlet 連接至 Azure,請參閱 如何安裝和設定 Azure PowerShell

重要

您必須先使用 Connect-AzAccount 登入,才能執行 Azure PowerShell Cmdlet。

列出 Azure 內容傳遞網路 Cmdlet

您可以使用 Get-Command Cmdlet 列出所有的 Azure 內容傳遞網路 Cmdlet。

PS C:\> Get-Command -Module Az.Cdn

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Confirm-AzCdnEndpointProbeURL                      2.1.0      Az.Cdn
Cmdlet          Disable-AzCdnCustomDomain                          2.1.0      Az.Cdn
Cmdlet          Disable-AzCdnCustomDomainHttps                     2.1.0      Az.Cdn
Cmdlet          Enable-AzCdnCustomDomain                           2.1.0      Az.Cdn
Cmdlet          Enable-AzCdnCustomDomainHttps                      2.1.0      Az.Cdn
Cmdlet          Get-AzCdnCustomDomain                              2.1.0      Az.Cdn
Cmdlet          Get-AzCdnEdgeNode                                  2.1.0      Az.Cdn
Cmdlet          Get-AzCdnEndpoint                                  2.1.0      Az.Cdn
Cmdlet          Get-AzCdnEndpointResourceUsage                     2.1.0      Az.Cdn
Cmdlet          Get-AzCdnOrigin                                    2.1.0      Az.Cdn
Cmdlet          Get-AzCdnProfile                                   2.1.0      Az.Cdn
Cmdlet          Get-AzCdnProfileResourceUsage                      2.1.0      Az.Cdn
Cmdlet          Get-AzCdnProfileSupportedOptimizationType          2.1.0      Az.Cdn
Cmdlet          Get-AzCdnSubscriptionResourceUsage                 2.1.0      Az.Cdn
Cmdlet          New-AzCdnCustomDomain                              2.1.0      Az.Cdn
Cmdlet          New-AzCdnDeliveryPolicy                            2.1.0      Az.Cdn
Cmdlet          New-AzCdnDeliveryRule                              2.1.0      Az.Cdn
Cmdlet          New-AzCdnDeliveryRuleAction                        2.1.0      Az.Cdn
Cmdlet          New-AzCdnDeliveryRuleCondition                     2.1.0      Az.Cdn
Cmdlet          New-AzCdnEndpoint                                  2.1.0      Az.Cdn
Cmdlet          New-AzCdnProfile                                   2.1.0      Az.Cdn
Cmdlet          Remove-AzCdnCustomDomain                           2.1.0      Az.Cdn
Cmdlet          Remove-AzCdnEndpoint                               2.1.0      Az.Cdn
Cmdlet          Remove-AzCdnProfile                                2.1.0      Az.Cdn
Cmdlet          Set-AzCdnProfile                                   2.1.0      Az.Cdn
Cmdlet          Start-AzCdnEndpoint                                2.1.0      Az.Cdn
Cmdlet          Stop-AzCdnEndpoint                                 2.1.0      Az.Cdn

取得說明

您可以使用 Get-Help Cmdlet 取得這些 Cmdlet 的說明。 Get-Help 提供使用方式與語法,並選擇性地顯示範例。

PS C:\> Get-Help Get-AzCdnProfile

NAME
    Get-AzCdnProfile

SYNOPSIS
    Gets an Azure CDN profile.

SYNTAX
    Get-AzCdnProfile [-ProfileName <String>] [-ResourceGroupName <String>] [-InformationAction
    <ActionPreference>] [-InformationVariable <String>] [<CommonParameters>]

DESCRIPTION
    Gets an Azure CDN profile and all related information.

RELATED LINKS
    https://docs.microsoft.com/powershell/module/az.cdn/get-azcdnprofile

REMARKS
    To see the examples, type: "get-help Get-AzCdnProfile -examples".
    For more information, type: "get-help Get-AzCdnProfile -detailed".
    For technical information, type: "get-help Get-AzCdnProfile -full".
    For online help, type: "get-help Get-AzCdnProfile -online"

列出現有的 Azure 內容傳遞網路設定檔

不含任何參數的 Get-AzCdnProfile Cmdlet 會擷取所有現有的 內容傳遞網路設定檔。

Get-AzCdnProfile

此輸出可以輸送到 Cmdlet 以便列舉。

# Output the name of all profiles on this subscription.
Get-AzCdnProfile | ForEach-Object { Write-Host $_.Name }

您也可以指定設定檔名稱和資源群組,以傳回單一設定檔。

Get-AzCdnProfile -ProfileName CdnDemo -ResourceGroupName CdnDemoRG

提示

可能會有多個具有相同名稱的內容傳遞網路設定檔,只要它們位於不同的資源群組即可。 省略 ResourceGroupName 參數會傳回所有具有相符名稱的設定檔。

列出現有的內容傳遞網路端點

Get-AzCdnEndpoint 可以擷取設定檔上的個別端點或所有端點。

# Get a single endpoint.
Get-AzCdnEndpoint -ProfileName CdnDemo -ResourceGroupName CdnDemoRG -EndpointName cdndocdemo

# Get all of the endpoints on a given profile. 
Get-AzCdnEndpoint -ProfileName CdnDemo -ResourceGroupName CdnDemoRG

建立內容傳遞網路設定檔與端點

New-AzCdnProfileNew-AzCdnEndpoint 可用來建立內容傳遞網路設定檔與端點。 支援以下 SKU:

  • Standard_Verizon
  • Premium_Verizon
  • Custom_Verizon
  • Standard_Microsoft
  • Standard_ChinaCdn
# Create a new profile
New-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Sku Standard_Microsoft -Location "Central US"

# Create a new endpoint
$origin = @{
    Name = "Contoso"
    HostName = "www.contoso.com"
};

New-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Location "Central US" -EndpointName cdnposhdoc -Origin $origin

新增自訂網域

New-AzCdnCustomDomain 會將自訂網域名稱新增至現有端點。

重要

您必須以 [如何將自訂網域對應至內容傳遞網路端點] 中所述方式,透過您的 DNS 提供者設定 CNAME。 您可以先測試對應,然後使用 Test-AzCdnCustomDomain修改端點。

# Create the custom domain on the endpoint
New-AzCdnCustomDomain -ResourceGroupName CdnDemoRG -ProfileName CdnPoshDemo -Name contoso -HostName "cdn.contoso.com" -EndpointName cdnposhdoc

修改端點

Update-AzCdnEndpoint 可修改現有的端點。

# Update endpoint with compression settings
Update-AzCdnEndpoint -Name cdnposhdoc -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -IsCompressionEnabled -ContentTypesToCompress "text/javascript","text/css","application/json"

正在清除

Clear-AzCdnEndpointContent 會清除快取的資產。

# Purge some assets.
Clear-AzCdnEndpointContent -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc -ContentFilePath @("/images/kitten.png","/video/rickroll.mp4")

預先載入某些資產

注意

預先載入僅適用於來自 Edgio 設定檔的 Azure 內容傳遞網路。

Import-AzCdnEndpointContent 將資產預先載入內容傳遞網路快取。

Import-AzCdnEndpointContent -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc -ContentFilePath @("/images/kitten.png","/video/rickroll.mp4")`

啟動/停止內容傳遞網路端點

Start-AzCdnEndpointStop-AzCdnEndpoint 可用來啟動和停止個別端點的端點群組。

# Stop the CdnPoshDemo endpoint
Stop-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Name cdnposhdoc

# Start the CdnPoshDemo endpoint
Start-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Name cdnposhdoc

建立標準規則引擎原則,並套用至現有的內容傳遞網路端點

下列 Cmdlet 清單可用來建立標準規則引擎原則,並將其套用至現有的內容傳遞網路端點。

條件:

動作:

# Create a path based Response header modification rule. 
$cond1 = New-AzCdnDeliveryRuleUrlPathConditionObject -Name UrlPath -ParameterOperator BeginsWith -ParameterMatchValue "/images/"
$action1 = New-AzCdnDeliveryRuleResponseHeaderActionObject -Name ModifyResponseHeader -ParameterHeaderAction Overwrite -ParameterHeaderName "Access-Control-Allow-Origin" -ParameterValue "*"
$rule1 = New-AzCdnDeliveryRuleObject -Name "PathBasedCacheOverride" -Order 1 -Condition $cond1 -Action $action1

# Create a new http to https redirect rule
$cond1 = New-AzCdnDeliveryRuleRequestSchemeConditionObject -Name RequestScheme -ParameterMatchValue HTTPS
$action1 = New-AzCdnUrlRedirectActionObject -Name UrlRedirect -ParameterRedirectType Found -ParameterDestinationProtocol Https
$rule2 = New-AzCdnDeliveryRuleObject -Name "UrlRewriteRule" -Order 2 -Condition $cond1 -Action $action1

# Update existing endpoint with new rules
Update-AzCdnEndpoint -Name cdnposhdoc -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -DeliveryPolicyRule $rule1,$rule2

刪除內容傳遞網路資源

Remove-AzCdnProfileRemove-AzCdnEndpoint 可用來移除設定檔和端點。

# Remove a single endpoint
Remove-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc

# Remove a single profile
Remove-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG

後續步驟