-
Notifications
You must be signed in to change notification settings - Fork 7
/
copy-managed-disk-image.ps1
103 lines (69 loc) · 4.07 KB
/
copy-managed-disk-image.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Param(
[string] $sourceSubscriptionId,
[string] $destinationSubscriptionId,
[string] $resourceGroupName,
[string] $imageName,
[string] $sourceRegion,
[string] $destinationRegion,
[string] $vmName,
[string] $servicePrincipalName,
[securestring] $servicePrincipalPassword
)
$snapshotName = $imageName + $sourceRegion + "-snap"
# ----- 5. Create a snapshot of the OS (and optionally data disks) from the generalized VM -----
# TODO - SUPPORT DATA DISKS
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $sourceRegion
New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -Snapshot $snapshot -SnapshotName $snapshotName
# ----- 6. Copy the snapshot to the second subscription -----
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId
$snapshotConfig = New-AzureRmSnapshotConfig -OsType Windows `
-Location $region `
-CreateOption Copy `
-SourceResourceId $snap.Id
$snap = New-AzureRmSnapshot -ResourceGroupName $resourceGroupName `
-SnapshotName $snapshotName `
-Snapshot $snapshotConfig
# ----- 7. In the second subscription, create a new Image from the copied snapshot -----
Select-AzureRmSubscription -SubscriptionId $destinationSubscriptionId
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$imageConfig = New-AzureRmImageConfig -Location $destinationRegion
Set-AzureRmImageOsDisk -Image $imageConfig `
-OsType Windows `
-OsState Generalized `
-SnapshotId $snap.Id
New-AzureRmImage -ResourceGroupName $resourceGroupName `
-ImageName $imageName `
-Image $imageConfig
(Get-AzureRmImage -ResourceGroupName $resourceGroupName) | Select-Object Name, Location, ProvisioningState
# ----- 8. In the second subscription, create a new VM from the new Image. -----
$currentDate = Get-Date -Format yyyyMMdd.HHmmss
$deploymentLabel = "vmimage-$currentDate"
$image = Get-AzureRmImage -ResourceGroupName $resourceGroupName -ImageName $imageName
$dnsPrefix = "myvm-" + -join ((97..122) | Get-Random -Count 7 | ForEach-Object {[char]$_})
$creds = Get-Credential -Message "Enter username and password for new VM."
$templateParams = @{
vmName = $vmName;
adminUserName = $creds.UserName;
adminPassword = $creds.Password;
dnsLabelPrefix = $dnsPrefix
managedImageResourceId = $image.Id
}
# Put the dummy VM in a separate resource group as it makes it super easy to clean up all the extra stuff that goes with a VM (NIC, IP, VNet, etc.)
$rgNameTemp = $resourceGroupName + "-temp"
New-AzureRmResourceGroup -Location $region `
-Name $rgNameTemp
New-AzureRmResourceGroupDeployment -Name $deploymentLabel `
-ResourceGroupName $rgNameTemp `
-TemplateParameterObject $templateParams `
-TemplateUri 'https://raw.githubusercontent.com/mcollier/copy-azure-managed-disk-images/master/azuredeploy.json' `
-Verbose
# ----- 9. Delete the snapshot in the second subscription -----
Remove-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -Force
# ----- 10. Delete the VM created in step 8. -----
Remove-AzureRmResourceGroup -Name $rgNameTemp -Force
# ----- 11. Switch back to the source (original) subscription and delete the original snapshot. -----
Select-AzureRmSubscription -SubscriptionId $sourceSubscriptionId
Remove-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName