如何通过PowerShell完全清除Azure CDN?

时间:2018-05-29 19:53:35

标签: powershell azure azure-powershell azure-cdn

cmdlet Unpublish-AzureRmCdnEndpointContent有一个-PurgeContent选项,但天真用法:

Unpublish-AzureRmCdnEndpointContent 
  -EndpointName $endpointName 
  -ProfileName $profileName 
  -ResourceGroupName $resourceGroupName 
  -PurgeContent "/*"

仅显示清除根目录中的文件(路径中名为no /),而不是嵌套文件。如何确保一次从所有嵌套级别清除所有内容?

2 个答案:

答案 0 :(得分:0)

  

仅显示清除根目录下的文件(也就是路径中的no /,而不是嵌套文件)。如何确保一次从所有嵌套级别清除所有内容?

根据Purge an Azure CDN endpoint,我们可以使用"/*"清除路径中/*的端点下的所有文件夹,子文件夹和文件。

  

通配符清除:星号(*)可用作通配符。使用路径中的/ *清除端点下的所有文件夹,子文件夹和文件。

当我们尝试从端点清除所有网络时,我们也可以从azure门户捕获网络。

enter image description here

我们也可以使用fiddler在运行命令期间捕获API休息。

与我们从azure portal检查[purge all]进行操作相同。

enter image description here

我用以下代码测试它。

$endpointName = "endpoint name"
$resourceGroupName = "resource group"
$profileName = "profileName"
[string[]]$purge = @("/*")  
Unpublish-AzureRmCdnEndpointContent -EndpointName $endpointName -ProfileName $profileName -ResourceGroupName $resourceGroupName -PurgeContent $purge -Debug

注意:完成此任务需要很长时间。

enter image description here

答案 1 :(得分:0)

所以我尝试了另一个答案,命令返回没有错误,但实际上并未清除文件。我最终写了一个脚本,该脚本枚举了磁盘上的源文件并清除了它们。您一次只能指定50个,因此我不得不改写这个答案:

Powershell break a long array into a array of array with length of N in one line?

这与我需要的内容有些特定,因为之前的步骤是首先使用azcopy将文件放在存储中...

$currentDirectory = get-location | select -ExpandProperty Path
$files = ls ".\" -Recurse | select -ExpandProperty FullName | %{ 
    $_.Replace($currentDirectory ,"\") } | %{ $_.Replace("\", "/") }

$counter = [pscustomobject] @{ Value = 0 }
$groups = $files | Group-Object -Property { [math]::Floor($counter.Value++ / 50) }

Import-Module azurerm

$profileName = "yourprofile"
$endpoint = "yourendpoint"
$profile = Get-AzureRmCdnProfile -ProfileName $profileName

$groups | %{
    Unpublish-AzureRmCdnEndpointContent `
        -EndpointName $endpoint `
        -ProfileName $profileName `
        -ResourceGroupName $profile.resourceGroupName `
        -PurgeContent $_.Group
}

取决于您拥有多少文件,您可能想使用Start-Job对其进行线程化。我不需要,因为每次通话大约需要5-10秒。

希望这对某人有帮助。