PowerShell Script to Create and Link Schedules in Azure Automation

 

 

Hi All,

Lately I've been playing with Azure automation and needed to create schedules to kick off Runbooks at certain times of the day. Rather than doing this manually I decided to just write a simple PowerShell script to create these for me automatically, since I may need to do this task again in the future.  To do this I need to ensure I have Azure PowerShell configured and connected . I wont go into how in this blog please use the following blog as a reference How to install and configure Azure PowerShell

I then used the following script I wrote to create and register each Schedule

 

################################################################################################################################

$Days = ("Monday","Tuesday","Wednesday","Thursday","Friday")
$StartDate = ("26/01/2015 08:30","27/01/2015 08:30","28/01/2015 08:30","29/01/2015 08:30","30/01/2015 08:30")
$EndDate = ("26/01/2015 20:30","27/01/2015 20:30","28/01/2015 20:30","29/01/2015 20:30","30/01/2015 20:30")
$Accountname = "yourAutomationAccountname"
$Count = 0

ForEach ($Day in $Days)
{

New-AzureAutomationSchedule -AutomationAccountName $Accountname -Name "Start AzureVMs $($Day)" -StartTime "$($StartDate[$Count])" -DayInterval 7
Register-AzureAutomationScheduledRunbook -AutomationAccountName $Accountname -Name Start-AllAzureVM -ScheduleName "Start AzureVMs $($Day)"
New-AzureAutomationSchedule -AutomationAccountName $Accountname -Name "Stop AzureVMs $($Day)" -StartTime "$($EndDate[$Count])" -DayInterval 7
Register-AzureAutomationScheduledRunbook -AutomationAccountName $Accountname -Name Stop-AllAzureVM -ScheduleName "Stop AzureVMs $($Day)"

$Count ++

}

##################################################################################################################################

 

We can see that before I run my script I have no schedules attached to my Start-AllAzureVM and Stop-AzureVM Runbooks

image

image

we then run the script from which you should see a similar output to the one below.

image

We now can see that both of our Runbooks have Schedules created and assigned.

image

image

As always if you improve or have similar scripts to share feel free to comment below.