{"id":78155,"date":"2016-05-24T00:01:00","date_gmt":"2016-05-24T07:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78155"},"modified":"2019-02-18T09:10:45","modified_gmt":"2019-02-18T16:10:45","slug":"work-with-the-azurerm-cmdlets-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/work-with-the-azurerm-cmdlets-part-2\/","title":{"rendered":"Work with the Azure Resource Manager cmdlets \u2013 Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Authenticate to Azure with the AzureRM cmdlets.<\/p>\n<p>This blog post is part of a series about how to work with the Azure Resource Manager cmdlets. To get the most out of this series, read the posts in order.<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/23\/work-with-the-azurerm-cmdlets-part-1\/\">Work with the Azure Resource Manager cmdlets \u2013 Part 1<\/a><\/li>\n<li>Work with the Azure Resource Manager cmdlets \u2013 Part 2<\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/25\/work-with-the-azurerm-cmdlets-part-3\/\">Work with the Azure Resource Manager cmdlets \u2013 Part 3<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/26\/work-with-the-azure-resource-manager-cmdlets-part-4\/\">Work with the Azure Resource Manager cmdlets \u2013 Part 4<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/27\/work-with-the-azure-resource-manager-cmdlets-part-5\/\">Work with the Azure Resource Manager cmdlets \u2013 Part 5<\/a><\/li>\n<\/ul>\n<p>After you finish this series, move on to the next two series to learn more about the Azure Resource Manager cmdlets.<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/05\/30\/retrieve-azure-resource-manager-virtual-machine-properties-by-using-powershell-part-1\/\">Retrieve Azure Resource Manager virtual machine properties by using PowerShell<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/06\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-1\/\">Create Azure Resource Manager virtual machines by using PowerShell<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>\u00a0I\u2019ve got the new AzureRM cmdlets installed, but I\u2019m just not sure where to start! Could you give me a nudge in the right direction?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0Honorary Scripting Guy, Sean Kearney, is here to help you out. The first real piece you need is to get authenticated to Azure Resource Manager before you can\u00a0get anything fun done.<\/p>\n<p>If you remember, we downloaded the AzureRM cmdlets for Azure Resource Manager yesterday. Just out of curiosity, did anybody look to see how many cmdlets were added? I did. I ran the following cmdlet to find out:<\/p>\n<p style=\"padding-left: 30px\"><code>(Get-Command \u2013module AzureRM*).count<\/code><\/p>\n<p>It turns out that just over 1,000 cmdlets are available to manage Azure Resource Manager. Today we\u2019re going to introduce you to the most important one, <strong>Add-AzureRMAccount<\/strong>.<\/p>\n<p>If you execute this cmdlet directly or in a script with no other parameters, you\u2019ll get the Microsoft Azure page to sign in.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-hsg-052416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78165\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-hsg-052416.png\" alt=\"Screenshot of the Microsoft Azure page where you sign in to your account.\" width=\"498\" height=\"508\" \/><\/a><\/p>\n<p>After you enter your credentials, you\u2019re in and can happily work away.<\/p>\n<p>But wait a minute!\u00a0Isn\u2019t this Windows PowerShell?\u00a0 Isn\u2019t this really all about automation?<\/p>\n<p>True. We need to show you a bit about how to automate all of this so that your script can do all the work while you get paid to do it.<\/p>\n<p>This is actually quite easy. To automate the connection of <strong>Add-AzureRMAccount<\/strong>, you\u2019ll need three key pieces of information.<\/p>\n<ul>\n<li>UserID and password (your credentials)<\/li>\n<li>SubscriptionID<\/li>\n<li>TenantID<\/li>\n<\/ul>\n<p>To obtain the SubscriptionID and the TenantID, you can capture the output produced from our authentication scenario.<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureRMInfo=Add-AzureRMAccount<\/code><\/p>\n<p>If you look at $AzureRMInfo, you\u2019ll note some interesting information that you\u2019ll need to automate this. It\u2019s the TenantID and SubscriptionID.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-hsg-052416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78166\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-hsg-052416.png\" alt=\"Screenshot of TenantId, SubscriptinId and corresponding values.\" width=\"524\" height=\"54\" \/><\/a><\/p>\n<p>However, if you run this against <strong>Get-Member<\/strong>, the information\u00a0won\u2019t be in such an obvious place.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-hsg-052416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78175\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-hsg-052416.png\" alt=\"Screenshot that shows some information that\u2019s buried under the Context property.\" width=\"676\" height=\"154\" \/><\/a><\/p>\n<p>The information is actually a bit buried under the <strong>Context<\/strong> property. If you look to pull it up under <strong>Get-Member<\/strong>, you\u2019ll spot two additional properties, <strong>Environment<\/strong> and <strong>Subscription<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-hsg-052416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78185\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-hsg-052416.png\" alt=\"Screenshot that shows the Environment and Subscriptions properties.\" width=\"536\" height=\"137\" \/><\/a><\/p>\n<p>If you\u2019re like me, you immediately thought \u201cAHA! I can just get the Subscription and be done! Muah ha ha ha!\u201d That is until you look at the property. You\u2019ll notice some \u201cextra pieces tossed in.\u201d<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-hsg-052416.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78186\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5-hsg-052416.png\" alt=\"Screenshot that shows information in addition to the SubscriptionId and TenantId that you need.\" width=\"536\" height=\"72\" \/><\/a><\/p>\n<p>In actual fact, to get the SubscriptionID, which is what you really need, you need to access it like this:<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureRMInfo.Context.Subscription.SubscriptionID<\/code><\/p>\n<p>Similarly, you\u2019ll find the TenantID is buried in the same fashion. You\u2019ll need to access it just under the <strong>Tenant<\/strong> property:<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureRMInfo.Context.Tenant.TenantID<\/code><\/p>\n<p>Now that you\u2019ve captured this data, you\u00a0can now either store it away in a file to retrieve it later or just hard code it into our script. You can even just pipe the information to the CLIP.exe command to save some typing.<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureRMInfo.Context.Subscription.SubscriptionID : CLIP<\/code><\/p>\n<p>To enter it into the script, assign a new object name, such as $SubscriptionID, and then use a friendly CTRL-V to paste it into the editor.<\/p>\n<p>With all of this information, you\u00a0can automate the connection to Azure Resource Manager. Building credentials is no different from any other Windows PowerShell scenario. You\u00a0can one of two options.<\/p>\n<ul>\n<li>Prompt before script execution with <strong>Get-Credential<\/strong>:<\/li>\n<\/ul>\n<p style=\"padding-left: 60px\"><code>$Credential=Get-Credential<\/code><\/p>\n<ul>\n<li>Build them out by using <strong>New-Object<\/strong>:<\/li>\n<\/ul>\n<p style=\"padding-left: 60px\"><code>$UserID=\u2018SomeID@Hotmail.com\u2019<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Password=\u2018P@ssw0rd\u2019<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$SecurePassword=Convertto-SecureString $Password \u2013asplaintext -force<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Credential=New-Object System.Management.Automation.PSCredential ($UserID,$SecurePassword)<\/code><\/p>\n<p>With all this information pulled together you can automate your connection.<\/p>\n<p style=\"padding-left: 30px\"><code>$UserID=\u2018SomeID@Hotmail.com\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Password=\u2018P@ssw0rd\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$SecurePassword=Convertto-SecureString $Password \u2013asplaintext -force<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$Credential=New-PsObject System.Management.Automation.PSCredential ($UserID,$SecurePassword)<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$SubscriptionID=\u2019 11111111-a111-11aa-11a1-aa1aaaa11111\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$TenantID=\u2019 11111111-1111-1111-1111-111111111111\u2019<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>Add-AzureRMAccount \u2013Credential $Credential \u2013tenantid $TenantID \u2013subscriptionid $SubscriptionID<\/code><\/p>\n<p>Pop in tomorrow to\u00a0see how to view and create some of the needed pieces for next week when we\u2019ll build out Azure virtual machines.<\/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 email to them 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.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Authenticate to Azure with the AzureRM cmdlets. This blog post is part of a series about how to work with the Azure Resource Manager cmdlets. To get the most out of this series, read the posts in order. Work with the Azure Resource Manager cmdlets \u2013 Part 1 Work with the Azure Resource Manager [&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":[568,641],"tags":[56,154,45],"class_list":["post-78155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Authenticate to Azure with the AzureRM cmdlets. This blog post is part of a series about how to work with the Azure Resource Manager cmdlets. To get the most out of this series, read the posts in order. Work with the Azure Resource Manager cmdlets \u2013 Part 1 Work with the Azure Resource Manager [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78155","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=78155"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78155\/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=78155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}