-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattach-MDSnapshot.ps1
139 lines (112 loc) · 3.97 KB
/
attach-MDSnapshot.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<#
.NOTES
Daniel Hibbert - March 2020
Version 0.1
Microsoft Azure - attach-snapshotMD.ps1
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
.DESCRIPTION
This script will automate the process of creating a managed disk from a snapshot
and attaching the new disk to an existing virtual machine.
.PARAMETER sourceVM
[string] Source resource group name where snapshots are stored
.PARAMETER targetVmName
[string] Target VM to attach new managed disk
.PARAMETER targetVmResourceGroupName
[string] Target VM Resource Group Name
.EXAMPLE
attach-snashotMD.ps1 -targetVM VM01 -snapshotRGName rg-snapshotStorage
#>
param (
[parameter(position=1, mandatory=$true)][string]$snapshotRGName,
[parameter(position=2, Mandatory=$true)][string]$targetVmName,
[parameter(position=3, Mandatory=$false)][string]$targetVmResourceGroupName
)
# Query for targetVM configration. [Optional] include VM resource group name
if($targetVmResourceGroupName){
$vmConfig = Get-AzVM `
-name $targetVmName `
-ResourceGroupName $targetVmResourceGroupName
}
else {
$vmConfig = Get-AzVM -name $targetVmName
}
# Check for valid VM. If not found exit script
if (!$vmConfig.Id){
Write-error -message "Unable to find target virtual machine. Check the name and try again"
exit;
}
## Query for existing SnapShots
write-host -ForegroundColor green "## Discovering Available Snapshots ##"
$availableSnapshots = Get-AzSnapshot -ResourceGroupName $snapshotRGName
# Confirm snapshots are found. If (0) exit script
if ($availableSnapshots.count -eq 0){
Write-Error -Message "No snapshots found."
exit;
}
# Generate menu to select snapshot
write-host -ForegroundColor yellow "`nMultiple snapshots found.`n"
[int]$listitr =1
$availableSnapshots | ForEach-Object {
write-host "[$listitr] - "$_.name
$listitr++
}
[string]$mdResponse = $(Read-Host -Prompt "Select snapshot")-1
$sourceSnapshot = $availableSnapshots[$mdResponse]
#### Create disk from snapshot
## Create new managed disk from snapshot
write-host -ForegroundColor green -NoNewline "## Creating Managed Disk from Snapshot"
# Create managed disk from snapshot (one for each target VM)
try {
$sourceSnapshot | foreach-object {
$mdConfig = New-AzDiskConfig `
-SkuName $_.Tags.diskSKU `
-Location $vmConfig.Location `
-CreateOption Copy `
-SourceResourceId $_.Id `
-DiskSizeGB $_.tags.diskSizeGB `
-Tag $_.Tags
$newMD = New-AzDisk `
-Disk $mdConfig `
-ResourceGroupName $vmConfig.ResourceGroupName `
-DiskName $_.Name
}
write-host -ForegroundColor Yellow "...Completed!"
}
catch {
Write-Error -Message "Managed Disk creation FAILED`n`n"
$_
exit;
}
## STEP 3: Attach data disk to target VM
try {
write-host -ForegroundColor green -NoNewline "## Attach data disk"
# Check for exiting data disk connected to desired LUN
foreach ($disk in $($vmConfig.StorageProfile.DataDisks)) {
# If exists detach data disk
if ($disk.lun -eq $sourceSnapshot.tags.LUN){
write-host -foregroundColor Yellow "`n`t!! Found existing disk at LUN $($sourceSnapshot.tags.LUN) !!"
Remove-AzVMDataDisk -VM $vmConfig -Name $disk.name
update-AzVM -ResourceGroupName $vmConfig.ResourceGroupname -VM $vmConfig | out-null
}
}
# attach snapshot disk to target VM
Add-AzVMDataDisk `
-vm $vmConfig `
-CreateOption attach `
-Lun $sourceSnapshot.tags.LUN `
-ManagedDiskId $newMD.Id `
-ErrorAction Stop
Update-AzVm `
-vm $vmConfig `
-ResourceGroupName $vmConfig.ResourceGroupName `
-ErrorAction Stop | out-null
write-host -ForegroundColor Yellow "...Completed!"
}
catch {
write-error -message "Failed to attach to VM`n`n"
$_
exit;
}