PowerShell script used to create resource group level locks in all your Azure Subscriptions
A script used to get the list of all ResourceGroups in all your Azure Subscriptions.
It will check for existence of a ResourceGroup level lock presence and if not present
will create a lock.
#Login to the Azure Environment first
Connect-AzAccount
##get all the subscription which you have access to in the Azure environment.
$azSubs = Get-AzSubscription
#loop through each of the subscription
foreach ( $azSub in $azSubs ) {
Set-AzContext -Subscription $azSub | Out-Null
$azSubName = $azSub.Name
#get all the resourcegorups within the subscription
$RGS = Get-AzResourceGroup
#loop through each resource group
foreach($RG in $RGS) {
#get the resourcegroup lock details.
$lck = Get-AzResourceLock -ResourceGroupName $RG.ResourceGroupName -AtScope
#if the lock does not exist, create one
if ($null -eq $lck)
{
Write-Host "$($RG.ResourceGroupName) has no lock"
New-AzResourceLock -resourceGroupName $RG.ResourceGroupName -LockName "$($RG.ResourceGroupName)-lck" -LockLevel CanNotDelete -Force
Write-Host "$($RG.ResourceGroupName) has been locked"
}
else
{
Write-host "$RG.ResourceGroupName already locked"
}
}
}