Remove all runbooks with specific names from specific Automation Acct from Azure
I was recently being tasked to create a script to remove two specific Runbooks from Automation Accounts which names starts with “x-xxx” and containts “-x-“.
The script will has a function called “Remove-Runbook” with a parameter “-subscriptionID”.
Currently it works per subscription, you need to pass the subscription ID in parameter.
#requires -version 3 <# .SYNOPSIS Remove Azure automation runbooks .DESCRIPTION This script will remove runbooks from automation acct that the name starts with "x-xxx" and containts "-xx-" from a specific Subscription It will store the logs in c:\runbook automatically .PARAMETER -SubscriptionID The -SubscriptionID is madatory to pass when running the Function .EXAMPLE Remove-runbook -subscriptionID 'subscription id' .NOTES Version: 1.0 Author: hashmat Creation Date: 3/18/2020 Purpose/Change: Dev/Prod #> #---------------------------------------------------------[Initialisations]-------------------------------------------------------- #Set Error Action $ErrorActionPreference = "stop" #----------------------------------------------------------[Declarations]---------------------------------------------------------- #-----------------------------------------------------------[Functions]------------------------------------------------------------ Function Remove-Runbook{ [CmdletBinding()] Param( [Parameter(Mandatory)] $SubscriptionID ) Begin{ Write-host '###############################################################' Write-host '# GETTING SUBSCRIPTION #'-foregroundcolor Green Write-host '###############################################################' $subscriptionId = $SubscriptionID Set-AzContext -Subscription $SubscriptionId Write-host '###############################################################' Write-host '# GETTING AUTOMATION ACCOUNT #'-foregroundcolor Green Write-host '###############################################################' Get-AzAutomationAccount | Where-Object {$_.AutomationAccountName.StartsWith('g-aaa-') -and $_.AutomationAccountName.Contains('-ia-')} | select AutomationAccountName, SubscriptionId | ft -AutoSize $acct = Get-AzAutomationAccount | Where-Object {$_.AutomationAccountName.StartsWith('g-aaa-') -or $_.AutomationAccountName.Contains('-ia-')} | select AutomationAccountName, ResourceGroupName, SubscriptionId write-host "THere is Total $($acct.Count) Automation Accounts " -ForegroundColor Green $acct $runbookslocation = "c:\Runbook" if(!(test-path $runbookslocation)) { New-Item -ItemType Directory -Force -Path C:\runbook } $acct| Out-File c:\Runbook\ListingAutomationAcct.txt Write-host '###############################################################' Write-host '# Listing Runbooks #'-foregroundcolor Green Write-host '###############################################################' foreach ($account in $acct){ $vmStartStopParent = Get-AzAutomationRunbook -ResourceGroupName $account.ResourceGroupName -AutomationAccountName $account.AutomationAccountName | Where-object {$_.name -eq 'run-vmStartStopParent'} if($vmStartStopParent.name -eq 'run-vmStartStopParent') { Write-host "Runbooks $($vmStartStopParent.name) EXIST in $($vmStartStopParent.AutomationAccountName)" Write-output "Runbook $($vmStartStopParent.name) EXIST $($vmStartStopParent.AutomationAccountName)" >> C:\Runbook\List-vmStartStopParent.txt }elseif($vmStartStopParent.name -ne 'run-vmStartStopParent'){ write-host "Runbook run-vmStartStopParent DOESNT EXIST in $($account.AutomationAccountName)" } } foreach ($account in $acct){ $vmStartStopChild = Get-AzAutomationRunbook -ResourceGroupName $account.ResourceGroupName -AutomationAccountName $account.AutomationAccountName | Where-object {$_.name -eq 'run-vmStartStopChild'} if($vmStartStopChild.name -eq 'run-vmStartStopChild') { Write-host "Runbooks $($vmStartStopChild.name) EXIST in $($vmStartStopChild.AutomationAccountName)" Write-output "Runbook $($vmStartStopChild.name) EXIST in $($vmStartStopChild.AutomationAccountName)" >> C:\Runbook\List-vmStartStopChild.txt }elseif($vmStartStopParent.name -ne 'run-vmStartStopChild'){ write-host "Runbook run-vmStartStopChild DOESNT EXIST in $($account.AutomationAccountName)" } } } Process{ Try{ Write-host '###############################################################' Write-host '# REMOVING run-Runbooks #'-foregroundcolor Red Write-host '###############################################################' ############# REMOVING RUN-VMSTARTSTOPPARENT ########################## foreach ($account in $acct){ $vmStartStopParent = Get-AzAutomationRunbook -ResourceGroupName $account.ResourceGroupName -AutomationAccountName $account.AutomationAccountName | Where-object {$_.name -eq 'run-vmStartStopParent'} | select name, AutomationAccountName if($vmStartStopParent.name -eq 'run-vmStartStopParent') { Write-output "REMOVING $($vmStartStopParent.name) FROM $($vmStartStopParent.AutomationAccountName)" >> C:\Runbook\Remove-vmStartStopParent.txt Write-host "REMOVING $($vmStartStopParent.name) FROM $($vmStartStopParent.AutomationAccountName)" -ForegroundColor red Remove-AzAutomationRunbook -AutomationAccountName $account.AutomationAccountName -Name $vmStartStopParent.name -ResourceGroupName $account.ResourceGroupName -force }elseif($vmStartStopParent.name -ne 'run-vmStartStopParent' ){ Write-host "RUNBOOK run-vmStartStopParent DOESN'T EXIST in $($account.AutomationAccountName) to REMOVE" -ForegroundColor gray } } ############# REMOVING RUN-VMSTARTSTOPCHILD ########################## foreach ($account in $acct){ $vmStartStopChild = Get-AzAutomationRunbook -ResourceGroupName $account.ResourceGroupName -AutomationAccountName $account.AutomationAccountName | Where-object {$_.name -eq 'run-vmStartStopChild'} | select name, AutomationAccountName if($vmStartStopChild.name -eq 'run-vmStartStopChild') { Write-host "REMOVING $($vmStartStopChild.name) FROM $($vmStartStopChild.AutomationAccountName)" -ForegroundColor red Write-output "Removing $($vmStartStopChild.name) from $($vmStartStopChild.AutomationAccountName)" >> C:\Runbook\Remove-vmStartStopChild.txt Remove-AzAutomationRunbook -AutomationAccountName $account.AutomationAccountName -Name $vmStartStopChild.name -ResourceGroupName $account.ResourceGroupName -Force }elseif($vmStartStopChild.name -ne 'run-vmStartStopChild' ){ Write-host "RUNBOOK run-vmStartStopChild DOESN'T EXIST in $($account.AutomationAccountName) to REMOVE" -ForegroundColor gray } } } Catch{ Write-Error -Message $_.Exception throw $_.Exception #Write-Host -BackgroundColor Red "Error: $($_.Exception)" Break } } }