Ondemand Azure VM backup scheduling using powershell

Azure-learning 56 Reputation points
2022-09-23T14:14:23.72+00:00

1 - Can we schedule on demand Azure VM backup on a specific Time (using powershell). Like If i want to take the backup of VM every 3rd week of a month. I want on demand backup because I dont want to modify any existing policy attached to VM . Is it possible to schedule on demand backup on every 3rd week of a month .

2 - also below is the backup policy every week on Friday .
Dt = Get-Date
$dtinSchedulerFormat = Get-Date -Year $Dt.Year -Month $Dt.Month -Day $Dt.Day -Hour $Dt.Hour -Minute 0 -Second 0 -Millisecond 0
$dtinUTC = $dtinSchedulerFormat.ToUniversalTime()
$scheduledPolicy.ScheduleRunTimes[0] = $dtinUTC
$retPol = Get-AzRecoveryServicesBackupRetentionPolicyObject -WorkloadType "AzureVM"
$scheduledPolicy.ScheduleRunFrequency.Clear
$scheduledPolicy.ScheduleRunDays.Remove("Sunday")
$scheduledPolicy.ScheduleRunDays.Add("Friday")
$scheduledPolicy.ScheduleRunFrequency="Weekly"
$retPol.IsWeeklyScheduleEnabled = $true
$retPol.IsDailyScheduleEnabled=$false
$retPol.WeeklySchedule.DurationCountInWeeks = 1
$RetPol.WeeklySchedule.DaysOfTheWeek.Remove("Sunday")
$RetPol.WeeklySchedule.DaysOfTheWeek.Add("Friday")
$retPol.IsMonthlyScheduleEnabled = $false
$retPol.IsYearlyScheduleEnabled = $false
New-AzRecoveryServicesBackupProtectionPolicy -Name "Policy1" -WorkloadType "AzureVM" -RetentionPolicy $retPol -SchedulePolicy $scheduledPolicy -VaultId $vaultid

I need to update the policy which will take VM backup on every 3rd week of a Month ,starting from 9/23/2022 . Can you please help me how to update using powershell.

Azure Backup
Azure Backup
An Azure backup service that provides built-in management at scale.
1,117 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,081 questions
Windows Server Backup
Windows Server Backup
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Backup: A duplicate copy of a program, a disk, or data, made either for archiving purposes or for safeguarding valuable files from loss should the active copy be damaged or destroyed.
450 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,354 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Carlos Villagomez 1,031 Reputation points Microsoft Employee
    2022-09-27T17:03:52.777+00:00

    Hello @NGaur-3476,

    Thanks for your post! I was reviewing this post with one of my colleagues and first wanted to clarify that scheduling on-demand backups via PowerShell is currently unsupported. However, if you decide to proceed with using a PS script rather than the Portal (which will of course affect all VM's associated with that policy), then you would need to get the existing policies and pull the one to update and then modify it accordingly. In the case of the script you have shared, it appears to be creating a new policy in which case if you get the policy and then update it, it will then use the new updated information regarding wanting to take the VM backup every 3rd week of the month beginning on 9/23/22. If you wanted to do this outside of the policies, then you would in fact have to make the script simply create the "on-demand" backup and then run the script as the backups are needed.

    In the case of your script, the New-AzRecoveryServicesBackupProtectionPolicy will be used to create the new policy to be applied for your VM. Please let us know if you require any further assistance and we'll be happy to assist you further.

    Thanks!
    Carlos V.

    0 comments No comments