-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathARM - Managed Disk - Detach Data Disk to Virtual Machine (VM).ps1
182 lines (94 loc) · 3.66 KB
/
ARM - Managed Disk - Detach Data Disk to Virtual Machine (VM).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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
## To Set Verbose output
$PSDefaultParameterValues['*:Verbose'] = $true
# Variables - Common
$location = "eastus2"
$tags = New-Object 'System.Collections.Generic.Dictionary[String,object]'
$tags.Add("environment", "Production") # Production, Staging, QA
$tags.Add("projectName", "Demo Project")
$tags.Add("projectVersion", "1.0.0")
$tags.Add("managedBy", "developer.aashishpatel@gmail.com")
$tags.Add("billTo", "Ashish Patel")
$tags.Add("tier", "Front End") # Front End, Back End, Data
$tags.Add("dataProfile", "Public") # Public, Confidential, Restricted, Internal
$vmShortName = "Test"
$vmSuffix = "VM"
$vmName = "${vmShortName}${vmSuffix}"
$rgShortName = "qweasdzxc"
$rgSuffix = "-rg"
$rgName = "${rgShortName}${rgSuffix}"
$diskShortName = "TEST1"
$diskSuffix = "-DataDisk"
$diskName = "${diskShortName}${diskSuffix}"
<# Data Disk (Managed) #>
<#
Virtual Machine (VM)
Data Disk
#>
# Variables - Disk (Managed)
# logical unit number
$lun = 1
<# Create Disk (Managed), if it does not exist #>
Get-AzureRmDisk -Name $diskName -ResourceGroupName $rgName -ErrorVariable isDiskExist -ErrorAction SilentlyContinue `
If ($isDiskExist)
{
Write-Output "Disk (Managed) does not exist"
Write-Verbose "Fetching Virtul Machine: {$vmName}"
$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
Write-Verbose "Fetching Data Disk: {$diskName}"
$dataDisk = Get-AzureRmDisk -Name $diskName -ResourceGroupName $rgName
# attach data disk to virtual machine
Write-Verbose "Add the data disk {$diskName} to the virtual machine: {$vmName}"
$vm = Add-AzureRmVMDataDisk `
-VM $vm `
-Name $diskName `
-ManagedDiskId $dataDisk.Id `
-CreateOption Attach `
-Lun $lun
Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
}
Else
{
Write-Output "Data Disk is attached to Virtul Machine"
Write-Verbose "Fetching Disk (Managed): {$diskName}"
$dataDisk = Get-AzureRmDisk -Name $diskName -ResourceGroupName $rgName
}
Write-Verbose "Get list of Disks (Managed)"
Write-Output "Disks (Managed)"
Get-AzureRmDisk -ResourceGroupName $rgName `
| Select-Object Name, ResourceGroupName, Location `
| Format-Table -AutoSize -Wrap
<#
Get-AzureRmDisk `
| Select-Object Name, ResourceGroupName, Location `
| Format-Table -AutoSize -Wrap -GroupBy ResourceGroupName
#>
<#
# Prepare data disks in VM
# Create an RDP connection with the virtual machine. Open up PowerShell and run this script.
Get-Disk | Where partitionstyle -eq 'raw' | `
Initialize-Disk -PartitionStyle MBR -PassThru | `
New-Partition -AssignDriveLetter -UseMaximumSize | `
Format-Volume -FileSystem NTFS -NewFileSystemLabel $diskName -Confirm:$false
#>
<#
$vmShortName = "Test"
$vmSuffix = "VM"
$vmName = "${vmShortName}${vmSuffix}"
$rgShortName = "qweasdzxc"
$rgSuffix = "-rg"
$rgName = "${rgShortName}${rgSuffix}"
$diskShortName = "TEST1"
$diskSuffix = "-DataDisk"
$diskName = "${diskShortName}${diskSuffix}"
Write-Verbose "Detach Data Disk from Virtul Machine: {$diskName}"
Write-Verbose "Fetching Virtul Machine: {$vmName}"
$vm = Get-AzureRmVM -Name $vmName -ResourceGroupName $rgName
Remove-AzureRmVMDataDisk -VM $vm -Name $diskName
Update-AzureRmVM -VM $vm -ResourceGroupName $rgName
#>
<#
# References
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-manage-data-disk
# detach
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/detach-disk
#>