{"id":78727,"date":"2016-06-06T00:01:35","date_gmt":"2016-06-06T07:01:35","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78727"},"modified":"2019-02-18T09:10:40","modified_gmt":"2019-02-18T16:10:40","slug":"create-azure-resource-manager-virtual-machines-by-using-powershell-part-1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-1\/","title":{"rendered":"Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 1"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use PowerShell cmdlets to start to create a virtual machine in Azure Resource Manager.<\/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>Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 1<\/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><a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/06\/10\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-5\/\">Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 5<\/a><\/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\" \/> How do I create virtual machines (VMs) in Azure Resource Manager by using PowerShell? Where do I start?<\/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, and we\u2019re going to give you some basic step-by-step guidance this week about how to spin up a VM with Azure Resource Manager PowerShell cmdlets. We\u2019re also going to reference back to some cool tricks that we picked up last week to obtain data from a previous VM so that you don\u2019t have to go about digging for the settings.<\/p>\n<p>When I go to prepare a VM in Azure Resource Manager (outside of the actual preparation of the infrastructure such as our network or resource groups), I tend to think of six key tasks:<\/p>\n<ul>\n<li>Set up the base configuration (VM size and name)<\/li>\n<li>Set up the storage for the operating system image<\/li>\n<li>Define the network configuration (virtual network, card, subnet, security group)<\/li>\n<li>Set up the operating-specific details (template for the image)<\/li>\n<li>Assign the user ID and password for the operating system<\/li>\n<li>Spin up the VM<\/li>\n<\/ul>\n<p>When I think of these six small pieces and look at them within a script, you can actually visualize the layers of the VM falling together like a well-defined structure.<\/p>\n<p>The other part that makes all of this far easier in Azure Resource Manager is knowing that you can create a VM (like we did last week) within the portal and then access its properties to know <u>what<\/u> to supply to PowerShell.<\/p>\n<p>As you know, often it\u2019s not about what parameters I need to use but more about what values are <u>valid<\/u>. Because we can get valid values from other VMs within Azure Resource Manager (even if the VMs aren\u2019t powered up), it\u2019s far easier on the IT Pro. You can do it in the Azure portal. If you need to replicate the solution multiple times, you can take a sample PowerShell script for a VM and plug in the appropriate values.<\/p>\n<p>At this point we should actually start the process, shouldn\u2019t we? Well then, release the cmdlets!<\/p>\n<p>First, as always, we need to authenticate to Azure Resource Manager by using the <strong>Add-AzureRMAccount<\/strong> cmdlet. To give you a quick and simple reference, here is a small sample script. It contains the subscription ID and tenant ID and prompts for the <strong>LoginID<\/strong>.<\/p>\n<p style=\"padding-left: 30px\"><code>$SubscriptionID='00000000-0000-0000-0000-000000000000'\n$TenantID='00000000-0000-0000-0000-000000000000'\n$UserID='AzureLoginID@Contoso.OnMicrosoft.com'\n$Credential=Get-Credential $UserID\nAdd-AzureRMAccount -credential $Credential -tenantid $TenantID -subscriptionid $SubscriptionID<\/code><\/p>\n<p>Now that we\u2019re authenticated, we can start to do the first big piece: start up the configuration.<\/p>\n<p>The first cmdlet that we\u2019re going to use is the <strong>New-AzureRMVMConfig<\/strong> cmdlet. Its task is actually quite simple: to lay down the very first piece of our VM definition.<\/p>\n<p>The cmdlet will need two parameters at a minimum:<\/p>\n<ul>\n<li>The VM (and by proxy, the computer name)<\/li>\n<li>The machine size<\/li>\n<\/ul>\n<p>If you have an availability set defined, which is a key piece that you need to provide redundancy in Azure, you can provide it here.<\/p>\n<p>The VM size is pretty critical depending on your need and your loads. A complete description and sizing chart is at <a href=\"https:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/virtual-machines-windows-sizes\/\">Sizes for virtual machines in Azure<\/a>.<\/p>\n<p>The easy answer for the size to choose is, \u201cIt depends.\u201d The amazing answer to everything from a consultant.<\/p>\n<p>Put these words in your head to determine the answer:<\/p>\n<ul>\n<li>Budget<\/li>\n<li>Availability<\/li>\n<li>Performance<\/li>\n<\/ul>\n<p>In some cases, you can have a very expensive environment that only starts up and shuts down for hours, days, or even a month. You can also have several smaller solutions that are spread across different locations. This can take some thought.<\/p>\n<p>For our reference, we\u2019re going to presume that you\u2019re working with your last VM as a reference and are satisfied with the size.<\/p>\n<p>From last week, we obtained the size of our previous VM by grabbing the VM object from Azure Resource Manager and obtaining the size property directly.<\/p>\n<p style=\"padding-left: 30px\"><code># Get Virtual Machine Object from AzureRM\n$VM=Get-AzureRMVM \u2013ResourceGroupName HSG-AzureRG \u2013Name HSG-Linux1\n# Retrieve Virtual Machine Size from Object\n$VMSize=$vm.HardwareProfile.VmSize<\/code><\/p>\n<p>In the case of this particular VM, I piped the results to the CLIP command to store it on the clipboard and pasted it into my CreateAzureVM.ps1 Script.<\/p>\n<p style=\"padding-left: 30px\"><code>$VMSize | Clip<\/code><\/p>\n<p>One CTRL-V or mouse click later\u2026<\/p>\n<p style=\"padding-left: 30px\"><code>$VMSize=\u2019 Standard_A0\u2019<\/code><\/p>\n<p>At this point, we choose a new VM name:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMName=\u2019HSG-Server1\u2019<\/code><\/p>\n<p>Then, we define the configuration by using this cmdlet:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMConfig=New-AzureRMVMConfig \u2013VMName $VMName \u2013VMSize $VMSize<\/code><\/p>\n<p>This <u>really<\/u> doesn\u2019t do anything in Azure yet. It\u2019s just defining an object so that it won\u2019t trap for the size or the name.<\/p>\n<p>But, trapping for the name wouldn\u2019t be difficult. You could just run something simple like this first:<\/p>\n<p style=\"padding-left: 30px\"><code>$VMFound=$NULL; $VMFound=Get-AzureRMVM \u2013name $VMName \u2013ResourceGroupName HSG-AzureRG\nIf ($VMFound)\n{\nWrite-output \u201cVirtual Machine name $VMName Already Exists\u201d\n}\nElse\n{\n$VMConfig=New-AzureRMVMConfig \u2013VMName $VMName \u2013VMSize $VMSize\n}<\/code><\/p>\n<p>We have now started the base. Tomorrow, we\u2019ll continue on with defining our storage for our new VM in Azure Resource Manager.<\/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\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use PowerShell cmdlets to start to create a virtual machine in Azure Resource Manager. 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-78727","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 PowerShell cmdlets to start to create a virtual machine in Azure Resource Manager. 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\/78727","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=78727"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78727\/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=78727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}