{"id":5801,"date":"2015-06-02T00:01:00","date_gmt":"2015-06-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/06\/02\/using-azure-automation-part-2\/"},"modified":"2019-02-18T09:47:38","modified_gmt":"2019-02-18T16:47:38","slug":"using-azure-automation-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-2\/","title":{"rendered":"Using Azure Automation: Part 2"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary: <\/b><span style=\"font-size:12px\">Honorary Scripting Guy, Sean Kearney discusses how to get started and test a basic runbook with Azure Automation.<\/span><\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. Today I&#039;m going to show you the basics you&#039;re going to need to make a useful runbook (or Windows PowerShell workflow, if that makes it easier to remember) in Microsoft Azure. This is the second post in a five-part series. Be sure to read <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/using-azure-automation-part-1\/\" target=\"_blank\">Using Azure Automation: Part&nbsp;1<\/a> first.<\/p>\n<p>I&#039;ve started a very basic script block to get a list of virtual machines in Azure and stop them all. Nothing fancy.<\/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\/344776.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\/344776.1.PNG\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<p>If I want to, I can click the <b>Test<\/b> button to see how well I did or did not fare with my simple typing. This will attempt to parse my script and make it do something.<\/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\/65245.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\/65245.2.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>I wait happily thinking to myself, &quot;Ah ha! I am so smart! Got the work done the first time! Didn&#039;t have anybody proof it and&hellip;&quot;<\/p>\n<p>Apparently, I am not so smart because this message returns:<\/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\/4428.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\/4428.3.PNG\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<p>The test system in Azure Automation caught something I messed up. Clicking the <b>Details<\/b> button reveals why I should have checked my glucose this morning!<\/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\/5224.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\/5224.4.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Well, that was nice! Microsoft Azure caught where I goofed before I went to production (remember this runbook\/ is all in draft mode right now). I scan my script and add the missing piece to correct (at least) my lapse in logic and my common sense.<\/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\/8424.5.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\/8424.5.PNG\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<p>Sure enough. I forgot to put a semicolon in my <strong>Foreach<\/strong> loop!<\/p>\n<p>But anybody who knows Azure knows this workflow is doomed to fail regardless. &quot;Why?&quot; you ask? Did anybody see me authenticate to Azure?<\/p>\n<p>No? Oops!<\/p>\n<p>Which gets us to the part where Azure Automation starts to rock. It&#039;s not just about running a hosted Windows PowerShell workflow. It&#039;s about doing it in a secure manner, including storing credentials!<\/p>\n<p>Let&#039;s look at a basic script that has passwords in clear text doing pretty much the same thing (which is shutting down some virtual machines in Azure):<\/p>\n<p style=\"margin-left:30px\">import-module azure<\/p>\n<p style=\"margin-left:30px\">$pass=ConvertTo-SecureString &#039;Sup3rR@dk001&#039; -AsPlainText -Force<\/p>\n<p style=\"margin-left:30px\">$cred=New-object -TypeName system.management.automation.pscredential -ArgumentList &#039;AdminAccount@contoso.onmicrosoft.com&#039;,$pass<\/p>\n<p style=\"margin-left:30px\">Add-AzureAccount -credential $cred<\/p>\n<p style=\"margin-left:30px\">$VMlist=Get-AzureVM<\/p>\n<p style=\"margin-left:30px\">&nbsp;foreach ($VM in $VMList)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">$Result=Stop-AzureVM -name $VM.Name -servicename $VM.ServiceName -Force<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>We could probably use this script as it stands if we replaced in the Azure Automation runbook.<\/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\/6102.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\/6102.6.PNG\" alt=\"Image of script\" width=\"586\" height=\"226\" title=\"Image of script\" \/><\/a><\/p>\n<p>But wouldn&#039;t it be nice (oh so very nice) if we could store this securely?<\/p>\n<p>Cue drum roll, lights, and shiny objects! Enter the POWER of Azure Automation assets! Assets are where you can place objects that can be centrally accessed by Windows PowerShell workflows within your Azure Automation account.<\/p>\n<p>You can store simple stuff such as a URL for the company website or secure things&mdash;things you should not leave on a sticky note, things you shouldn&#039;t post on a whiteboard when a news reporter interviews you&hellip;<\/p>\n<p>Yes! Passwords, of course!<\/p>\n<p>To access the assets, go back to your Azure Automation account and select the <b>Assets<\/b> option.<\/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\/8446.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\/8446.7.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Within this page, you can simply go to the bottom and choose <b>Add Setting<\/b> to add an asset to the list.<\/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\/3005.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\/3005.8.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>There are four types of assets you can add: a simple variable, a schedule, an Azure connection, or the one we&#039;re most interested in, a credential.<\/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\/4784.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\/4784.9.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>You&#039;ll get an option to provide one of two credential types: a standard Windows PowerShell credential or a certificate. We&#039;re going to populate a Windows PowerShell credential.<\/p>\n<p>We give it a descriptive name and a very descriptive description (yes, I&#039;m allowed to have fun 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\/8816.10.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\/8816.10.PNG\" alt=\"Image of text boxes\" title=\"Image of text boxes\" \/><\/a><\/p>\n<p>We now enter in our credentials, the UserID, and the password, which&hellip;<\/p>\n<p>Oh my! You can&#039;t see it now can you?<\/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\/6320.11.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\/6320.11.PNG\" alt=\"Image of text boxes\" title=\"Image of text boxes\" \/><\/a><\/p>\n<p>I click the check mark and my asset is created!<\/p>\n<p>Swing by tomorrow and I&#039;ll show you the next part, which is how to get that runbook to access those assets.<\/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: Honorary Scripting Guy, Sean Kearney discusses how to get started and test a basic runbook with Azure Automation. Honorary Scripting Guy, Sean Kearney, is here. Today I&#039;m going to show you the basics you&#039;re going to need to make a useful runbook (or Windows PowerShell workflow, if that makes it easier to remember) in [&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-5801","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: Honorary Scripting Guy, Sean Kearney discusses how to get started and test a basic runbook with Azure Automation. Honorary Scripting Guy, Sean Kearney, is here. Today I&#039;m going to show you the basics you&#039;re going to need to make a useful runbook (or Windows PowerShell workflow, if that makes it easier to remember) in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5801","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=5801"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5801\/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=5801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}