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

PowerShell 提供管理 Azure 內容傳遞網路 配置檔和端點的最彈性方法之一。 您可以以互動方式使用PowerShell,或撰寫腳本來自動化管理工作。 本教學課程示範數個您可以使用 PowerShell 完成的最常見工作,以管理 Azure 內容傳遞網路 配置檔和端點。

必要條件

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

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

重要

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

列出 Azure 內容傳遞網路 Cmdlet

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

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

取得說明

您可以使用 Cmdlet 取得上述任何 Cmdlet Get-Help 的說明。 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
  • 進階版_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

後續步驟