public/New-ResourceGroupDeleteLock.ps1

<#
 .Synopsis
 Ensures that a no-delete lock is directly on a resource group in Azure and creates one if none is existing.
 .Description
 Removes all firewall rules currently added to the SQL server given.
 .Parameter SubscriptionId
 The unique ID of the Azure subscription where the SQL Azure Server is located.
 .Parameter ResourceGroupName
 The name of the resource group
 .Parameter ResourceGroupLocation
 The Azure location of the resource group.
 .Parameter TenantId
 The unique ID of the tenant where the subscription lives in for faster context switch.
 .Example
  Set-AzdResourceGroupDeleteLock -SubscriptionId [Id] -ResourceGroupName rg-test -ResourceGroupLocation westeurope
  Ensures that a no-delete lock is directly on the resource group.
#>

Function New-ResourceGroupDeleteLock {
    [CmdLetBinding()]
    param (
        [Parameter(Mandatory = $true)] [string] $SubscriptionId,
        [Parameter(Mandatory = $true)] [string] $ResourceGroupName,        
        [Parameter(Mandatory = $true)] [string] $ResourceGroupLocation,
        [Parameter(Mandatory = $false)] [string] $TenantId,
        [switch] $NoLogo
    )
    begin {
        if (!$NoLogo.IsPresent) {
            Write-Logo $MyInvocation.InvocationName        
        }
        New-FunctionStartup
    }
    process {
        if ($null -ne (Get-AzResourceGroup -Name $ResourceGroupName -Location $ResourceGroupLocation -Verbose -ErrorAction SilentlyContinue)) {                        
            if ($null -eq (Get-AzResourceLock -AtScope -ResourceGroupName $ResourceGroupName | Where-Object { $_.Properties.level -eq "CanNotDelete" })) {
                Write-HostDebug "Set No-Delete-Lock for $ResourceGroupName..."
                New-AzResourceLock -LockName nodelete -LockLevel CanNotDelete -ResourceGroupName $ResourceGroupName -Force -ErrorAction Stop | Out-Null
                Write-HostSuccess "Delete Lock is set."
            }
            else {
                Write-HostInfo "Skipping becazse another no-delete lock was found."
            }
        }
        else {
            Write-HostError "Resource group $ResourceGroupName not found."     
        }
    }
}