-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateVmFromManagedOsDisk.ps1
65 lines (44 loc) · 2.72 KB
/
CreateVmFromManagedOsDisk.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.DESCRIPTION
This sample demonstrates how to create a virtual machine by attaching an existing Managed Disk as OS Disk.
.NOTES
1. Before you use this sample, please install the latest version of Azure PowerShell from here: http://go.microsoft.com/?linkid=9811175&clcid=0x409
2. Provide the appropriate values for each variable. Note: The angled brackets should not be included in the values you provide.
#>
#Provide the subscription Id
$subscriptionId = 'yourSubscriptionId'
#Provide the name of your resource group
$resourceGroupName ='yourResourceGroupName'
#Provide the name of the Managed Disk
$diskName = 'yourDiskName'
#Provide the Azure region (e.g. westus) where virtual machine will be located.
#This location should be same as the Managed Disk location
#Get all the Azure location using command below:
#Get-AzureRmLocation
$location = 'westus'
#Provide the name of an existing virtual network where virtual machine will be created
$virtualNetworkName = 'yourVirtualNetworkName'
#Provide the name of the virtual machine
$virtualMachineName = 'yourVirtualMachineName'
#Provide the size of the virtual machine
#e.g. Standard_DS3
#Get all the vm sizes in a region using below script:
#e.g. Get-AzureRmVMSize -Location westus
$virtualMachineSize = 'Standard_DS3'
#Set the context to the subscription Id where Managed Disk will be created
Select-AzureRmSubscription -SubscriptionId $SubscriptionId
#Get the Managed Disk based on the resource group and the disk name
$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $diskName
#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
#Use the Managed Disk Resource Id to attach it to the virtual machine. Please change the OS type to linux if OS disk has linux OS
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows
#Create a public IP for the VM
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $location -AllocationMethod Dynamic
#Get the virtual network where virtual machine will be hosted
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
# Create NIC in the first subnet of the virtual network
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $location