run a ps script on a schedule via Azure
Problem to address: run a ps script to retrieve a list from Cloudflare via their API and store in CSV format somewhere reliable and accessible.
Solution: use an Azure Automation Account with a Powershell Runbook and configure a Schedule, combined with Azure Storage for csv store.
Extra: post a Slack message once complete/failed.
[string]$ZoneId = Get-AutomationVariable -Name 'CloudflareZoneId'
[string]$CloudflareApiToken = Get-AutomationVariable -Name 'CloudflareApiToken'
[string]$StorageAccountKey = Get-AutomationVariable -Name 'StorageAccountKey'
[string]$ListId = Get-AutomationVariable -Name 'CloudflareRedirectListId'
[string]$WebhookUrl = Get-AutomationVariable -Name 'SlackWebhookUrl'
[string]$StorageAccountName = "accnamehere"
[string]$ContainerName = "containernamehere"
[string]$DateTime = Get-Date -Format "yyyy-MM-dd-HH-mm-ss"
[string]$OutputFileName = "bulk-redirects_$DateTime.csv"
# Set API URL
$baseUrl = "https://api.cloudflare.com/client/v4/accounts/$ZoneId/rules/lists/$ListId/items"
$redirectList = @()
$cursor = $null
try {
do {
$url = $baseUrl
if ($cursor) {
$url += "?cursor=$cursor"
}
$headers = @{
Authorization = "Bearer $CloudflareApiToken"
}
$response = Invoke-RestMethod -Method Get -Uri $url -Headers $headers
if ($response.success -ne $true) {
Write-Error "Failed to retrieve data from Cloudflare: $($response.errors | ConvertTo-Json -Depth 5)"
exit 1
}
$redirectList += $response.result
$cursor = $response.result_info.cursors.after
} while ($cursor)
# Export to CSV
$tempFilePath = [System.IO.Path]::GetTempFileName()
$tempCsvPath = "$tempFilePath.csv"
$redirectList | Select-Object `
@{Name='source_url'; Expression={ $_.redirect.source_url }},
@{Name='target_url'; Expression={ $_.redirect.target_url }},
@{Name='status_code'; Expression={ $_.redirect.status_code }},
@{Name='preserve_query_string'; Expression={ $_.redirect.preserve_query_string }},
@{Name='include_subdomains'; Expression={ $_.redirect.include_subdomains }},
@{Name='subpath_matching'; Expression={ $_.redirect.subpath_matching }},
@{Name='preserve_path_suffix'; Expression={ $_.redirect.preserve_path_suffix }} |
Export-Csv -Path $tempCsvPath -NoTypeInformation
# Upload to Azure Blob
$context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Set-AzStorageBlobContent -File $tempCsvPath -Container $ContainerName -Blob $OutputFileName -Context $context -Force
Invoke-RestMethod -Method Post -Uri $WebhookUrl -Body '{"message" : "Production Cloudflare Redirect List has been exported to CSV successfully :white_check_mark:"}' -ContentType 'application/json'
Write-Host "Export complete. CSV uploaded to blob '$OutputFileName' in container '$ContainerName'."
}
catch {
Invoke-RestMethod -Method Post -Uri $WebhookUrl -Body '{"message" : "Error exporting Production Cloudflare Redirect List to CSV, please raise with dwaineharding"}' -ContentType 'application/json'
}
note
By adding the Azure Automation Extension to VS Code you are able to really easily publish PS script changes to the Azure account.