-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathArchive-AzureRMvm.ps1
430 lines (344 loc) · 18.2 KB
/
Archive-AzureRMvm.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<#
.SYNOPSIS
Archives or Rehydrates Azure V2 (ARM) Virtual Machines from specified resource group to save VM core allotment
Requires AzureRM module version 4.2.1 or later
.DESCRIPTION
Removes VMs from a subscription leaving the VHDs, NICs and other assets along with a JSON configuration file that can
be used later to recreate the environment using the -Rehydrate switch
.EXAMPLE
.\Archive-AzureRMvm.ps1 -ResourceGroupName 'CONTOSO'
Archives all VMs in the CONTOSO resource group.
.EXAMPLE
.\Archive-AzureRMvm.ps1 -ResourceGroupName 'CONTOSO' -Rehydrate
Rehydrates the VMs using the saved configuration and remaining resource group components (VNet, NIC, NSG, AvSet etc...
.PARAMETER -ResourceGroupName [string]
Name of resource group being copied
.PARAMETER -Rehydrate[switch]
Rebuilds VMs from configuration file
.PARAMETER -OptionalEnvironment [string]
Name of the Environment. e.g. AzureUSGovernment, AzureGermanCloud or AzureChinaCloud. Defaults to AzureCloud.
.NOTES
The script attempts to restore VM extensions but some extensions may need to be reinstalled manually.
Original Author: https://github.com/JeffBow
------------------------------------------------------------------------
Copyright (C) 2017 Microsoft Corporation
You have a royalty-free right to use, modify, reproduce and distribute
this sample script (and/or any modified version) in any way
you find useful, provided that you agree that Microsoft has no warranty,
obligations or liability for any sample application or script files.
------------------------------------------------------------------------
#>
#Requires -Version 4.0
param(
[Parameter(mandatory=$True,
HelpMessage="Enter the name of the Azure Resource Group you want to target and Press <Enter> e.g. CONTOSO")]
[string]$ResourceGroupName,
[Parameter(mandatory=$False,
HelpMessage="Use this switch to rebuild the script after waiting for the blob copy to complete")]
[switch]$Rehydrate,
[Parameter(mandatory=$True,
HelpMessage="Press <Enter> to default to AzureCloud or enter the Azure Environment name of the subscription. e.g. AzureUSGovernment")]
[AllowEmptyString()]
[string]$OptionalEnvironment
)
$ProgressPreference = 'SilentlyContinue'
import-module AzureRM
if ((Get-Module AzureRM).Version -lt "4.2.1") {
Write-warning "Old version of Azure PowerShell module $((Get-Module AzureRM).Version.ToString()) detected. Minimum of 4.2.1 required. Run Update-Module AzureRM"
BREAK
}
<###############################
Get Storage Context function
################################>
function Get-StorageObject
{ param($resourceGroupName, $srcURI)
$split = $srcURI.Split('/')
$strgDNS = $split[2]
$splitDNS = $strgDNS.Split('.')
$storageAccountName = $splitDNS[0]
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $StorageAccountName).Value[0]
$StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
return $StorageContext
} # end of Get-StorageObject function
<###############################
New-VM function
param $vmobj takes Microsoft.Azure.Commands.Compute.Models.PSVirtualMachineList object
or custom PS object that was hydrated from a JSON export of the VM configuration
################################>
function New-VM
{ param($vmObj)
$created = $false
# get source VM attributes
$VMName = $vmObj.Name
$location = $vmObj.location
$VMSize = $vmObj.HardwareProfile.VMSize
$OSDiskName = $vmObj.StorageProfile.OsDisk.Name
$OSType = $vmObj.storageprofile.osdisk.OsType
$OSDiskCaching = $vmObj.StorageProfile.OsDisk.Caching
$CreateOption = "Attach"
if($vmObj.AvailabilitySetReference)
{
$avSetRef = ($vmObj.AvailabilitySetReference.id).Split('/')
$avSetName = $avSetRef[($avSetRef.count -1)]
$AvailabilitySet = Get-AzureRmAvailabilitySet -ResourceGroupName $ResourceGroupName -Name $avSetName
}
# get storage account context from $vmObj.storageprofile.osdisk.vhd.uri
$OSDiskUri = $null
$OSDiskUri = $vmObj.storageprofile.osdisk.vhd.uri
$OSsplit = $OSDiskUri.Split('/')
$OSblobName = $OSsplit[($OSsplit.count -1)]
$OScontainerName = $OSsplit[3]
$OSstorageContext = Get-StorageObject -resourceGroupName $resourceGroupName -srcURI $OSDiskUri
# get the Network Interface Card we created previously based on the original source name
$NICRef = ($vmObj.NetworkInterfaceIDs).Split('/')
$NICName = $NICRef[($NICRef.count -1)]
$NIC = Get-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName
#create VM config
if($AvailabilitySet)
{
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize -AvailabilitySetID $AvailabilitySet.Id -wa SilentlyContinue
}
else
{
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize -wa SilentlyContinue
}
# Set OS Disk based on OS type
if($OStype -eq 'Windows' -or $OStype -eq '0'){
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -Caching $OSDiskCaching -CreateOption $createOption -Windows
}
else
{
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -Caching $OSDiskCaching -CreateOption $createOption -Linux
}
# add NIC
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
# readd data disk if they were present
if($vmObj.storageProfile.datadisks)
{
foreach($disk in $vmObj.storageProfile.DataDisks)
{
$dataDiskName = $null
$dataDiskUri = $null
$diskBlobName = $null
$dataDiskName = $disk.Name
$dataDiskLUN = $disk.Lun
$diskCaching = $disk.Caching
$DiskSizeGB = $disk.DiskSizeGB
$dataDiskUri = $disk.vhd.uri
$split = $dataDiskUri.Split('/')
$diskBlobName = $split[($split.count -1)]
$diskContainerName = $split[3]
$diskStorageContext = Get-StorageObject -resourceGroupName $resourceGroupName -srcURI $dataDiskUri
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $dataDiskName -DiskSizeInGB $DiskSizeGB -Lun $dataDiskLUN -VhdUri $dataDiskUri -Caching $diskCaching -CreateOption $CreateOption | out-null
}
}
# create the VM from the config
try
{
write-verbose "Rehydrating Virtual Machine $VMName in resource group $resourceGroupName at location $location" -verbose
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $location -VM $VirtualMachine -ea Stop -wa SilentlyContinue | out-null
$created = $true
write-host "Successfully rehydrated Virtual Machine $VMName"
}
catch
{
$_
write-warning "Failed to create Virtual Machine $VMName"
$created = $false
}
if($created)
{
try
{
$newVM = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $VMName
if($vmObj.DiagnosticsProfile.BootDiagnostics.Enabled -eq 'True')
{
write-verbose "Adding Boot Diagnostics to virtual machine $VMName" -Verbose
$storageURI = $vmObj.DiagnosticsProfile.BootDiagnostics.StorageUri
$StgSplit = $storageUri.Split('/')
$DiagStorageAccountName = $StgSplit[2].Split('.')[0]
$newVM | Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroupName -StorageAccountName $DiagStorageAccountName | Out-Null
}
else
{
write-verbose "Disabling Boot Diagnostics on virtual machine $VMName" -Verbose
$newVM | Set-AzureRmVMBootDiagnostics -Disable | Out-Null
}
$newVM | Update-AzureRMVm -ResourceGroupName $resourceGroupName | Out-Null
}
catch{}
}
return $created
} # end of New-VM function
# Verify specified Environment
if($OptionalEnvironment -and (Get-AzureRMEnvironment -Name $OptionalEnvironment) -eq $null)
{
write-warning "The specified -OptionalSourceEnvironment could not be found. Select one of these valid environments."
$OptionalEnvironment = (Get-AzureRMEnvironment | Select-Object Name, ManagementPortalUrl | Out-GridView -title "Select a valid Azure environment for your source subscription" -OutputMode Single).Name
}
# get Azure creds for source
write-host "Enter credentials for the Azure Subscription..." -f Yellow
if($OptionalEnvironment)
{
$login= Connect-AzureRmAccount -EnvironmentName $OptionalEnvironment
}
else
{
$login= Connect-AzureRmAccount
}
$loginID = $login.context.account.id
$sub = Get-AzureRmSubscription
$SubscriptionId = $sub.Id
# check for multiple subs under same account and force user to pick one
if($sub.count -gt 1)
{
$SubscriptionId = (Get-AzureRmSubscription | Select-Object * | Out-GridView -title "Select Target Subscription" -OutputMode Single).Id
Select-AzureRmSubscription -SubscriptionId $SubscriptionId | Out-Null
$sub = Get-AzureRmSubscription -SubscriptionId $SubscriptionId
}
# check for valid sub
if(! $SubscriptionId)
{
write-warning "The provided credentials failed to authenticate or are not associcated to a valid subscription. Exiting the script."
break
}
$SubscriptionName = $sub.Name
write-host "Logged into $SubscriptionName with subscriptionID $SubscriptionId as $loginID" -f Green
# check for valid source resource group
if(-not ($sourceResourceGroup = Get-AzureRmResourceGroup -ResourceGroupName $resourceGroupName))
{
write-warning "The provided resource group $resourceGroupName could not be found. Exiting the script."
break
}
# check for valid source resource group
if(-not ($sourceResourceGroup = Get-AzureRmResourceGroup -ResourceGroupName $resourceGroupName))
{
write-warning "The provided resource group $resourceGroupName could not be found. Exiting the script."
break
}
if($Rehydrate)
{
# search all storage accounts and containers for rehydrate files
foreach($StorageAccount in (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName))
{
$StorageAccountName = $StorageAccount.StorageAccountName
write-verbose "Searching for rehydrate files in storage account $StorageAccountName)..." -verbose
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $StorageAccountName).Value[0]
$StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
foreach($StorageContainer in (Get-AzureStorageContainer -Context $StorageContext) )
{
$StorageContainerName = $StorageContainer.Name
write-verbose "Searching for rehydrate files in container $StorageContainerName)..." -verbose
$rehydrateBlobs = Get-AzureStorageBlob -Container $StorageContainerName -Context $StorageContext | Where-Object{$_.name -like "*.rehydrate.json"}
foreach($rehydrateBlob in $rehydrateBlobs)
{
write-verbose "Retreiving rehydrate file $($rehydrateBlob.name)..." -verbose
try
{
$TempRehydratefile = Get-AzureStorageBlobContent -CloudBlob $RehydrateBlob.iCloudBlob -Context $StorageContext -Destination $env:temp -Force -ea Stop
$TempRehydratefileName = $TempRehydratefile.Name
$fileContent = get-content "$env:temp\$TempRehydratefileName" -ea Stop
$fileContent | Where-Object{$_ -ne ''} | out-file "$env:temp\$TempRehydratefileName"
$rehydrateVM = (get-content "$env:temp\$TempRehydratefileName" -ea Stop) -Join "`n"| ConvertFrom-Json -ea Stop
}
catch
{
write-warning "Virtual machine object data could not be restored from rehydrate file $($rehydrateBlob.Name) "
}
if($rehydrateVM)
{
$created = New-VM -vmObj $rehydrateVM
if($created)
{
try
{
write-verbose "Searching for Diagnostics config file in container $StorageContainerName..." -verbose
$rehydrateDiagBlob = Get-AzureStorageBlob -Container $StorageContainerName -Context $StorageContext -ea Stop | Where-Object{$_.name -like "*$($rehydrateVM.Name).rehydratediag.xml"}
if($rehydrateDiagBlob)
{
write-verbose "Retreiving Diagnostics config file $($rehydrateDiagBlob.name)..." -verbose
$TempDiagRehydratefile = Get-AzureStorageBlobContent -CloudBlob $RehydrateDiagBlob.iCloudBlob -Context $StorageContext -Destination $env:temp -Force -ea Stop
$TempDiagRehydratefileName = $TempDiagRehydratefile.Name
$DiagfileContent = get-content "$env:temp\$TempDiagRehydratefileName" -ea Stop
$DiagfileContent | Where-Object{$_ -ne ''} | out-file "$env:temp\$TempDiagRehydratefileName"
$rehydrateVMDiag = (get-content "$env:temp\$TempDiagRehydratefileName" -ea Stop) -Join "`n"| ConvertFrom-Json
$wadCfg = $rehydrateVMDiag.wadCfg
$wadStorageAccount = $rehydrateVMDiag.StorageAccount
$wadStorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $wadStorageAccount -ea Stop).Value[0]
write-verbose "Applying VM Diagnostic settings to virtual machine $($rehydrateVM.Name)..." -verbose
Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $ResourceGroupName -VMName $($rehydrateVM.Name) -DiagnosticsConfigurationPath "$env:temp\$TempDiagRehydratefileName" -StorageAccountName $wadStorageAccount -StorageAccountKey $wadStorageAccountKey | out-null
}
else
{
write-verbose "No Diagnostics config file found for $($rehydrateVM.Name)..." -verbose
}
}
catch{}
}
}
}
}
}
} # end of if rehydrate
else
{
# get configuration details for all VMs
[string] $location = $sourceResourceGroup.location
$resourceGroupVMs = Get-AzureRMVM -ResourceGroupName $resourceGroupName
if(! $resourceGroupVMs){write-warning "No virtual machines found in resource group $resourceGroupName"; break}
$resourceGroupVMs | %{
$status = ((get-azurermvm -ResourceGroupName $resourceGroupName -Name $_.name -status).Statuses|where{$_.Code -like 'PowerState*'}).DisplayStatus
write-output "$($_.name) status is $status"
if($status -eq 'VM running')
{write-warning "All virtual machines in this resource group are not stopped. Please stop all VMs and try again"; break}
}
# remove each VM - leaving VHD for archive and possible rehydration
foreach($srcVM in $resourceGroupVMs)
{
# get source VM OS disk attributes to determine storage account and container to copy config file
$vmName = $srcVM.Name
$OSDiskUri = $null
$OSDiskUri = $srcVM.storageprofile.osdisk.vhd.uri
$OSsplit = $OSDiskUri.Split('/')
$OScontainerName = $OSsplit[3]
$StorageContext = Get-StorageObject -resourceGroupName $resourceGroupName -srcURI $OSDiskUri
$diagSettings = (Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $ResourceGroupName -VMName $vmName).PublicSettings
if($diagSettings)
{
$RehydrateDiagFile = ("$ResourceGroupName.$vmName.rehydrateDiag.xml").ToLower()
$tempDiagFilePath = "$env:TEMP\$RehydrateDiagFile"
$diagSettings | Out-File -FilePath $tempDiagFilePath -Force
# expand file size to 20KB for Page blob write if we experience Premium_LRS storage
$file = [System.IO.File]::OpenWrite($tempDiagFilePath)
$file.SetLength(40960)
$file.Close()
# copy to cloud container as Page blog
$copyDiagResult = Set-AzureStorageBlobContent -File $tempDiagFilePath -Blob $RehydrateDiagFile -Context $StorageContext -Container $OScontainerName -BlobType Page -Force
}
#save off VM config to temp drive before copying it back to cloud storage
$RehydrateFile = ("$ResourceGroupName.$vmName.rehydrate.json").ToLower()
$tempFilePath = "$env:TEMP\$RehydrateFile"
$srcVM | ConvertTo-Json -depth 10 | Out-File -FilePath $tempFilePath -Force
# expand file size to 20KB for Page blob write if we experience Premium_LRS storage
$file = [System.IO.File]::OpenWrite($tempFilePath)
$file.SetLength(20480)
$file.Close()
# copy to cloud container as Page blog
$copyResult = Set-AzureStorageBlobContent -File $tempFilePath -Blob $RehydrateFile -Context $StorageContext -Container $OScontainerName -BlobType Page -Force
if($copyResult)
{
# remove VM
write-verbose "Archiving Virtual Machine $vmName..." -verbose
try
{
Remove-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName -Force -ea Stop | out-null
write-output "Archived $vmName"
}
catch
{
$_
Write-Warning "Failed to remove Virtual Machine $vmName"
}
}
}
}