Created
February 8, 2019 09:10
-
-
Save Ba4bes/f321f6ce43790c3c03ff2b901d28c4cc to your computer and use it in GitHub Desktop.
Remove expired runbookschedules from an Azure automation account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
This script removes expired schedules from a runbookaccount. | |
.DESCRIPTION | |
The script finds schedules that were scheduled onetime and don't have a next runtime. | |
All schedules that meet the criteria will be removed. | |
.EXAMPLE | |
.\Remove-OldSchedules.ps1 -AutomationAccountName "OGD-EUW-AUT-OPL-AUTOMATION-01" | |
.NOTES | |
Script was created to be used as a scheduled runbook. | |
#> | |
param( | |
[Parameter(Mandatory = $true)] | |
[String]$AutomationAccountName | |
) | |
# Get the resourcegroup of the automation account | |
$AAResourceGroup = (Get-AzureRmResource -Name $AutomationAccountName).ResourceGroupName | |
# Get the schedules that have run once and have no NextRun-time set. | |
$Schedules = Get-AzureRmAutomationSchedule -ResourceGroupName $AAResourceGroup -AutomationAccountName $AutomationAccountName | | |
Where-Object {$_.Frequency -eq "Onetime" -and $null -eq $_.NextRun} | |
Write-Verbose "$($Schedules.Count) expired Schedules found" | |
foreach ($Schedule in $Schedules) { | |
try { | |
Remove-AzureRmAutomationSchedule -Name $Schedule.Name -ResourceGroupName $AAResourceGroup -AutomationAccountName $AutomationAccountName -force -ErrorAction Stop | |
Write-Output "$($Schedule.Name) has been removed" | |
} | |
catch { | |
Write-Error $_ | |
continue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment