-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertASM_to_ARM.ps1
More file actions
271 lines (228 loc) · 12.9 KB
/
ConvertASM_to_ARM.ps1
File metadata and controls
271 lines (228 loc) · 12.9 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<#
.Synopsis
This script is provided as an EXAMPLE to show how to CREATE a VM using existing OS and Data Disks. You can customize it according to your specific requirements.
.Description
The script will Create a VM based on Variables provided and using an existing OS and Data Disk,
that need to be ready BEFORE running this Script.
You can modify the script to satisfy your specific requirement but please be aware of the items specified
in the Terms of Use section.
.Terms of Use
Copyright © 2016 Microsoft Corporation. All rights reserved.
THIS CODE AND ANY ASSOCIATED 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. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#>
## Lines from 28 to 103 ====> Copy Disks
###################################################################################################################################################
###################################################################################################################################################
########################################## COPY DISKS FROM ASM TO ARM STORAGE ACCOUNT #############################################################
###################################################################################################################################################
###################################################################################################################################################
###################################################################################################################################################
# To login in Azure ARM and Select Specific Subscription where DESTINATION Storage Account exists
#Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName "Tecnologia - Produção"
##################################################################################################################################################
# To login in Azure ASM and Select Specific Subscription where SOURCE Storage Account exists
#Add-AzureAccount
Select-AzureSubscription -SubscriptionName "Tecnologia - Produção"
###################################################################################################################################################
# Define correct values for variables that will be used for collect Disk Info
"-----------------------------------------"
"Definindo Variaveis..."
" "
## Copy Disks variables
$CloudService = "azpaemrsr001"
$VMName = "azpaemrsr001"
$SourceSAName = "portalvhdszkx5wgr6jgk5p"
$SourceSAURI = "https://"+$SourceSAName+".blob.core.windows.net/vhdc214689551044e0fab0adbc2f40de9c2"
$SourceSAKey = (Get-AzureStorageKey -StorageAccountName $SourceSAName).Secondary
$SAName = "satvgprodmig"
$DestSAURI = "https://"+$SAName+".blob.core.windows.net/vhds"
$RGStorage = "RG-Storage"
$DestSAKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $RGStorage -StorageAccountName $SAName).Value[1]
## VM Creation Variables
$location = "East US 2"
$RGVM = "RG-SharePointComercial"
$RGNetwork = "RG-Network"
$SALogName = "satvgproddiag"
$vnetName = "Vnet-Producao"
$subnetName = "FrontEnd"
$AvailSetName = "azpaemrsr001-AVS"
## Define VMSize according to ASM VM Size comparation
$vmASMSize = (Get-AzureVM -ServiceName $CloudService -Name $VMName).InstanceSize
switch ($vmASMSize) {
Standard_D2 {
$vmSize = "Standard_D2_v3"
break
}
Standard_A1_v2 {
$vmSize = "Standard_D1_v2"
break
}
Standard_D1 {
$vmSize = "Standard_D1_v2"
break
}
A5 {
$vmSize = "Standard_D3_v2"
break
}
Medium {
$vmSize = "Standard_D1_v2"
break
}
Basic_A4 {
$vmSize = "Standard_D3_v2"
break
}
ExtraLarge {
$vmSize = "Standard_D3_v2"
break
}
Large {
$vmSize = "Standard_D2_v3"
break
}
Small {
$vmSize = "Standard_D1_v2"
break
}
Basic_A3 {
$vmSize = "Standard_D2_v3"
break
}
default {
$vmSize = "Standard_D1_v2"
break
}
}
$NICName = $vmName+"-NIC1"
#$PIPName = $vmName+"-PIP"
#$StaticIP = "172.16.0.204"
###################################################################################################################################################
# Get Viratual Machine Properties
"-----------------------------------------"
"Coletando Informacoes da VM"
" "
$vm = Get-AzureVM -ServiceName $CloudService -Name $VMName
###################################################################################################################################################
# Get OS Disk Information
"-----------------------------------------"
"Coletando Informacoes do Disco de S.O."
" "
$osDisk = Get-AzureOSDisk -VM $vm
$osBlobName = $osDisk.MediaLink.Segments[2]
###################################################################################################################################################
# Copy VM OS Disk from an Storage Account to Another
# AzCopy MUST be installed on directory specified. If is not, change the command line above, pointing it to correct AzCopy directory
"-----------------------------------------"
"Copiando Disco de Sistema Operacional ..."
" "
$OSDiskLogFile = $VMName+"-OS_Copy.Log"
C:\AzCopy\AzCopy.exe /V:$OSDiskLogFile /Source:$SourceSAURI /Dest:$DestSAURI /SourceKey:$SourceSAKey /DestKey:$DestSAKey /Pattern:$osBlobName /SyncCopy /NC:30 /Y
#Renaming disk do vmname-OSDisk.vhd
"-----------------------------------------"
"Renomeando o Disco de SO ..."
" "
$Context = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $DestSAKey
$newDiskName=$VMName+"-OSDisk.vhd"
Start-AzureStorageBlobCopy -SrcContainer "vhds" -DestContainer "vhds" -SrcBlob $osBlobName -DestBlob $newDiskName -Context $Context -DestContext $Context
Remove-AzureStorageBlob -Container "vhds" -Context $Context -Blob $osBlobName
$osBlobName=$newDiskName
###################################################################################################################################################
# Get Data Disk Information when VM has ONLY 01 Data Disk attached
# For VMs with Multiple Data Disks, we need to collect BlobName manually (for now)
"-----------------------------------------"
"Coletando Informações sobre o(s) Disco(s) de Dados"
" "
$DataDisks = Get-AzureDataDisk -VM $vm
if ($DataDisks -eq $null){
"### VM Sem Disco de Dados ###"
}
Else{
"-----------------------------------------"
"Copiando " + $Datadisks.Count + " Disco(s) de Dados ..."
" "
foreach ($DataDisk in $DataDisks){
$DataDiskBlobName = $DataDisk.MediaLink.Segments[2]
$DataDiskLogFile = $VMName+"-DataDisks_Copy.Log"
C:\AzCopy\AzCopy.exe /V:$DataDiskLogFile /Source:$SourceSAURI /Dest:$DestSAURI /SourceKey:$SourceSAKey /DestKey:$DestSAKey /Pattern:$DataDiskBlobName /SyncCopy /NC:30 /Y
}
}
###################################################################################################################################################
###################################################################################################################################################
########################################## CREATE AN ARM VIRTUAL MACHINE ##########################################################################
###################################################################################################################################################
###################################################################################################################################################
# To set some common Variables that can be used in many others cmdlets
# To create a New Public IP (PIP) without associate it to anything
#"-----------------------------------------"
#"Criando o PIP ..."
#" "
#New-AzureRmPublicIpAddress -ResourceGroupName $RGVM -Name $PIPName -Location $location -AllocationMethod Dynamic
"-----------------------------------------"
"Criando a NIC ..."
" "
# To create a New Network Interface (NIC) associated to a specific Subnet and PIP, give it an Static Internal IP without associate it to a VM
#$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $RGNetwork -Name $vnetName
#$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
#$pip = Get-AzureRmPublicIpAddress -ResourceGroupName $RGNetwork -Name $PIPName
#New-AzureRmNetworkInterface -ResourceGroupName $RGVM -Location $location -Name $NICName -Subnet $subnet -PublicIpAddress $pip -PrivateIpAddress $StaticIP
# To create a New Network Interface (NIC) associated to a specific Subnet with Static IP and WITHOUT PIP
#$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $RGNetwork -Name $vnetName
#$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
#New-AzureRmNetworkInterface -ResourceGroupName $RGVM -Location $location -Name $NICName -Subnet $subnet -PrivateIpAddress $StaticIP
# To create a New Network Interface (NIC) associated to a specific Subnet WITHOUT Static IP and WITHOUT PIP
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $RGNetwork -Name $vnetName
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
#$pip = Get-AzureRmPublicIpAddress -ResourceGroupName $RGVM -Name $PIPName
New-AzureRmNetworkInterface -ResourceGroupName $RGVM -Location $location -Name $NICName -Subnet $subnet #-PublicIpAddress $pip
# Use JUST if the AVSET was not previously created
"-----------------------------------------"
"Criando o Availability Set ..."
" "
New-AzureRmAvailabilitySet -ResourceGroupName $RGVM -Location $location -Name $AvailSetName -PlatformFaultDomainCount 3 -PlatformUpdateDomainCount 5
###################################################################################################################################################
# To create a New Virtual Machine
# We need to create SOME/MANY variables to specify major properties,
# Then, create a new variable that will have all VM configs, and then create a new VM using that config variable
"-----------------------------------------"
"Criando a VM ..."
" "
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $RGVM -Name $NICName
$storageacc = Get-AzureRmStorageAccount -ResourceGroupName $RGStorage -Name $SAName
$avset = Get-AzureRmAvailabilitySet -ResourceGroupName $RGVM -Name $AvailSetName
$osblobPath = "vhds/"+$osBlobName
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $osblobPath
$osdiskName = $osBlobName
#$osdiskName = $vmname+"-osDisk"
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize -LicenseType "Windows_Server" -AvailabilitySetId $avset.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $osdiskName -VhdUri $osDiskUri -Windows -CreateOption attach
## Incluindo Discos de Dados
if ($DataDisks -eq $null){
"### VM Sem Disco de Dados ###"
}Else{
foreach ($DtDisk in $Datadisks){
$DataDiskblobPath = "vhds/"+$DtDisk.MediaLink.Segments[2]
$DataDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $DataDiskblobPath
$DataDiskName = $DtDisk.MediaLink.Segments[2]
$vm = Add-AzureRmVMDataDisk -VM $vm -Name $DataDiskName -VhdUri $DataDiskUri -DiskSizeInGB $DtDisk.LogicalDiskSizeInGB -Lun $dtdisk.Lun -Caching None -CreateOption Attach
}
}
$vm = Set-AzureRMVMBootDiagnostics -VM $vm -Enable -ResourceGroupName $RGStorage -StorageAccountName $SALogName
New-AzureRmVM -ResourceGroupName $RGVM -Location $location -VM $vm -Verbose
###################################################################################################################################################
###################################################################################################################################################
########################################## MIGRATING VM DISKS TO MANAGED DISKS ####################################################################
###################################################################################################################################################
###################################################################################################################################################
#"-----------------------------------------"
#"Convertendo os Discos em a VM ..."
#" "
#Stop-AzureRmVM -ResourceGroupName $RGVM -Name $VMName -Force
#Update-AzureRmAvailabilitySet -AvailabilitySet $avSet -Sku Aligned
#ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $RGVM -VMName $VMName
#Start-AzureRmVM -ResourceGroupName $RGVM -Name $VMName