{"id":3007,"date":"2015-03-10T16:26:19","date_gmt":"2015-03-10T07:26:19","guid":{"rendered":"http:\/\/www.skyarch.net\/blog\/?p=3007"},"modified":"2015-03-10T17:49:06","modified_gmt":"2015-03-10T08:49:06","slug":"windows-azure-virtual-machines-with-powershell","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/en\/windows-azure-virtual-machines-with-powershell\/","title":{"rendered":"Windows Azure Virtual Machines with PowerShell"},"content":{"rendered":"<p><strong>PowerShell<\/strong> is good to start with, you can do some seriously powerful stuff with it. Like performing a variety of tasks in Azure. PowerShell provides cmdlets to manage Azure. You can use the cmdlets to perform the same tasks that you can perform through the Azure Management Portal. For example create and configure Windows Azure Virtual Machine.<\/p>\n<p><font color=\"#CC000\"><strong>Note:<\/strong> To use Azure cmdlets you must have Windows Azure PowerShell modules that requires Microsoft .NET Framework 4.5 and Powershell 3.0<\/font><\/p>\n<h2>How to create a Windows Azure Virtual Machine with PowerShell?<\/h2>\n<p>I created a script, so that I don't need to do it manually every time I want to create Windows Azure  Virtual Machine with PowerShell. So step one was creating a script, and below is the powershell cmdlets I used for my script.<\/p>\n<h3>0. Your global variable<\/h3>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$SubscriptionName = &quot;SUBSCRIPTION NAME&quot;\r\n$SourceImage = &quot;IMAGE SOURCE&quot;\r\n$ServiceLocation = &quot;SERVICE LOCATION&quot;\r\n$DNSName = &quot;VM DNS NAME&quot;\r\n$VMName = &quot;VM NAME&quot;\r\n$VMSize = &quot;VM SIZE&quot;\r\n$VMUser = &quot;VM USER&quot;\r\n$VMPassword = &quot;VM PASSWORD&quot;\r\n$StorageAccountName = &quot;STORAGE ACCOUNT&quot;\r\n<\/pre>\n<p>To get your <strong>$SubscriptionName<\/strong>, type in your powershell console<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n(Get-AzureSubscription).SubscriptionName\r\n<\/pre>\n<p>As part of the above process I had to specify an image name (<strong>$SourceImage<\/strong>) for the virtual machine to be based on - you can look these up easily with the<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\"> \r\nGet-AzureVMImage &gt; C:\\imagelist.txt\r\n<\/pre>\n<p>This will return a list of all available images which you can filter your way through as you need. For example, I was using a Server 2012 image which had an imagename of \"a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201412.01-en.us-127GB.vhd\".<\/p>\n<p>Virtual Machine size <strong>$VMSize<\/strong> = <em>\"Small, Medium, Large, and etc\"<\/em> see <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/dn197896.aspx\" title=\"Virtual Machine size\" target=\"_blank\">https:\/\/msdn.microsoft.com\/en-us\/library\/azure\/dn197896.aspx<\/a> for more details.<\/p>\n<p>And for your <strong>$ServiceLocation<\/strong>, see image below.<br \/>\n<a href=\"http:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Azure-Subscription1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Azure-Subscription1.png\" alt=\"Azure Subscription\" width=\"284\" height=\"168\" class=\"alignnone size-full wp-image-2938\" \/><\/a><\/p>\n<h3>1. Add Azure Account Details.<\/h3>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# The cmdlets need your subscription so they can manage your services.\r\nAdd-AzureAccount\r\nSet-AzureSubscription -SubscriptionName $SubscriptionName \r\n<\/pre>\n<h3>2. Provision Storage Account.<\/h3>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nNew-AzureStorageAccount -StorageAccountName $StorageAccountName -Location $ServiceLocation -Type &quot;Standard_LRS&quot;\r\n\r\n# Set storage account as default one, otherwise you will not be able to build your VM\r\nSet-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccount $StorageAccountName \r\n<\/pre>\n<h3>3. Create an Azure Virtual Machine.<\/h3>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nNew-AzureVMConfig -Name $VMName -InstanceSize $VMSize -ImageName $SourceImage.Trim() | Add-AzureProvisioningConfig -Windows -AdminUsername $VMUser -Password $VMPassword | New-AzureVM -ServiceName $DNSName -Location $ServiceLocation -WaitForBoot \r\n<\/pre>\n<h3>4. Waiting for Virtual Machine to reach status \"ReadyRole\" then edit the Endpoint,<\/h3>\n<p>so you can use powershell to remote your VM.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nDo {}While ((Get-AzureVM -Name $VMName -ServiceName $DNSName).status -ne &quot;ReadyRole&quot;)\r\n\r\nGet-AzureVM -ServiceName $DNSName -Name $VMName | Set-AzureEndpoint -Name &quot;PowerShell&quot; -PublicPort 5986 -LocalPort 5986 -Protocol &quot;tcp&quot; | Update-AzureVM\r\n<\/pre>\n<h3>And here are the other cmdlets you can use to manage your VM.<\/h3>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Stop, start or restart the VM\r\nStop-AzureVM -Name $VMName -ServiceName $DNSName \r\nStart-AzureVM -Name $VMName -ServiceName $DNSName \r\nRestart-AzureVM -Name $VMName -ServiceName $DNSName \r\n\r\n# Remove VM, cloud service and storage account\r\nRemove-AzureVM -Name $VMName -ServiceName $DNSName \r\nRemove-AzureVM -ServiceName $DNSName \r\nRemove-AzureStorageAccount -StorageAccountName $StorageAccountName\r\n<\/pre>\n<p>You are good to go as easy as it is!. Here how I run my script.<br \/>\nRun <strong><em>Powershell<\/em><\/strong> console as <strong><em>Administrator<\/em><\/strong><br \/>\ntype <strong><em>Set-ExecutionPolicy RemoteSigned -Force<\/em><\/strong><br \/>\nthen <strong><em>.\\Create_AzureVM.ps1<\/em><\/strong><\/p>\n<a href=\"http:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Create-Azure-VM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Create-Azure-VM.png\" alt=\"Create Azure VM\" width=\"959\" height=\"698\" class=\"alignnone size-full wp-image-2933\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Create-Azure-VM.png 959w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Create-Azure-VM-300x218.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2015\/03\/Create-Azure-VM-900x655.png 900w\" sizes=\"auto, (max-width: 959px) 100vw, 959px\" \/><\/a>\n<p style=\"text-align: right;width:100%;margin:0;padding:0\"><a href=\"http:\/\/www.skyarch.net\/blog\/?p=2956\"><strong>Windows Azure Virtual Machines with Remote PowerShell \u25ba<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell is good to start with, you can do some seriously powerful stuff with it. Like performing a variety &#8230;<\/p>\n","protected":false},"author":1,"featured_media":2948,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"2915","footnotes":""},"categories":[88,83,18,30],"tags":[],"class_list":{"0":"post-3007","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-azure","8":"category-devops","9":"category-os","10":"category-windows","11":"en-US"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/3007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=3007"}],"version-history":[{"count":2,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/3007\/revisions"}],"predecessor-version":[{"id":3013,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/3007\/revisions\/3013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/2948"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=3007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=3007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=3007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}