Skip to content

Instantly share code, notes, and snippets.

@kensykora
Created May 20, 2014 16:08
Show Gist options
  • Save kensykora/c842fd00460d2f9d5ca3 to your computer and use it in GitHub Desktop.
Save kensykora/c842fd00460d2f9d5ca3 to your computer and use it in GitHub Desktop.
Purge Akamai Cache
# Purge Akamai Cache
#
# Invokes the Akamai REST Api to purge the cache for the given objects
#
# == Usage ==
# .\purge-akamai-cache.ps1 -CpCode cpcode -User Username -Pass Password
#
# == Params ==
# CpCodes: comma separated list of cpcodes of the site to purge
# User / Pass: Valid credentials for an Akamai User that has permissions to purge cache
#
# == Example Usage ==
# .\purge-akamai-cache.ps1 -CpCode "12345,23456" -User "ken.sykora@nerdery.com" -Pass #######
#
param ([Parameter(Mandatory=$True)][string[]]$CpCodes,
[Parameter(Mandatory=$True)][string]$user,
[Parameter(Mandatory=$True)][string]$pass)
$ErrorActionPreference = "Stop"
function Purge-AkamaiCache([string[]]$AkamaiCpCodes,
[string]$username,
[string]$password) {
Write-Host ("Purging Cache for CpCodes {0} with user $username" -f ($AkamaiCpCodes -join ", "))
$body = @{ type="cpcode"; objects=$AkamaiCpCodes }
$cred = New-Object System.Management.Automation.PSCredential($username, (ConvertTo-SecureString $password -AsPlainText -Force))
$response = Invoke-RestMethod -Uri "https://api.ccu.akamai.com/ccu/v2/queues/default" -Method Post -Body (ConvertTo-Json $body) -ContentType "application/json" -Credential $cred
if($response.httpStatus -eq 201){
$timeRemaining = New-Object System.TimeSpan(0,0,$response.estimatedSeconds)
$pingAfter = New-Object System.TimeSpan(0,0,$response.pingAfterSeconds)
Write-Host ("Estimated Time: {0:c}" -f $timeRemaining)
Write-Host ("Purge ID: {0}" -f $response.purgeId)
Write-Host ("Support ID: {0}" -f $response.supportId)
Write-Host ("{0} (updating in {1:c})..." -f $response.detail, $pingAfter)
$purged = $false
while(-not $purged) {
Start-Sleep -Seconds $pingAfter.TotalSeconds
$response = Invoke-RestMethod -Uri ("https://api.ccu.akamai.com{0}" -f $response.progressUri) -Credential $cred
$pingAfter = New-Object System.TimeSpan(0,0,$response.pingAfterSeconds)
$purged = $response.purgeStatus -eq "Done"
$nextUpdateString = ""
if(-not $purged) { $nextUpdateString = " (updating in {0:c})..." -f $pingAfter }
Write-Host ("{0}{1}" -f $response.purgeStatus, $nextUpdateString)
}
Write-Host "Content Purged"
} else {
$response
throw "Unexpected Reponse Received"
}
}
Purge-AkamaiCache -AkamaiCpCode ($CpCodes -split ",") -username $user -password $pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment