BDAzureTools.psm1
function Remove-MyResourceGroup { <# .Synopsis Deletes a chosen resource group and all sub resources .DESCRIPTION This script allows you to see the progress when removing reource groups, this is not possible when using the Remove-AZResourceGroup command alone. Make sure you have a connection to your subcription before running this command. For example: use the following command: Connect-AZAccount .EXAMPLE Remove-MyResourceGroup -ResourceGroupName RG-test-AU101 This will remove a resource group called RG-test-AU101 and will show the the progress as it removes each of the resources inside this group .EXAMPLE Get-AZResourceGroup | Remove-MyResourceGroup This will pipe the Resource Group objects to this command where they will be deleted .PARAMETER ResourceGroupName This determines which Resource Group will be remove .PARAMETER ResourceGroup You can use the Resource Group object with this parameter to assist in piplining the data from a Get-AZResourceGroup command as shown in one of the examples. .INPUTS String .OUTPUTS none .NOTES General notes Created By: Brent Denny Created On: 17 Dec 2019 #> [cmdletbinding(SupportsShouldProcess=$true, DefaultParameterSetName='DefaultParams', ConfirmImpact='Medium')] Param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='DefaultParams')] [string[]]$ResourceGroupName, [Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName='PipeParams')] [Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResourceGroup[]]$ResourceGroup, [switch]$RemoveLocks ) Process { if ($PSCmdlet.ParameterSetName -eq 'PipeParams') {[string[]]$ResourceGroupName = $ResourceGroup.ResourceGroupName} foreach ($ResGrpName in $ResourceGroupName) { try {$Tennant = Get-AzTenant -ErrorAction Stop} catch { Connect-AzAccount try {$Tennant = Get-AzTenant -ErrorAction stop} catch {Write-Warning "The connection to your Azure subscription did not succeed";break} } try {$RGFound = if ($PSCmdlet.ShouldProcess("$ResGrpName",'Get ResourceGroup')) {Get-AzResourceGroup -Name $ResGrpName -ErrorAction stop}} Catch {if ($RGFound.count -eq 0) {write-warning "$ResGrpName does not exist";break}} $ResourcesInRG = Get-AzResource -ResourceGroupName $ResGrpName $counter = 0 $NumResources = $ResourcesInRG.count + 1 foreach ($Resource in $ResourcesInRG) { if ($RemoveLocks -eq $true) { if ($PSCmdlet.ShouldProcess("$($Resource.Name)",'Removing locks from')) { $LocksOnResource = Get-AZResourceLock | Where-Object {$_.ResourceName -eq $Resource.Name} foreach ($Lock in $LocksOnResource) { Remove-AZResourceLock -LockID $Lock.LockID -Force > $null } } } if ($PSCmdlet.ShouldProcess("$($Resource.Name)",'Delete Resource')) { Write-Progress -Activity "Deleting Resource: $($Resource.name)" -PercentComplete ($counter /$NumResources * 100) try {$Resource | Remove-AzResource -Force -ErrorAction Stop *>$null} catch{} } $counter++ } if ($PSCmdlet.ShouldProcess("$ResGrpName",'Delete ResourceGroup')) { Write-Progress -Activity "Deleting ResourceGroup: $($ResGrpName)" -PercentComplete ($counter /$NumResources * 100) try {Remove-AzResourceGroup -Name $ResGrpName -Force -ErrorAction Stop *>$null} catch{} } } } } |