Proposed Pull Request Change

title description ms.topic ms.date author ms.author
Script Sample - Create a new or modify the current system state backup policy Learn about how to use a script to create a new or modify the current system state backup policy. sample 03/27/2025 AbhishekMallick-MS v-mallicka
📄 Document Links
GitHub View on GitHub Microsoft Learn View on Microsoft Learn
Content Truncation Detected
The generated rewrite appears to be incomplete.
Original lines: -
Output lines: -
Ratio: -
Raw New Markdown
Generating updated version of doc...
Rendered New Markdown
Generating updated version of doc...
+0 -0
+0 -0
--- title: Script Sample - Create a new or modify the current system state backup policy description: Learn about how to use a script to create a new or modify the current system state backup policy. ms.topic: sample ms.date: 03/27/2025 author: AbhishekMallick-MS ms.author: v-mallicka # Customer intent: "As an IT administrator, I want to create or modify the system state backup policy using a script, so that I can ensure the server protected by the MARS agent has an up-to-date and efficient backup schedule." --- # PowerShell Script to create a new or modify the current system state backup policy This script helps you to create new backup policy or modify the current system state backup policy set for the server protected by MARS agent. ## Sample script ```azurepowershell <# .SYNOPSIS Modify system state policy .DESCRIPTION Modify system state policy .ROLE Administrators #> param ( [Parameter(Mandatory = $true)] [string[]] $daysOfWeek, [Parameter(Mandatory = $true)] [string[]] $timesOfDay, [Parameter(Mandatory = $true)] [int] $weeklyFrequency, [Parameter(Mandatory = $false)] [int] $retentionDays, [Parameter(Mandatory = $false)] [Boolean] $retentionWeeklyPolicy, [Parameter(Mandatory = $false)] [int] $retentionWeeks, [Parameter(Mandatory = $false)] [Boolean] $retentionMonthlyPolicy, [Parameter(Mandatory = $false)] [int] $retentionMonths ) Set-StrictMode -Version 5.0 $env:PSModulePath = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' -Name PSModulePath).PSModulePath Import-Module MSOnlineBackup $ErrorActionPreference = "Stop" Try { $oldPolicy = Get-OBSystemStatePolicy if ($oldPolicy) { return } $policy = New-OBPolicy $policy = Add-OBSystemState -Policy $policy $timesOfDaySchedule = @() foreach ($time in $timesOfDay) { $timesOfDaySchedule += ([TimeSpan]$time) } $daysOfWeekSchedule = @() foreach ($day in $daysOfWeek) { $daysOfWeekSchedule += ([System.DayOfWeek]$day) } $schedule = New-OBSchedule -DaysOfWeek $daysOfWeekSchedule -TimesOfDay $timesOfDaySchedule -WeeklyFrequency $weeklyFrequency if ($daysOfWeekSchedule.Count -eq 7) { if ($retentionWeeklyPolicy -and $retentionMonthlyPolicy) { $retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths } elseif ($retentionWeeklyPolicy) { $retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks } elseif ($retentionMonthlyPolicy) { $retention = New-OBRetentionPolicy -RetentionDays $retentionDays -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths } else { $retention = New-OBRetentionPolicy -RetentionDays $retentionDays } } else { if ($retentionWeeklyPolicy -and $retentionMonthlyPolicy) { $retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths } elseif ($retentionWeeklyPolicy) { $retention = New-OBRetentionPolicy -RetentionWeeklyPolicy:$true -WeekDaysOfWeek $daysOfWeekSchedule -WeekTimesOfDay $timesOfDaySchedule -RetentionWeeks $retentionWeeks } elseif ($retentionMonthlyPolicy) { $retention = New-OBRetentionPolicy -RetentionMonthlyPolicy:$true -MonthDaysOfWeek $daysOfWeekSchedule -MonthTimesOfDay $timesOfDaySchedule -RetentionMonths $retentionMonths } } Set-OBSchedule -Policy $policy -Schedule $schedule Set-OBRetentionPolicy -Policy $policy -RetentionPolicy $retention Set-OBSystemStatePolicy -Policy $policy -Confirm:$false } Catch { if ($error[0].ErrorDetails) { throw $error[0].ErrorDetails } throw $error[0] } ``` ## Execute the script To execute the script, follow these steps: 1. Save the above script on your machine with a name of your choice and .ps1 extension. 1. Execute the script by providing the following parameters: <br> Schedule of backup and number of days/weeks/months or years that the backup needs to be retained. ## Next steps [Use PowerShell to deploy and manage on-premises backups using MARS agent](../backup-client-automation.md).
Success! Branch created successfully. Create Pull Request on GitHub
Error: