-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathModify-Azure-VM-Backup-to-all-disks.ps1
106 lines (68 loc) · 4.8 KB
/
Modify-Azure-VM-Backup-to-all-disks.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
<#
.SYNOPSIS
A script used to modify the protection of an Azure IaaS VM backup of a specific VM to backup all disks.
.DESCRIPTION
A script used to modify the protection of an Azure IaaS VM backup of a specific VM to backup all disks.
The script will do all of the following:
Remove the breaking change warning messages.
Modify protection to backup all disks.
.NOTES
Filename: Modify-Azure-VM-Backup-all-disks.ps1
Created: 15/03/2023
Last modified: 15/03/2023
Author: Wim Matthyssen
Version: 1.0
PowerShell: Azure PowerShell
Requires: PowerShell Az (v9.3.0)
Action: Change variables as needed to fit your needs.
Disclaimer: This script is provided "as is" with no warranties.
.EXAMPLE
Connect-AzAccount
Get-AzTenant (if not using the default tenant)
Set-AzContext -tenantID "xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx" (if not using the default tenant)
Set-AzContext -Subscription "<SubscriptionName>" (set to subscription holding the Recovery Services vault and the Azure VM)
.\Modify-Azure-VM-Backup-all-disks <"your VM name here">
-> Modify-Azure-VM-Backup-all-disks swpdc004
.LINK
https://wmatthyssen.com/2023/03/17/azure-spring-clean-2023-lets-optimize-your-azure-backup-costsazure-spring-clean-2023/
#>
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Parameters
param(
# $vmName -> Name of the target Azure Windows VM
[parameter(Mandatory =$true)][ValidateNotNullOrEmpty()] [string] $vmName
)
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$rgNameBackup = #<your Recovery Services vault resource group name here> The name of the resource group in which the new vault will be created. Example: "rg-hub-myh-backup-01"
$vaultName = #<your Recovery Services vault name here> The name for the Recovery Services vault here. Example: "rsv-hub-myh-we-01"
$workloadType = "AzureVM"
$policyName = #<your Backup policy name here> The name of your Backup policy here. Example: "pol-1100-pm-2ir-sun-27d-54w-12m-5y"
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Green"
$foregroundColor2 = "Yellow"
$foregroundColor3 = "Red"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Remove the breaking change warning messages
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script started
Write-Host ($writeEmptyLine + "# Script started. Without errors, it can take up to 1 minute to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Modify protection to backup all disks
$vault = Get-AzRecoveryServicesVault -ResourceGroupName $rgNameBackup -Name $vaultName
Set-AzRecoveryServicesVaultContext -Vault $vault
$container = Get-AzRecoveryServicesBackupContainer -ContainerType $workloadType -FriendlyName $vmName -VaultId $vault.ID
$policy = Get-AzRecoveryServicesBackupProtectionPolicy -Name $policyName
$backupItem = Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType $workloadType -VaultId $vault.ID
Enable-AzRecoveryServicesBackupProtection -Item $backupItem -ResetExclusionSettings -VaultId $vault.ID -Policy $policy | Out-Null
Write-Host ($writeEmptyLine + "# Protection VM $vmName set to backup all disks" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script completed
Write-Host ($writeEmptyLine + "# Script completed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------