{"id":4114,"date":"2015-02-28T15:08:06","date_gmt":"2015-02-28T14:08:06","guid":{"rendered":"http:\/\/contoso.se\/blog\/?p=4114"},"modified":"2015-02-28T15:08:06","modified_gmt":"2015-02-28T14:08:06","slug":"deploy-new-vm-and-attach-a-template-data-disk","status":"publish","type":"post","link":"http:\/\/contoso.se\/blog\/?p=4114","title":{"rendered":"Deploy new VM and attach a template data disk"},"content":{"rendered":"<p>I was working on a scenario were we needed to deploy new\u00c2\u00a0VMs and attach a data disk that included a number of tools. This Azure Powershell script will deploy a new VM, make a copy of a template data disk\u00c2\u00a0with all the tools, and attach the disk to the new VM.<\/p>\n<p>Before you run the script you need to update at least network settings, source storage Account and target storage account. You might also update VMsize, CloudService and VMName. The current script will create a new VM with a named based on the current date and time. Information about Your storage accounts can be found in the Azure management portal, under each storage Account.<\/p>\n<p>## Parameters<\/p>\n<p>### Admin account settings<br \/>\n$login = &#8220;AdminUser&#8221;<br \/>\n$Password = &#8220;YourSecretPassword!!&#8221;<\/p>\n<p>### Vm name and cloud service settings<br \/>\n$TimeStamp = Get-Date -Format ddHHmmss<br \/>\n$NewVMName = &#8220;TEST-&#8221; + $TimeStamp<br \/>\n$CloudServiceName = Get-AzureService -ServiceName myCloudService<br \/>\n$InstanceSize = &#8220;Small&#8221;<\/p>\n<p>### Network Settings<br \/>\n$SubnetName = &#8220;Subnet-1&#8221;<br \/>\n$VNetName = &#8220;vnet01&#8221;<\/p>\n<p>### Source Storage Account and tool disk source URL<br \/>\n$srcStorageAccount = &#8220;contosostorage201501&#8221;<br \/>\n$srcStorageKey = &#8220;FJHmK\/\/VbvHeeEgRqukL1015XjP2mU+DQvy65TmCR9TMw==&#8221;<br \/>\n$srcUri = &#8220;https:\/\/contoso0001.blob.core.windows.net\/vhds\/myToolsDisk.vhd&#8221;<\/p>\n<p>### Target Storage Account and blob container settings<br \/>\n$destStorageAccount = &#8220;contoso0002&#8221;<br \/>\n$destStorageKey = &#8220;f94MIsNiwrv4qJBqEOfbVUQeSNkvT4QA==&#8221;<br \/>\n$containerName = &#8220;vhds&#8221;<\/p>\n<p>##################################################################<br \/>\n##################################################################<br \/>\n### NO NEED TO CHANGE BELOW THIS LINE ###########################<br \/>\n##################################################################<br \/>\n##################################################################<\/p>\n<p>##<br \/>\n## Deploy new Windows Server 2012 R2 Datacenter Edition<br \/>\n##<\/p>\n<p>$image = Get-AzureVMImage | where {$_.Label -like &#8220;Windows Server 2012 R2 Datacenter*&#8221;} | select -First 1<br \/>\n$vmconfig = New-AzureVMConfig -ImageName $image.ImageName -InstanceSize $InstanceSize -name $NewVMName<br \/>\nAdd-AzureProvisioningConfig -Windows -AdminUsername $login -Password $Password -vm $vmconfig<br \/>\nSet-AzureSubnet -SubnetNames $SubnetName -vm $vmconfig<br \/>\nNew-AzureVM -servicename $CloudServiceName.ServiceName -vms $vmconfig -VNetName $VNetName -WaitForBoot<\/p>\n<p>##<br \/>\n## Stop the VM<br \/>\n##<\/p>\n<p>Stop-AzureVM -Name $NewVMName -ServiceName $CloudServiceName.Label<\/p>\n<p>###<br \/>\n### Create the source storage account context<br \/>\n###<\/p>\n<p>$srcContext = New-AzureStorageContext \u00e2\u20ac\u201cStorageAccountName $srcStorageAccount -StorageAccountKey $srcStorageKey<\/p>\n<p>###<br \/>\n### Create the destination storage account context<br \/>\n###<\/p>\n<p>$destContext = New-AzureStorageContext \u00e2\u20ac\u201cStorageAccountName $destStorageAccount -StorageAccountKey $destStorageKey<\/p>\n<p>###<br \/>\n### Create the container on the destination, if not already existing<br \/>\n###<\/p>\n<p>$containers = Get-AzureStorageContainer -Context $destContext -Name vhds<br \/>\nif($containers.count -eq 0){<br \/>\nNew-AzureStorageContainer -Name $containerName -Context $destContext<br \/>\nWrite-host &#8220;Container created&#8221;<br \/>\n} else {<br \/>\nWrite-host &#8220;Container already exists&#8221;<br \/>\n}<\/p>\n<p>###<br \/>\n## Configure new VHD file name<br \/>\n###<\/p>\n<p>$DestVHD = $NewVMName + &#8220;-tools-disk.vhd&#8221;<br \/>\n$DestVHDURL = $destContext.BlobEndPoint + $containerName + &#8220;\/&#8221; + $DestVHD<\/p>\n<p>###<br \/>\n### Start the asynchronous copy<br \/>\n###<\/p>\n<p>$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri -SrcContext $srcContext -DestContainer $containerName -DestBlob $DestVHD -DestContext $destContext<\/p>\n<p>##<br \/>\n## Register VHD as a Data Disk<br \/>\n##<\/p>\n<p>Add-AzureDisk -DiskName $destVHD -MediaLocation $DestVHDURL -Label $destVHD<\/p>\n<p>##<br \/>\n## Add the disk<br \/>\n##<\/p>\n<p>Get-AzureVM -ServiceName $CloudServiceName.Label -Name $NewVMName | Add-AzureDataDisk -Import -DiskName $DestVHD -LUN 0 | Update-AzureVM<\/p>\n<p>##<br \/>\n## Start the VM again<br \/>\n##<\/p>\n<p>Start-AzureVM -Name $NewVMName -ServiceName $CloudServiceName.Label<br \/>\nWrite-host &#8220;##################&#8221;<br \/>\nWrite-host &#8220;### FINISHED!! ###&#8221;<br \/>\nWrite-host &#8220;##################&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a scenario were we needed to deploy new\u00c2\u00a0VMs and attach a data disk that included a number of tools. This Azure Powershell script will deploy a new VM, make a copy of a template data disk\u00c2\u00a0with all the tools, and attach the disk to the new VM. Before you run the &hellip; <a href=\"http:\/\/contoso.se\/blog\/?p=4114\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4114"}],"collection":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4114"}],"version-history":[{"count":3,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4114\/revisions"}],"predecessor-version":[{"id":4117,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4114\/revisions\/4117"}],"wp:attachment":[{"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4114"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/contoso.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}