{"id":5741,"date":"2015-06-05T00:01:00","date_gmt":"2015-06-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/06\/05\/using-azure-automation-part-5\/"},"modified":"2019-02-18T09:47:35","modified_gmt":"2019-02-18T16:47:35","slug":"using-azure-automation-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-5\/","title":{"rendered":"Using Azure Automation: Part 5"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary: <\/b><span style=\"font-size:12px\">Schedule an Azure Automation runbook to get Azure to work for you.<\/span><\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here to show you the most importance piece of Azure Automation: the actual scheduling of runbooks to allow you to let Azure do all the automation for you! This is the final post in a five-part series. To catch up, read:<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-1\/\" target=\"_blank\">Using Azure Automation: Part&nbsp;1<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-2\/\" target=\"_blank\">Using Azure Automation: Part 2<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-3\/\" target=\"_blank\">Using Azure Automation: Part 3<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-4\/\" target=\"_blank\">Using Azure Automation: Part 4<\/a><\/li>\n<\/ul>\n<p>Yesterday, we finished our script to power down the virtual machines in Microsoft Azure, including sending some output to indicate the status of the shutdown.<\/p>\n<p>If we go back into our runbook, things look a little different now. Instead of defaulting to the draft editing window, we are in the published runbook window as shown here:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8306.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8306.1.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Clicking <b>Start<\/b> will trigger the runbook just as if you were at a Windows PowerShell command prompt running a script.<\/p>\n<p>You will see a status bar that indicates that the job is running. You can check the status of the job by selecting the <b>View Job<\/b> option on the status bar in Azure. Clicking this will open a status window that shows you the ongoing results of the job:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3527.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3527.2.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Drilling down further (near the bottom), we can view the <b>Output<\/b>. This window will show us any output that would normally echo to a Windows PowerShell console, including the data we specified with the <b>Write-Output<\/b> cmdlet.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7851.3.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7851.3.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>If you look at the upper right of the screen, you&#039;ll see a <b>History<\/b> tab, which allows you to view a log of the working runbooks.<\/p>\n<p>But now for the fun stuff&#8230;setting this as scheduled task. As shown here, to the right of <b>Author<\/b>, you&#039;ll see a tab marked <b>Schedule<\/b>. Click that to begin creating a schedule.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5415.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5415.4.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>With a new Azure Automation instance, you&#039;ll have a blank schedule as you see in the following image. It has two simple options: <b>Link to a New Schedule<\/b> or <b>Link to an Existing Schedule<\/b>.<\/p>\n<p>Click <b>Link to a New Schedule<\/b> to create a new schedule for your Azure Automation runbook.&gt;&gt;5<\/p>\n<p>The first part will require a name and a description for the scheduled task. Again lets be sensible. Naming the scheduled task &quot;Marvin the Paranoid Android&quot; might be funny, but it won&#039;t make a lick of sense later (unless your description is really good&mdash;but even then, I wouldn&#039;t suggest that).<\/p>\n<p>In the following example, we call this scheduled task <b>EndOfDayShutdownTask<\/b>:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4812.6.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4812.6.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>If we click our friendly neighborhood checkmark to continue, we&#039;ll see that we have three basic scheduling options:<\/p>\n<ul>\n<li><b>One time<\/b>: Do this once and only once.<\/li>\n<li><b>Hourly<\/b>: Repeat on a scheduled hourly cycle (minimum every hour, but you can increase the gap).<\/li>\n<li><b>Daily<\/b>: Repeat this on a scheduled daily cycle (minimum every day, but you can increase the gap).<\/li>\n<\/ul>\n<p>With the daily and hourly choices, you can also provide a date that this should stop running. In our example, this task will occur every day (Saturdays and Sundays included) at 5 P.M.:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1715.7.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1715.7.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>We&#039;ll click that lovely little checkmark at the bottom of the wizard. Our schedule is created, and we can see our runbook set up as an available job:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6153.8.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6153.8.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>As shown here, you&#039;ll see three for options for your scheduled runbook. They are <b>Link<\/b>, <b>View Details<\/b>,<b> <\/b>and<b> Unlink<\/b>:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4747.9.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4747.9.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p><b>Link<\/b> is the same wizard you previously saw on the screen. <b>Unlink<\/b> allows you to remove that runbook as a scheduled task.<\/p>\n<p>Now here is where you can schedule a runbook the fun way. If you&#039;d like to use Windows PowerShell to do exactly what we just did, you can! In the Azure module, there is a cmdlet called <b>New-AzureAutomationSchedule<\/b>. We could use it do define our daily schedule like so:<\/p>\n<p style=\"margin-left:30px\">$StartTime=(GET-Date &quot;17:00&quot;)<\/p>\n<p style=\"margin-left:30px\">$AzureAutomationAccount=&#039;HSG-AzureAutomation&#039;<\/p>\n<p style=\"margin-left:30px\">$TaskName=&#039;EndOfDayShutdownTask&#039;<\/p>\n<p style=\"margin-left:30px\">$DailyFrequency=1<\/p>\n<p style=\"margin-left:30px\">$Description=&#039;Daily Task to occur at 5pm EST to shut down my Azure VMs&#039;<\/p>\n<p style=\"margin-left:30px\">New-AzureAutomationSchedule &ndash;AutomationAccountName $AzureAutomationAccount &ndash;Name $TaskName &ndash;Starttime $StartTime &ndash;DailyInterval $DailyFrequency &ndash;Description $Description<\/p>\n<p>There is, of course, far more that you can do. This series has given you a quick entry level introduction to Azure Automation. If nothing else, it&#039;s a great way to keep costs under control when people forget to shut things down at the end of the day!<\/p>\n<p>I invite you to follow The Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send an email to The Scripting Guys at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then remember eat your cmdlets every day with a taste of creativity.<\/p>\n<p><b>Sean Kearney, <\/b>Windows PowerShell MVP and Honorary Scripting Guy<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Schedule an Azure Automation runbook to get Azure to work for you. Honorary Scripting Guy, Sean Kearney, is here to show you the most importance piece of Azure Automation: the actual scheduling of runbooks to allow you to let Azure do all the automation for you! This is the final post in a five-part [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[583,56,154,549,45],"class_list":["post-5741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-azure-automation","tag-guest-blogger","tag-sean-kearney","tag-series","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Schedule an Azure Automation runbook to get Azure to work for you. Honorary Scripting Guy, Sean Kearney, is here to show you the most importance piece of Azure Automation: the actual scheduling of runbooks to allow you to let Azure do all the automation for you! This is the final post in a five-part [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=5741"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=5741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}