PowerShell を使用した Azure CDN の管理
PowerShell により、Azure CDN プロファイルとエンドポイントを柔軟に管理することができます。 この PowerShell は対話的に使用することも、スクリプトを記述して、管理タスクを自動化することもできます。 このチュートリアルでは、Azure CDN のプロファイルとエンドポイントを管理するために、PowerShell で実行できる一般的なタスクをいくつか紹介します。
前提条件
注意
Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。
PowerShell を使用して Azure CDN のプロファイルとエンドポイントを管理するには、Azure PowerShell モジュールをインストールする必要があります。 Connect-AzAccount
コマンドレットを使用して Azure PowerShell をインストールし、Azure に接続する方法については、「 Azure PowerShell のインストールおよび構成方法」を参照してください。
重要
Azure PowerShell コマンドレットを実行する前に、Connect-AzAccount
でログインする必要があります。
Azure CDN コマンドレットの一覧表示
Azure CDN コマンドレットの一覧を表示するには、 Get-Command
コマンドレットを使用します。
PS C:\> Get-Command -Module Az.Cdn
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Confirm-AzCdnEndpointProbeURL 1.4.0 Az.Cdn
Cmdlet Disable-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet Disable-AzCdnCustomDomainHttps 1.4.0 Az.Cdn
Cmdlet Enable-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet Enable-AzCdnCustomDomainHttps 1.4.0 Az.Cdn
Cmdlet Get-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet Get-AzCdnEdgeNode 1.4.0 Az.Cdn
Cmdlet Get-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet Get-AzCdnEndpointNameAvailability 1.4.0 Az.Cdn
Cmdlet Get-AzCdnEndpointResourceUsage 1.4.0 Az.Cdn
Cmdlet Get-AzCdnOrigin 1.4.0 Az.Cdn
Cmdlet Get-AzCdnProfile 1.4.0 Az.Cdn
Cmdlet Get-AzCdnProfileResourceUsage 1.4.0 Az.Cdn
Cmdlet Get-AzCdnProfileSsoUrl 1.4.0 Az.Cdn
Cmdlet Get-AzCdnProfileSupportedOptimizationType 1.4.0 Az.Cdn
Cmdlet Get-AzCdnSubscriptionResourceUsage 1.4.0 Az.Cdn
Cmdlet New-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet New-AzCdnDeliveryPolicy 1.4.0 Az.Cdn
Cmdlet New-AzCdnDeliveryRule 1.4.0 Az.Cdn
Cmdlet New-AzCdnDeliveryRuleAction 1.4.0 Az.Cdn
Cmdlet New-AzCdnDeliveryRuleCondition 1.4.0 Az.Cdn
Cmdlet New-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet New-AzCdnProfile 1.4.0 Az.Cdn
Cmdlet Publish-AzCdnEndpointContent 1.4.0 Az.Cdn
Cmdlet Remove-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet Remove-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet Remove-AzCdnProfile 1.4.0 Az.Cdn
Cmdlet Set-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet Set-AzCdnOrigin 1.4.0 Az.Cdn
Cmdlet Set-AzCdnProfile 1.4.0 Az.Cdn
Cmdlet Start-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet Stop-AzCdnEndpoint 1.4.0 Az.Cdn
Cmdlet Test-AzCdnCustomDomain 1.4.0 Az.Cdn
Cmdlet Unpublish-AzCdnEndpointContent 1.4.0 Az.Cdn
ヘルプの表示
ヘルプを表示するには、このコマンドレットのいずれかで 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
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".
既存の Azure CDN プロファイルを一覧表示
パラメーターを指定せずに Get-AzCdnProfile
コマンドレットを実行すると、既存の CDN プロファイルすべてを取得できます。
Get-AzCdnProfile
この出力は、列挙のコマンドレットにパイプ処理できます。
# Output the name of all profiles on this subscription.
Get-AzCdnProfile | ForEach-Object { Write-Host $_.Name }
# Return only **Azure CDN from Verizon** profiles.
Get-AzCdnProfile | Where-Object { $_.Sku.Name -eq "Standard_Verizon" }
また、プロファイル名とリソース グループを指定して、1 つのプロファイルを返すこともできます。
Get-AzCdnProfile -ProfileName CdnDemo -ResourceGroupName CdnDemoRG
ヒント
複数のリソース グループがある場合は、それぞれのグループに同じ名前の CDN プロファイルが存在する可能性があります。 ResourceGroupName
パラメーターを省略すると、名前が一致するすべてのプロファイルが返されます。
既存の CDN エンドポイントの一覧表示
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
# Return all of the endpoints on all of the profiles.
Get-AzCdnProfile | Get-AzCdnEndpoint
# Return all of the endpoints in this subscription that are currently running.
Get-AzCdnProfile | Get-AzCdnEndpoint | Where-Object { $_.ResourceState -eq "Running" }
CDN プロファイルとエンドポイントの作成
New-AzCdnProfile
および New-AzCdnEndpoint
は、CDN プロファイルとエンドポイントを作成するときに使用します。 次の SKU がサポートされています。
- Standard_Verizon
- Premium_Verizon
- Custom_Verizon
- Standard_Akamai
- Standard_Microsoft
- Standard_ChinaCdn
# Create a new profile
New-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Sku Standard_Akamai -Location "Central US"
# Create a new endpoint
New-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Location "Central US" -EndpointName cdnposhdoc -OriginName "Contoso" -OriginHostName "www.contoso.com"
# Create a new profile and endpoint (same as above) in one line
New-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -Sku Standard_Akamai -Location "Central US" | New-AzCdnEndpoint -EndpointName cdnposhdoc -OriginName "Contoso" -OriginHostName "www.contoso.com"
エンドポイント名の可用性の確認
Get-AzCdnEndpointNameAvailability
は、エンドポイント名を使用できるかどうかを示すオブジェクトを返します。
# Retrieve availability
$availability = Get-AzCdnEndpointNameAvailability -EndpointName "cdnposhdoc"
# If available, write a message to the console.
If($availability.NameAvailable) { Write-Host "Yes, that endpoint name is available." }
Else { Write-Host "No, that endpoint name is not available." }
カスタム ドメインの追加
New-AzCdnCustomDomain
は、カスタム ドメイン名を既存のエンドポイントに追加します。
重要
「Content Delivery Network (CDN) エンドポイントにカスタム ドメインをマップする方法」の説明に従って、DNS プロバイダーで CNAME を設定する必要があります。 エンドポイントを変更する前に、 Test-AzCdnCustomDomain
を使用してマッピングをテストできます。
# Get an existing endpoint
$endpoint = Get-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc
# Check the mapping
$result = Test-AzCdnCustomDomain -CdnEndpoint $endpoint -CustomDomainHostName "cdn.contoso.com"
# Create the custom domain on the endpoint
If($result.CustomDomainValidated){ New-AzCdnCustomDomain -CustomDomainName Contoso -HostName "cdn.contoso.com" -CdnEndpoint $endpoint }
エンドポイントの変更
Set-AzCdnEndpoint
で既存のエンドポイントを変更します。
# Get an existing endpoint
$endpoint = Get-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc
# Set up content compression
$endpoint.IsCompressionEnabled = $true
$endpoint.ContentTypesToCompress = "text/javascript","text/css","application/json"
# Save the changed endpoint and apply the changes
Set-AzCdnEndpoint -CdnEndpoint $endpoint
CDN 資産の消去/事前読み込み
Unpublish-AzCdnEndpointContent
は、キャッシュされた資産を消去します。また、Publish-AzCdnEndpointContent
は、サポートされているエンドポイントで、資産を事前に読み込みます。
# Purge some assets.
Unpublish-AzCdnEndpointContent -ProfileName CdnDemo -ResourceGroupName CdnDemoRG -EndpointName cdndocdemo -PurgeContent "/images/kitten.png","/video/rickroll.mp4"
# Pre-load some assets.
Publish-AzCdnEndpointContent -ProfileName CdnDemo -ResourceGroupName CdnDemoRG -EndpointName cdndocdemo -LoadContent "/images/kitten.png","/video/rickroll.mp4"
# Purge everything in /images/ on all endpoints.
Get-AzCdnProfile | Get-AzCdnEndpoint | Unpublish-AzCdnEndpointContent -PurgeContent "/images/*"
CDN エンドポイントの開始/停止
Start-AzCdnEndpoint
および Stop-AzCdnEndpoint
を使用すると、個別のエンドポイントまたはエンドポイント グループを開始および停止できます。
# Stop the cdndocdemo endpoint
Stop-AzCdnEndpoint -ProfileName CdnDemo -ResourceGroupName CdnDemoRG -EndpointName cdndocdemo
# Stop all endpoints
Get-AzCdnProfile | Get-AzCdnEndpoint | Stop-AzCdnEndpoint
# Start all endpoints
Get-AzCdnProfile | Get-AzCdnEndpoint | Start-AzCdnEndpoint
標準ルール エンジン ポリシーの作成と、既存の CDN エンドポイントへの適用
New-AzCdnDeliveryRule
、New=AzCdnDeliveryRuleCondition
、New-AzCdnDeliveryRuleAction
を使用して、Microsoft プロファイルから Azure CDN 上の Azure CDN 標準ルール エンジンを構成できます。
# Create a new http to https redirect rule
$Condition=New-AzCdnDeliveryRuleCondition -MatchVariable RequestProtocol -Operator Equal -MatchValue HTTP
$Action=New-AzCdnDeliveryRuleAction -RedirectType Found -DestinationProtocol HTTPS
$HttpToHttpsRedirectRule=New-AzCdnDeliveryRule -Name "HttpToHttpsRedirectRule" -Order 2 -Condition $Condition -Action $Action
# Create a path based Response header modification rule.
$Cond1=New-AzCdnDeliveryRuleCondition -MatchVariable UrlPath -Operator BeginsWith -MatchValue "/images/"
$Action1=New-AzCdnDeliveryRuleAction -HeaderActionType ModifyResponseHeader -Action Overwrite -HeaderName "Access-Control-Allow-Origin" -Value "*"
$PathBasedCacheOverrideRule=New-AzCdnDeliveryRule -Name "PathBasedCacheOverride" -Order 1 -Condition $Cond1 -Action $action1
# Create a delivery policy with above deliveryRules.
$Policy = New-AzCdnDeliveryPolicy -Description "DeliveryPolicy" -Rule $HttpToHttpsRedirectRule,$UrlRewriteRule
# Update existing endpoint with created delivery policy
$ep = Get-AzCdnEndpoint -EndpointName cdndocdemo -ProfileName CdnDemo -ResourceGroupName CdnDemoRG
$ep.DeliveryPolicy = $Policy
Set-AzCdnEndpoint -CdnEndpoint $ep
CDN リソースの削除
Remove-AzCdnProfile
および Remove-AzCdnEndpoint
を使用すると、プロファイルとエンドポイントを削除できます。
# Remove a single endpoint
Remove-AzCdnEndpoint -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG -EndpointName cdnposhdoc
# Remove all the endpoints on a profile and skip confirmation (-Force)
Get-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG | Get-AzCdnEndpoint | Remove-AzCdnEndpoint -Force
# Remove a single profile
Remove-AzCdnProfile -ProfileName CdnPoshDemo -ResourceGroupName CdnDemoRG
次の手順
.NET または Node.js を使用して Azure CDN を自動化する方法について学習します。
CDN 機能の詳細については、CDN の概要に関するページをご覧ください。