{"id":78845,"date":"2016-06-10T00:01:50","date_gmt":"2016-06-10T07:01:50","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78845"},"modified":"2019-02-18T09:10:38","modified_gmt":"2019-02-18T16:10:38","slug":"create-azure-resource-manager-virtual-machines-by-using-powershell-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-5\/","title":{"rendered":"Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 5"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use the Azure Resource Manager cmdlets to assign credentials and create the virtual machine.<\/p>\n<p>This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. 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\/06\/06\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-1\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 1<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/07\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-2\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 2<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/08\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-3\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 3<\/a><\/li>\n<li><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/09\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-4\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 4<\/a><\/li>\n<li>Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 5<\/li>\n<\/ul>\n<p><span style=\"color: #008000\">If you\u2019re new to PowerShell, you might want to start with the two series that precede this series:<\/span><\/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<\/a><\/li>\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<\/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\" \/> Now that I\u2019ve built all the objects for the virtual machine (VM), what\u2019s left before we spin up the machine in Azure Resource Manager?<\/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\" \/> Honorary Scripting Guy, Sean Kearney, is here to finish up our three-week session about VMs in Azure Resource Manager using PowerShell.<\/p>\n<p>Let\u2019s consider everything that we\u2019ve seen over the past few weeks on a high level.<\/p>\n<ul>\n<li>Create the necessary resources and infrastructure for Azure VMs<\/li>\n<li>Spin up a VM and access its properties by using PowerShell<\/li>\n<li>Identify the necessary properties to create a new VM<\/li>\n<\/ul>\n<p>Then, finally, over the last few days, we\u2019ve been putting together that VM.<\/p>\n<p>Our last step is actually pretty simple, but it\u2019s one of the more important steps. We need to assign the primary UserId and password for the VM. There are, of course, some important and critical details.<\/p>\n<p>You obviously cannot use a UserId of \u2018root\u2019 in the Linux environment or \u2018Administrator\u2019 in the Windows world as they are both built in. In the case of Windows, this account would be disabled by default.<\/p>\n<p>Now an important note on the password. It can\u2019t be anything obvious. In fact, if you <u>try<\/u>, Azure will reject it. So \u2018Password\u2019,\u2019P@ssw0rd\u2019 are actually right off the list. Won\u2019t even let you, so don\u2019t try.<\/p>\n<p>Well, you <u>can<\/u> try but, it\u2019s only useful if you like red error messages. \ud83d\ude09<\/p>\n<p>The next cmdlet is <strong>Set-AzureVMOperatingSystem<\/strong>, and its task is to identify the key pieces for our operating system.<\/p>\n<ul>\n<li>The host name to assign to the operating system<\/li>\n<li>The primary user name<\/li>\n<li>The password<\/li>\n<li>The operating system type<\/li>\n<\/ul>\n<p>The Hostname (or NetBIOS name in the Windows world) is typically the same as the VM name. It <u>can<\/u> be unique, but you\u2019re better off to avoid digging in the cloud for the actual name. Let\u2019s just say, this is a good practice to, at least, have them match.<\/p>\n<p>So, remember earlier that we defined our VM name with the following line:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMName=\u2019HSG-Server1\u2019<\/code><\/p>\n<p>We\u2019ll just use this value for the computer name. To assign the credentials (the UserID and password), we need to create a <strong>PSCredential<\/strong> object. This is the same process that we use when we create credentials for PowerShell normally.<\/p>\n<p>You can choose the interactive method (which is a lower risk on security) in the following manner:<\/p>\n<p style=\"padding-left: 30px\"><code>$Credential=Get-Credential<\/code><\/p>\n<p>You could prepopulate the UserID as well and just pass this to the <strong>Get-Credential<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\"><code>$UserID=\u2019HSGAdmin\u2019\n$Credential=Get-Credential \u2013credential $UserID<\/code><\/p>\n<p>The third option is to fully automate it. In a normal fully exposed PowerShell script, there is high level of risk because the password is in clear text. However, if you are using Systems Center Orchestrator or Azure Automation, you can mitigate this risk by storing the credentials as a secure asset.<\/p>\n<p style=\"padding-left: 30px\"><code>$UserID=\u2019HSGAdmin'\n$Password='NotSoSecureP@ssw0rd'\n$SecurePassword=Convertto-SecureString $Password \u2013asplaintext -force\n$Credential=New-Object System.Management.Automation.PSCredential ($UserID,$SecurePassword)<\/code><\/p>\n<p>We now define the settings for our operating system by using <strong>Set-AzureRMVMOperatingSystem<\/strong>:<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureVM = Set-AzureRmVMOperatingSystem -VM $AzureVM -Windows -ComputerName $VMname -Credential $cred -Windows<\/code><\/p>\n<p>If this were a Linux operating system, we would simply change the <strong>\u2013Windows<\/strong> switch to a <strong>\u2013Linux<\/strong> switch.<\/p>\n<p>After we have defined all the particular objects, we need to call up only one cmdlet to do all the magic. We need to supply only three parameters: our <strong>ResourceGroupName<\/strong>, <strong>Location<\/strong>, and <strong>VMimage<\/strong> object that we have been building.<\/p>\n<p style=\"padding-left: 30px\"><code>$RGName=\u2019HSG-AzureRG\n$Location=\u2019eastus\u2019\nNew-AzureRMVM \u2013ResourceGroupName $RGName \u2013location $Location \u2013VM $AzureVM<\/code><\/p>\n<p>We\u2019ll let this run through and, given some appropriate time, we\u2019ll have a new VM spun up in Azure!<\/p>\n<p>I hope the information that we\u2019ve provided will make it easier to create VMs in Azure. Azure really offers some amazing features outside of just infrastructure as a service. The amazing piece as I dig into it is that almost <u>everything<\/u> can be accessed and modified by using Windows PowerShell!<\/p>\n<p>Please check it all out! Hope to share more with you soon!<\/p>\n<p>If you&#8217;d like a sample of the PowerShell scripts that were used in this series of blog posts, you can download them from the <a href=\"https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Sample-AzureRM-for-HSG-ff468dbb\" target=\"_blank\">TechNet Script Repository<\/a>.<\/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: Use the Azure Resource Manager cmdlets to assign credentials and create the virtual machine. This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order. Create Azure Resource Manager virtual machines by [&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,685,641],"tags":[56,154,45],"class_list":["post-78845","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the Azure Resource Manager cmdlets to assign credentials and create the virtual machine. This blog post is part of a series about how to create Azure Resource Manager virtual machines by using PowerShell. To get the most out of this series, read the posts in order. Create Azure Resource Manager virtual machines by [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78845","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=78845"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78845\/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=78845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}