{"id":78775,"date":"2016-06-08T00:01:44","date_gmt":"2016-06-08T07:01:44","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78775"},"modified":"2019-02-18T09:10:39","modified_gmt":"2019-02-18T16:10:39","slug":"create-azure-resource-manager-virtual-machines-by-using-powershell-part-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-azure-resource-manager-virtual-machines-by-using-powershell-part-3\/","title":{"rendered":"Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 3"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use the Azure Resource Manager cmdlets to create and attach a virtual network card to a 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>Create Azure Resource Manager virtual machines by using PowerShell \u2013 Part 3<\/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\" \/> I\u2019m trying to find some guidance about how to use PowerShell to create a virtual network card. Would you be so kind as to lend me a hand?<\/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 today continuing forth (not C++) on our quest to create a virtual machine (VM) with the Azure Resource Manager cmdlets. Today, we\u2019re going to work on putting together a network card.<\/p>\n<p>Let\u2019s think back to a few weeks ago when we created some prerequisites for our environment, namely a virtual network and a network security group. These two pieces gave us the equivalent of:<\/p>\n<ul>\n<li>A VLAN or network wire system that we can attach to and communicate with (virtual network)<\/li>\n<li>A firewall or the ability to control traffic from said network (network security group)<\/li>\n<\/ul>\n<p>What our VM will need is a network card (really a virtual one). A great improvement in Azure Resource Manager is that you can create and attach multiple network cards.<\/p>\n<p>Each of these virtual NICs can be attached to a separate virtual network, too, just as if you had a system with one leg on the perimeter network and a separate card on a vendor network.<\/p>\n<p>Last week, we discovered that it wasn\u2019t tricky to get the network security group or the virtual network in use by a VM\u2019s network cards.<\/p>\n<p>We also need to be aware that we can ask Azure Resource Manager those questions as well. To obtain available network security groups, we can use the <strong>Get-AzureRMNetworkSecurityGroup<\/strong> cmdlet.<\/p>\n<p>In our case, we know of the security group because we created one a few weeks ago. We also did a neat little trick last Friday to get the information from a VM that was already using one.<\/p>\n<p style=\"padding-left: 30px\"><code>$NSG=\u2019HSG-NetworkSecurityGroup\u2019<\/code><\/p>\n<p>The other piece that we will need, of course, is the virtual network that we should attach to. Again, we could just grab the settings from an existing VM. However, it\u2019s far more useful to see what\u2019s available for you to consume.<\/p>\n<p>The <strong>Get-AzureRMVirtualNetwork<\/strong> cmdlet will dump all this onto the screen. Of course, the default output won\u2019t be all that useful. You\u2019ll find there are three properties that you\u2019ll be interested in. They are Name, AddressSpace, and Subnets:<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRMVirtualNetwork | Select-Object Name, AddressSpace, Subnets<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060816.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78776\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-060816.png\" alt=\"Screenshot that shows results from the Get-AzureRMVirtualNetwork cmdlet to return the name, address space, and subnet.\" width=\"572\" height=\"44\" \/><\/a><\/p>\n<p>AddressSpace will need to have its property expanded to show the really useful information, which is the network space (192.168.0.0 or 10.0.0.0 etc.) that it\u2019s actually a part of. Subnets from this view will show you the name and little else. Here, we need to do a little magic with <strong>Select-Object<\/strong>.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRmVirtualNetwork | Select-Object Name, @{Name='AddressSpace'; Expression={ $_.AddressSpace.AddressPrefixes}}<\/code><\/p>\n<p>This will pull out the value of AddressSpace so that we can read it properly:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060816.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78785\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-060816.png\" alt=\"Screenshot that shows the value of the AddressSpace property.\" width=\"242\" height=\"56\" \/><\/a><\/p>\n<p>We\u2019ll also need to do some similar work to access the information from the Subnets property. If you take a quick look at the property, you\u2019ll note that not only do we have the name, but we also have the value for each Subnet. An Id, which we&#8217;ll need to acquire, is also attached to each Subnet.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060816.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78795\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-HSG-060816.png\" alt=\"Screenshot that shows the value of the Subnet property.\" width=\"480\" height=\"205\" \/><\/a><\/p>\n<p>Accessing these will be similar to pulling out the extra information from the AddressSpace. There\u2019s a little more \u201cExpression\u201d magic, of course, as we acquire the additional properties.<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRmVirtualNetwork | Select-Object Name, @{Name='SubnetName'; Expression={ $_.Subnets.Name}},@{Name='SubnetAddressPrefix';Expression={$_.Subnets.AddressPrefix}}<\/code><\/p>\n<p>After we have all the information, we can have an easier time visualizing the virtual networks that we\u2019ll need to use and the Subnets to choose. We can tie it all together with the following line in PowerShell. We\u2019re just tying up all three custom expressions into one:<\/p>\n<p style=\"padding-left: 30px\"><code>Get-AzureRmVirtualNetwork | Select-Object Name, @{Name='AddressSpace'; Expression={ $_.AddressSpace.AddressPrefixes}}, @{Name='SubnetName'; Expression={ $_.Subnets.Name}},@{Name='SubnetAddressPrefix';Expression={$_.Subnets.AddressPrefix}}, @{Name='SubnetId';Expression={$_.Subnets.Id}}<\/code><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060816.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78805\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4-HSG-060816.png\" alt=\"Screenshot that shows the information for the Name, AddressSpace, and Subnet properties.\" width=\"378\" height=\"87\" \/><\/a><\/p>\n<p>After we have identified the network and, more importantly, the Subnet that we need, we just need the Subnet ID. We can get this with the following bit of PowerShell:<\/p>\n<p style=\"padding-left: 30px\"><code>$SubnetList=Get-AzureRmVirtualNetwork | Select-Object Name, @{Name='AddressSpace'; Expression={ $_.AddressSpace.AddressPrefixes}}, @{Name='SubnetName'; Expression={ $_.Subnets.Name}},@{Name='SubnetAddressPrefix';Expression={$_.Subnets.AddressPrefix}}, @{Name='SubnetId';Expression={$_.Subnets.Id}}<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$SubnetID=($SubnetList | Where { $_.SubnetAddressPrefix \u2013eq \u201910.0.0.0\/24\u2019 }).SubnetID<\/code><\/p>\n<p>Excellent. So, we now have a way to view the various network names and security groups. Now for the fun part: creating the network card for the VM.<\/p>\n<p>For this, we will be using three cmdlets.<\/p>\n<p>The first is <strong>New-AzureRMPublicIPAddress<\/strong>, which gives us a publicly accessible Internet IP that\u2019s bound to our VM. If you need, you can remove it later after post configuration if the VM does <u>not<\/u> need direct remote access.<\/p>\n<p>This object needs to be given a name, and the name has to be unique to the VM in question. For our examples, we will use the VM name that we selected from Monday as the identifying marker.<\/p>\n<p>In addition, the public IP will need a label, and it must be in all lowercase. A <strong>tolower()<\/strong> method can be used for this:<\/p>\n<p style=\"padding-left: 30px\"><code>$Nic=$VMName+\u2019_Nic1\u2019\n$RGName=\u2019HSG-AzureRG\u2019\n$Location=\u2019eastus\u2019\n$PublicIP = New-AzureRmPublicIpAddress -ResourceGroupName $RGName -Name $Nic -Location $Location -AllocationMethod Dynamic -DomainNameLabel $VMName.ToLower()<\/code><\/p>\n<p>Our next task is to attach a network card to this VM. This is done with a combination of two cmdlets. <strong>New-AzureRMNetworkInterface<\/strong> needs to be used to create the object for the VM.<\/p>\n<p style=\"padding-left: 30px\"><code># Add Network card<\/code><\/p>\n<p style=\"padding-left: 30px\"><code>$NIC=New-AzureRMNetworkInterface \u2013Force \u2013Name $VMName \u2013ResourceGroupName $RGName \u2013Location $Location \u2013SubnetID $Subnet.ID \u2013PublicIPAddress $PublicIP.ID<\/code><\/p>\n<p>Afterwards, we just add the newly created network object to the VM in Azure by using the <strong>Add-AzureRMVMNetworkInterface<\/strong> cmdlet.<\/p>\n<p style=\"padding-left: 30px\"><code>$AzureVM = Add-AzureRmVMNetworkInterface -VM $AzureVM -Id $NIC.Id<\/code><\/p>\n<p>Now, we\u2019ve completed one of the most important pieces, the ability for our VM to communicate with other systems.<\/p>\n<p>Tomorrow? We choose an operating system!<\/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 the Azure Resource Manager cmdlets to create and attach a virtual network card to a 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 [&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-78775","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 create and attach a virtual network card to a 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78775","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=78775"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78775\/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=78775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}