Skip to content

Instantly share code, notes, and snippets.

@damienpontifex
Last active June 19, 2022 01:33
Show Gist options
  • Save damienpontifex/74d23e48c99f20b70ac5c8b527706b47 to your computer and use it in GitHub Desktop.
Save damienpontifex/74d23e48c99f20b70ac5c8b527706b47 to your computer and use it in GitHub Desktop.
<#
.DESCRIPTION
An example runbook which gets all the ARM resources using the Managed Identity
.NOTES
AUTHOR: Azure Automation Team
LASTEDIT: Oct 26, 2021
#>
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$ResourceGroups = Get-AzResourceGroup
foreach ($ResourceGroup in $ResourceGroups)
{
$Tags = $ResourceGroup.Tags
if (($Tags -ne $null) -and ($Tags.ContainsKey("AutoDelete")) -and ($Tags["AutoDelete"] -eq "false"))
{
Write-Output "Skipping deletion of resource group $($ResourceGroup.ResourceGroupName) because AutoDelete tag is: '$AutoDelete'"
}
else
{
Write-Output "Will delete resource group $($ResourceGroup.ResourceGroupName)"
# Remove-AzResourceGroup -Name $ResourceGroup.ResourceGroupName -Force -AsJob
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment