Created
August 10, 2023 18:45
-
-
Save mdowst/3d9835e8f1b3731b947f95785a25898f to your computer and use it in GitHub Desktop.
Test-AzureAutomationRegions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
This script was created to test the output and runtimes for Azure Automation runbooks running in different regions. | |
It is scope to only us regions. Update line 23 to use different areas. | |
#> | |
$SubscriptionId = '' | |
$ResourceGroupName = '' | |
$Runbook = 'Test-Sandbox' | |
if ($(Get-AzContext).Subscription.SubscriptionId -ne $SubscriptionId) { | |
Set-AzContext -SubscriptionId $SubscriptionId -ErrorAction SilentlyContinue | |
if ($(Get-AzContext).Subscription.SubscriptionId -ne $SubscriptionId) { | |
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue | |
Clear-AzDefault -Force -ErrorAction SilentlyContinue | |
Add-AzAccount -SubscriptionId $SubscriptionId | Out-Null | |
} | |
} | |
$RbPath = Join-Path $env:Temp "$($Runbook).ps1" | |
"`$env:COMPUTERNAME`nGet-AutomationVariable -Name 'TestVar'" | Out-File -FilePath $RbPath -Encoding utf8 | |
$timeStamp = (Get-Date).ToFileTime() | |
$locations = Get-AzLocation | Where-Object{ $_.Providers -contains 'Microsoft.Automation' } | |
$RGAccounts = Get-AzAutomationAccount -ResourceGroupName $ResourceGroupName | |
foreach($lc in $locations | Where-Object{ $_.Location -notin $RGAccounts.Location -and $_.GeographyGroup -eq 'US' }){ | |
$newAA = "$($lc.Location)-$($timeStamp)" | |
Write-Host "Creating: $newAA" | |
New-AzAutomationAccount -ResourceGroupName $ResourceGroupName -AutomationAccountName $newAA -Location $lc.Location | Out-Null | |
} | |
$AutomationAccounts = Get-AzAutomationAccount -ResourceGroupName $ResourceGroupName | Select-Object @{l='Account';e={$_.AutomationAccountName}}, Location, @{l='Tests';e={0}} | |
foreach($aa in $AutomationAccounts){ | |
if(-not (Get-AzAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $aa.Account -Name $Runbook -ErrorAction SilentlyContinue)){ | |
Write-Host "$($aa.Account) - runbook" | |
Import-AzAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $aa.Account -Path $RbPath -Name $Runbook -Type PowerShell -Published -Force | Out-Null | |
} | |
if(-not (Get-AzAutomationVariable -ResourceGroupName $ResourceGroupName -AutomationAccountName $aa.Account -Name 'TestVar' -ErrorAction SilentlyContinue)){ | |
Write-Host "$($aa.Account) - variable" | |
New-AzAutomationVariable -ResourceGroupName $ResourceGroupName -AutomationAccountName $aa.Account -Encrypted $True -Value "Testing" -Name "TestVar" | Out-Null | |
} | |
} | |
$TestsToRun = 10 | |
[System.Collections.Generic.List[PSObject]] $Tests = @() | |
while ($AutomationAccounts | Where-Object { $_.Tests -lt $TestsToRun }) { | |
foreach ($aa in $AutomationAccounts) { | |
$aa.Tests = ($Tests | Where-Object { $_.Account -eq $aa.Account -and $_.Status -in "Completed", "Failed", "Suspended", "Stopped" } | Measure-Object).Count | |
$all = ($Tests | Where-Object { $_.Account -eq $aa.Account } | Measure-Object).Count | |
if ($all -lt $TestsToRun) { | |
$RBjob = Start-AzAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $aa.Account -Name $Runbook | |
$Tests.Add([pscustomobject]@{ | |
Account = $aa.Account | |
Location = $aa.Location | |
JobId = $RBjob.JobId | |
Status = '' | |
Client = $null | |
Progress = 0 | |
Runtime = 0 | |
}) | |
} | |
} | |
$Tests | Where-Object { $_.Status -notin "Completed", "Failed", "Suspended", "Stopped" } | ForEach-Object { | |
$JobStatus = Get-AzAutomationJob -ResourceGroupName $ResourceGroupName -AutomationAccountName $_.Account -Id $_.JobId | |
$_.Runtime = (New-TimeSpan -Start $JobStatus.CreationTime.DateTime -End $JobStatus.LastModifiedTime.DateTime).TotalSeconds | |
$_.Status = $JobStatus.Status | |
} | |
$Tests | Where-Object { $_.Status -in "Completed", "Failed", "Suspended", "Stopped" -and -not $_.Client } | ForEach-Object { | |
$JobOutputs = Get-AzAutomationJobOutput -ResourceGroupName $ResourceGroupName -AutomationAccountName $_.Account -Id $_.JobId | |
if ($JobOutputs[0].Summary) { | |
$_.Client = $JobOutputs[0].Summary | |
} | |
else { | |
$_.Client = 'Unknown' | |
} | |
$_.Progress = ($JobOutputs | Where-Object { $_.Type -eq 'Progress' } | Measure-Object).Count | |
} | |
$Tests | Group-Object Location, Status, Client, Progress | Select-Object @{l='Jobs';e={$_.Count}}, @{l = 'Location'; e = { $_.Values[0] } }, @{l = 'Status'; e = { $_.Values[1] } }, | |
@{l = 'Client'; e = { $_.Values[2] } }, @{l = 'ProgressOutput'; e = { $_.Values[3] } }, | |
@{l='AverageRuntime';e={[math]::Round(($_.Group.Runtime | Measure-Object -Average).Average,0)}} | | |
Sort-Object Location, Status | Format-Table | |
if ($Tests | Where-Object { $_.Status -notin "Completed", "Failed", "Suspended", "Stopped" }) { | |
Start-Sleep -Seconds 10 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment