Last active
November 24, 2021 10:09
-
-
Save MrLenin/3e6b317ed101f0d8e69903937be7a63b to your computer and use it in GitHub Desktop.
Windows Server Backup Configuration script inspired by Windows Server Core's sconfig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # WSB (Windows Server Backup) | |
| ############################################################################### | |
| #### Script Prerequisites ##################################################### | |
| ############################################################################### | |
| using namespace Microsoft.Windows.ServerBackup.Commands | |
| Import-Module ServerManager | |
| Write-Host -NoNewline "`nVerifying Windows Server Backup is installed... " | |
| if (!(Get-WindowsFeature -Name Windows-Server-Backup).Installed) | |
| { | |
| Write-Host 'Not Installed.' | |
| Write-Host -NoNewline 'Installing Windows Server Backup... ' | |
| if (!(Add-WindowsFeature -Name Windows-Server-Backup).Success) | |
| { | |
| Write-Host 'Failed.' | |
| Write-Host 'Aborting script execution.' | |
| exit | |
| } | |
| else | |
| { Write-Host 'Success.' } | |
| } | |
| else | |
| { Write-Host 'Installed.' } | |
| $HaveHyperv = (Get-WindowsFeature -Name Hyper-V).Installed | |
| ############################################################################### | |
| #### Script Strings ########################################################### | |
| ############################################################################### | |
| $MainMenuTemplate = @" | |
| =============================================================================== | |
| Windows Server Backup Configuration | |
| =============================================================================== | |
| {0} | |
| 1) Daily Backup Schedule: `t`t`t`t{1} | |
| 2) Bare Metal Recovery Backup:`t`t`t`t{2} | |
| 3) System State Backup: `t`t`t`t{3} | |
| 4) Configure VSS Options: `t`t`t`t{4} | |
| 5) Configure Backup Sources | |
| 6) Configure Backup Targets | |
| 7) Configure Hyper-V Backup Sources | |
| 8) Save Backup Policy | |
| 9) Remove Backup Policy | |
| 10) Run Manual Backup | |
| 0) Exit | |
| Enter number to select an option | |
| "@ | |
| $VssOptionsMenuText = @" | |
| -------------------------------- | |
| VSS Options | |
| -------------------------------- | |
| Unless you are using another backup solution apart from Windows Server | |
| Backup, you should always select Full Backup. Incremental backups | |
| have been automatically performed and managed by Windows Server Backup | |
| since Windows Server 2008 R2 and there are no user facing settings for | |
| configuring them. | |
| 1) Full Backup | |
| 2) Copy Backup | |
| Enter number to select an option (Blank=Cancel) | |
| "@ | |
| $BackupSourcesMenuText = @" | |
| -------------------------------- | |
| Backup Sources | |
| -------------------------------- | |
| Critical volumes are automatically added to and cannot be removed from a | |
| backup policy that has Bare Metal Recovery enabled. | |
| Backup Sources: | |
| {0} | |
| Valid Sources: | |
| {1} | |
| 1) Add Backup Source | |
| 2) Remove Backup Source | |
| 0) Return to Main Menu | |
| Enter number to select an option | |
| "@ | |
| $BackupTargetsMenuText = @" | |
| -------------------------------- | |
| Backup Targets | |
| -------------------------------- | |
| The backup policy can only contain backup targets of a single target type. | |
| It can contain multiple volume or disk backup targets, but can only contain | |
| a single network backup target. | |
| It is recommended to only use volume or network backup targets. New disk backup | |
| targets are reinitialised and formatted when the backup policy is saved. Disk | |
| type backup targets are converted to volume type by this script when an existing | |
| backup policy is loaded to work around a bug which prevents saving a backup policy | |
| that includes existing backup targets of this type. | |
| Backup Targets: | |
| {0} | |
| Valid Volume Targets: | |
| {1} | |
| Valid Disk Targets: | |
| {2} | |
| 1) Add Volume Backup Target | |
| 2) Add Disk Backup Target | |
| 3) Set Backup Target to Network Path | |
| 4) Remove Backup Target | |
| 0) Return to Main Menu | |
| Enter number to select an option | |
| "@ | |
| $HypervBackupSourcesMenuText = @" | |
| -------------------------------- | |
| Hyper-V Backup Sources | |
| -------------------------------- | |
| Backup Sources: | |
| {0} | |
| Valid Sources: | |
| {1} | |
| 1) Add Backup Source | |
| 2) Remove Backup Source | |
| 0) Return to Main Menu | |
| Enter number to select an option | |
| "@ | |
| $NewPolicyText = @" | |
| A new policy has been created for you to edit. The backup policy cannot | |
| be saved until the backup schedule and at least one backup source and one | |
| backup target are configured. | |
| "@ | |
| $ExistingPolicyText = @" | |
| The existing backup policy has been copied into a new policy for editing. | |
| Any existing disk type backup targets are converted to volume type to work | |
| around a bug that prevents saving existing disk type backup targets. | |
| "@ | |
| $RemovingCriticalVolumesText = "`nRemoving Critical Volumes when the Bare Metal Recovery option is set is not supported." | |
| $DisablingSystemStateText = "`nDisabling System State backup when the Bare Metal Recovery option is enabled is not supported." | |
| $AddBackupSourceText = 'Enter the index of the valid source to add to the backup policy (Blank=Cancel)' | |
| $RemoveBackupSourceText = 'Enter the index of the backup source to remove from the backup policy (Blank=Cancel)' | |
| $EnterBackupLabelText = 'Enter a label for this backup disk (Optional)' | |
| $AddVolumeTargetText = 'Enter the index of the valid volume target to add to the backup policy (Blank=Cancel)' | |
| $AddDiskTargetText = 'Enter the index of the valid disk target to add to the backup policy (Blank=Cancel)' | |
| $AddNetworkTargetText = 'Enter the path of the network share target to add to the backup policy (Blank=Cancel)' | |
| $RemoveBackupTargetText = 'Enter the index of the backup target to remove from the backup policy (Blank=Cancel)' | |
| $SetBackupScheduleText = 'Enter the backup Schedule for the backup policy (00:00[,12:00])' | |
| $SetBareMetalRecoveryText = 'Enable the Bare Metal Recovery backup setting (true|false)' | |
| $SetSystemStateText = 'Enable the System State backup setting (true|false)' | |
| $ConfigureHypervText = "\r\n\r\n7\) Configure Hyper-V Backup Sources" | |
| $NotSet = 'Not Set' | |
| ############################################################################### | |
| #### String Formatting Functions ############################################## | |
| ############################################################################### | |
| function ConvertToBool | |
| { param ($InputValue) | |
| if ($InputValue -match '^y(es)?$' -or $InputValue -match '^t(rue)?$') | |
| { $true } | |
| elseif ($InputValue -match '^f(alse)?$' -or $InputValue -match '^n(o)?$') | |
| { $false } | |
| $false | |
| } | |
| function FormatVolumeString | |
| { param ($Index, $Volume) | |
| $mountPath = | |
| if ($Volume.MountPath -ne '') | |
| { $Volume.MountPath } | |
| else | |
| { "None" } | |
| return [PSCustomObject]@{ | |
| Index = $Index | |
| "Mount Path" = $mountPath | |
| "Volume Label" = $Volume.VolumeLabel | |
| "Mount Point" = $Volume.MountPoint | |
| } | |
| } | |
| function FormatBackupTargetString | |
| { param ($Index, $Target) | |
| switch ($Target.TargetType) | |
| { | |
| "Disk" | |
| { [PSCustomObject]@{ | |
| Index = $Index | |
| "Disk Label" = $Target.Label | |
| "Disk Name" = $Target.WBDisk.DiskName | |
| "Disk ID" = $Target.WBDisk.DiskId } } | |
| "Volume" | |
| { $mountPath = | |
| if ($Target.WBVolume.MountPath -ne '') | |
| { $Target.WBVolume.MountPath } | |
| else | |
| { "None" } | |
| [PSCustomObject]@{ | |
| Index = $Index | |
| "Mount Path" = $mountPath | |
| "Volume Label" = $Target.WBVolume.VolumeLabel | |
| "Mount Point" = $Target.WBVolume.MountPoint } } | |
| "Network" | |
| { [PSCustomObject]@{ | |
| Index = $Index | |
| "Network Path" = $Target.Path } } | |
| } | |
| } | |
| function FormatDiskTargetString | |
| { param ($Index, $Disk) | |
| [PSCustomObject]@{ | |
| Index = $Index | |
| "Disk Name" = $Disk.DiskName | |
| "Disk ID" = $Disk.DiskId | |
| "Disk Volumes" = $Disk.Volumes } | |
| } | |
| function FormatHypervSourceString | |
| { param ($Index, $VirtualMachine) | |
| return [PSCustomObject]@{ | |
| Index = $Index | |
| "Backup Type/Virtual Machine Name" = $VirtualMachine.Caption | |
| } | |
| } | |
| ############################################################################### | |
| #### Utility Functions ######################################################## | |
| ############################################################################### | |
| function IsVolumeBackupTarget | |
| { param ($Policy, $Volume) | |
| try | |
| { | |
| foreach ($target in Get-WBBackupTarget -Policy $Policy) | |
| { if ($target.WBVolume.MountPoint -eq $Volume.MountPoint) | |
| { $true } } | |
| $false | |
| } | |
| catch | |
| { $false } | |
| } | |
| function IsVolumeBackupSource | |
| { param ($Policy, $Volume) | |
| (Get-WBVolume -Policy $Policy).MountPoint -contains $Volume.MountPoint | |
| } | |
| function IsVolumeValidBackupSource | |
| { param ($Policy, $Volume) | |
| $ValidSource = [VolumeProperties]::ValidSource | |
| ( | |
| !(IsVolumeBackupTarget -Policy $Policy -Volume $Volume) -and | |
| !(IsVolumeBackupSource -Policy $Policy -Volume $Volume) -and | |
| $Volume.Property -band $ValidSource | |
| ) | |
| } | |
| function IsDiskBackupTarget | |
| { param ($Policy, $Disk) | |
| try | |
| { | |
| if ((Get-WBBackupTarget -Policy $Policy | | |
| where { $_.WBDisk.DiskId -eq $Disk.DiskId }) -ne $null) | |
| { $true } | |
| $false | |
| } | |
| catch | |
| { $false } | |
| } | |
| function IsDiskBackupSource | |
| { param ($Policy, $Disk) | |
| $backupVolumes = Get-WBVolume -Policy $Policy | |
| foreach ($diskVolume in $Disk.Volumes) | |
| { if ($backupVolumes.MountPoint -contains $diskVolume.MountPoint) | |
| { $true } } | |
| $false | |
| } | |
| function IsDiskValidBackupTarget | |
| { param ($Policy, $Disk) | |
| $IsValidTarget = [DiskProperties]::ValidTarget | |
| ( | |
| !(IsDiskBackupTarget -Policy $Policy -Disk $Disk) -and | |
| !(IsDiskBackupSource -Policy $Policy -Disk $Disk) -and | |
| $Disk.Properties -band $IsValidTarget | |
| ) | |
| } | |
| function IsVolumeValidBackupTarget | |
| { param ($Policy, $Volume) | |
| $IsCritical = [VolumeProperties]::Critical | |
| $IsReadOnly = [VolumeProperties]::ReadOnly | |
| $IsEfiSystemPartition = [VolumeProperties]::IsExtensibleFirmwareInterfaceSystemPartition | |
| ( | |
| !(IsVolumeBackupTarget -Policy $Policy -Volume $Volume) -and | |
| !(IsVolumeBackupSource -Policy $Policy -Volume $Volume) -and | |
| !($Volume.Property -band $IsCritical) -and !($Volume.Property -band $IsReadOnly) -and | |
| !($Volume.Property -band $IsEfiSystemPartition) | |
| ) | |
| } | |
| function CanRemoveBackupSource | |
| { param ($Policy, $Volume) | |
| $IsCritical = [VolumeProperties]::Critical | |
| if ((Get-WBBareMetalRecovery -Policy $Policy) -and | |
| $Volume.Property -band $IsCritical) | |
| { $false } | |
| else | |
| { $true } | |
| } | |
| function IsValidVmBackupSource | |
| { param ($Policy, $VirtualMachine) | |
| !((Get-WBVirtualMachine -Policy $Policy).ComponentName -contains $VirtualMachine.ComponentName) | |
| } | |
| ############################################################################### | |
| #### Backup Sources Functions ################################################# | |
| ############################################################################### | |
| function GetValidBackupSources | |
| { param ($Policy, [ref]$SourceTable) | |
| $volumeIndex = 0 | |
| $sourceStringArray = | |
| Get-WBVolume -AllVolumes | | |
| % { | |
| if (IsVolumeValidBackupSource -Policy $Policy -Volume $_) | |
| { | |
| $SourceTable.Value += [PSCustomObject]@{ Index = $volumeIndex; Volume = $_ } | |
| FormatVolumeString -Index ($volumeIndex++) -Volume $_ | |
| } | |
| } | |
| ($sourceStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function GetBackupSources | |
| { param ($Policy, [ref]$SourceTable) | |
| $volumeIndex = 0 | |
| $sourceStringArray = | |
| Get-WBVolume -Policy $Policy | | |
| % { | |
| $SourceTable.Value += [PSCustomObject]@{ Index = $volumeIndex; Volume = $_ } | |
| FormatVolumeString -Index ($volumeIndex++) -Volume $_ | |
| } | |
| ($sourceStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function AddBackupSource | |
| { param ($Policy, $SourceTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $SourceTable.Index -contains $returnedInt) | |
| { | |
| $volumeEntry = ($SourceTable | where Index -eq $returnedInt) | |
| Add-WBVolume -Policy $Policy -Volume $volumeEntry.Volume ` | |
| -WarningAction "SilentlyContinue" | Out-Null | |
| } | |
| } | |
| function RemoveBackupSource | |
| { param ($Policy, $SourceTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $SourceTable.Index -contains $returnedInt) | |
| { | |
| $volumeEntry = ($SourceTable | where Index -eq $returnedInt) | |
| if (CanRemoveBackupSource -Policy $Policy -Volume $volumeEntry.Volume) | |
| { Remove-WBVolume -Policy $Policy -Volume $volumeEntry.Volume ` | |
| -WarningAction "SilentlyContinue" | Out-Null } | |
| else | |
| { Write-Host $RemovingCriticalVolumesText } | |
| } | |
| } | |
| ############################################################################### | |
| #### Backup Target Functions ################################################## | |
| ############################################################################### | |
| function GetValidTargetVolumes | |
| { param ($Policy, [ref]$VolumeTable) | |
| $volumeIndex = 0 | |
| $targetStringArray = | |
| Get-WBVolume -AllVolumes | | |
| % { | |
| if (IsVolumeValidBackupTarget -Policy $Policy -Volume $_) | |
| { | |
| $VolumeTable.Value += [PSCustomObject]@{ Index = $volumeIndex; Volume = $_ } | |
| FormatVolumeString -Index ($volumeIndex++) -Volume $_ | |
| } | |
| } | |
| ($targetStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function GetValidTargetDisks | |
| { param ($Policy, [ref]$DiskTable) | |
| $diskIndex = 0 | |
| $targetStringArray = | |
| Get-WBDisk | | |
| % { | |
| if (IsDiskValidBackupTarget -Policy $Policy -Disk $_) | |
| { | |
| $DiskTable.Value += [PSCustomObject]@{ Index = $diskIndex; Disk = $_ } | |
| FormatDiskTargetString -Index ($diskIndex++) -Disk $_ | |
| } | |
| } | |
| ($targetStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function GetBackupTargets | |
| { param ($Policy, [ref]$TargetTable) | |
| try | |
| { | |
| $targetIndex = 0 | |
| $targetStringArray = | |
| Get-WBBackupTarget -Policy $Policy | | |
| % { | |
| $TargetTable.Value += [PSCustomObject]@{ Index = $targetIndex; Target = $_ } | |
| FormatBackupTargetString -Index ($targetIndex++) -Target $_ | |
| } | |
| ($targetStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| catch | |
| { '' } | |
| } | |
| function AddVolumeBackupTarget | |
| { param ($Policy, $VolumeTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $VolumeTable.Index -contains $returnedInt) | |
| { | |
| $volumeEntry = ($VolumeTable | where Index -eq $returnedInt) | |
| Add-WBBackupTarget -Policy $Policy -Target (New-WBBackupTarget -Volume $volumeEntry.Volume) -Force | Out-Null | |
| } | |
| } | |
| function AddDiskBackupTarget | |
| { param ($Policy, $DiskTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $DiskTable.Index -contains $returnedInt) | |
| { | |
| $diskEntry = ($DiskTable | where Index -eq $returnedInt) | |
| $diskLabel = Read-Host $EnterBackupLabelText | |
| Add-WBBackupTarget -Policy $Policy -Target (New-WBBackupTarget -Disk $diskEntry.Disk -Label $diskLabel) -Force | Out-Null | |
| } | |
| } | |
| function AddNetworkBackupTarget | |
| { param ($Policy, $InputValue) | |
| if ($InputValue -eq '') | |
| { return } | |
| Add-WBBackupTarget -Policy $Policy -Target (New-WBBackupTarget -NetworkPath $InputValue -Credential (Get-Credential)) ` | |
| -Force -WarningAction "SilentlyContinue" | Out-Null | |
| } | |
| function RemoveBackupTarget | |
| { param ($Policy, $TargetTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $TargetTable.Index -contains $returnedInt) | |
| { | |
| $targetEntry = ($TargetTable | where Index -eq $returnedInt) | |
| Remove-WBBackupTarget -Policy $Policy -Target $targetEntry.Target | Out-Null | |
| } | |
| } | |
| ############################################################################### | |
| #### Hyper-V Functions ######################################################## | |
| ############################################################################### | |
| function GetValidHypervBackupSources | |
| { param ($Policy, [ref]$HypervSourceTable) | |
| $hypervSourceIndex = 0 | |
| $hypervSourceStringArray = | |
| Get-WBVirtualMachine | | |
| % { | |
| if (IsValidVmBackupSource -Policy $Policy -VirtualMachine $_) | |
| { | |
| $HypervSourceTable.Value += [PSCustomObject]@{ Index = $hypervSourceIndex; VirtualMachine = $_ } | |
| FormatHypervSourceString -Index ($hypervSourceIndex++) -VirtualMachine $_ | |
| } | |
| } | |
| ($hypervSourceStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function GetHypervBackupSources | |
| { param ($Policy, [ref]$HypervSourceTable) | |
| $hypervSourceIndex = 0 | |
| $hypervSourceStringArray = | |
| Get-WBVirtualMachine -Policy $Policy | | |
| % { | |
| $HypervSourceTable.Value += [PSCustomObject]@{ Index = $hypervSourceIndex; VirtualMachine = $_ } | |
| FormatHypervSourceString -Index ($hypervSourceIndex++) -VirtualMachine $_ | |
| } | |
| ($hypervSourceStringArray | Format-Table -AutoSize | Out-String) -replace "`r`n`r`n$",'' | |
| } | |
| function AddHypervBackupSource | |
| { param ($Policy, $HypervSourceTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $HypervSourceTable.Index -contains $returnedInt) | |
| { | |
| $vmEntry = ($HypervSourceTable | where Index -eq $returnedInt) | |
| Add-WBVirtualMachine -Policy $Policy -VirtualMachine $vmEntry.VirtualMachine ` | |
| -WarningAction "SilentlyContinue" | Out-Null | |
| } | |
| } | |
| function RemoveHypervBackupSource | |
| { param ($Policy, $HypervSourceTable, $InputValue) | |
| $returnedInt = 0 | |
| $result = [int]::TryParse($InputValue, [ref]$returnedInt) | |
| if ($result -and $HypervSourceTable.Index -contains $returnedInt) | |
| { | |
| $vmEntry = ($HypervSourceTable | where Index -eq $returnedInt) | |
| Remove-WBVirtualMachine -Policy $Policy -VirtualMachine $vmEntry.VirtualMachine ` | |
| -WarningAction "SilentlyContinue" | Out-Null | |
| } | |
| } | |
| ############################################################################### | |
| #### Backup Policy Functions ################################################## | |
| ############################################################################### | |
| # Work around for inability to save an existing policy that does not contain | |
| # any FileSpecs. This also converts any existing disk type backup target set | |
| # to a volume type backup target set to work around a similar bug | |
| function GetPolicy | |
| { param ($Policy) | |
| $CurrentPolicy = Get-WBPolicy -Editable | |
| if ($CurrentPolicy -ne $null) | |
| { | |
| Set-WBSchedule -Policy $Policy -Schedule $CurrentPolicy.Schedule | Out-Null | |
| foreach ($target in $CurrentPolicy.BackupTargets) | |
| { Add-WBBackupTarget -Policy $Policy -Target (New-WBBackupTarget -Volume $target.WBVolume) ` | |
| -Force -WarningAction "SilentlyContinue" | Out-Null } | |
| foreach ($volume in $CurrentPolicy.VolumesToBackup) | |
| { Add-WBVolume -Policy $Policy -Volume $volume -WarningAction "SilentlyContinue" | Out-Null } | |
| if ($CurrentPolicy.BMR) | |
| { Add-WBBareMetalRecovery -Policy $Policy | Out-Null } | |
| if ($CurrentPolicy.SystemState) | |
| { Add-WBSystemState -Policy $Policy | Out-Null } | |
| switch ($CurrentPolicy.VssBackupOptions) | |
| { | |
| 'VssFullBackup' | |
| { Set-WBVssBackupOptions -Policy $Policy -VssFullBackup | Out-Null } | |
| 'VssCopyBackup' | |
| { Set-WBVssBackupOptions -Policy $Policy -VssCopyBackup | Out-Null } | |
| } | |
| $false | |
| } | |
| else | |
| { $true } | |
| } | |
| function IsValidPolicy | |
| { param ($Policy) | |
| try | |
| { | |
| $schedule = Get-WBSchedule -Policy $Policy | |
| $backupVolumes = Get-WBVolume -Policy $Policy | |
| $backupTargets = Get-WBBackupTarget -Policy $Policy | |
| if ($schedule[0] -eq $null -or | |
| $backupVolumes[0] -eq $null -or | |
| $backupTargets[0] -eq $null) | |
| { $false } | |
| $true | |
| } | |
| catch | |
| { $false } | |
| } | |
| function SetPolicyBackupSchedule | |
| { param ($Policy, $InputValue) | |
| try | |
| { | |
| $times = $InputValue -split ',' | |
| $dates = $times | % { [DateTime]::Parse($_) } | |
| Set-WBSchedule -Policy $Policy -Schedule $dates | Out-Null | |
| } | |
| catch | |
| { } | |
| } | |
| function SetPolicyBareMetalRecovery | |
| { param ($Policy, $InputValue) | |
| try | |
| { | |
| if (ConvertToBool -InputValue $InputValue) | |
| { | |
| Add-WBBareMetalRecovery -Policy $Policy | Out-Null | |
| Add-WBSystemState -Policy $Policy | Out-Null | |
| Add-WBVolume -Policy $Policy -Volume (Get-WBVolume -CriticalVolumes) -WarningAction "SilentlyContinue" | Out-Null | |
| } | |
| else | |
| { Remove-WBBareMetalRecovery -Policy $Policy | Out-Null } | |
| } | |
| catch | |
| { } | |
| } | |
| function SetPolicySystemState | |
| { param ($Policy, $InputValue) | |
| try | |
| { | |
| if (ConvertToBool -InputValue $InputValue) | |
| { Add-WBSystemState -Policy $Policy | Out-Null } | |
| else | |
| { | |
| if (Get-WBBareMetalRecovery -Policy $Policy) | |
| { Write-Host $DisablingSystemStateText } | |
| else | |
| { Remove-WBSystemState -Policy $Policy | Out-Null } | |
| } | |
| } | |
| catch | |
| { } | |
| } | |
| function SavePolicy | |
| { param ($Policy) | |
| try | |
| { if (IsValidPolicy -Policy $Policy) | |
| { Set-WBPolicy -Policy $Policy } } | |
| catch | |
| { } | |
| } | |
| ############################################################################### | |
| #### Submenu Functions ######################################################## | |
| ############################################################################### | |
| function ManageHypervBackupSourcesMenu | |
| { param ($Policy) | |
| do | |
| { | |
| $currentHypervSourceTable = @() | |
| $validHypervSourceTable = @() | |
| $currentHypervSourcesText = GetHypervBackupSources -Policy $Policy -HypervSourceTable ([ref]$currentHypervSourceTable) | |
| $validHypervSourcesText = GetValidHypervBackupSources -Policy $Policy -HypervSourceTable ([ref]$validHypervSourceTable) | |
| $selection = Read-Host ($HypervBackupSourcesMenuText -f $currentHypervSourcesText, $validHypervSourcesText) | |
| switch ($selection) | |
| { | |
| 1 { AddHypervBackupSource -Policy $Policy -HypervSourceTable $validHypervSourceTable -InputValue ` | |
| (Read-Host $AddBackupSourceText) } | |
| 2 { RemoveHypervBackupSource -Policy $Policy -HypervSourceTable $currentHypervSourceTable -InputValue ` | |
| (Read-Host $RemoveBackupSourceText) } | |
| } | |
| } | |
| until ($selection -eq '0') | |
| } | |
| function ManageBackupTargetsMenu | |
| { param ($Policy) | |
| do | |
| { | |
| $currentTargetTable = @() | |
| $validVolumeTargetTable = @() | |
| $validDiskTargetTable = @() | |
| $backupTargetsText = GetBackupTargets -Policy $Policy -TargetTable ([ref]$currentTargetTable) | |
| $validVolumeTargetsText = GetValidTargetVolumes -Policy $Policy -VolumeTable ([ref]$validVolumeTargetTable) | |
| $validDiskTargetsText = GetValidTargetDisks -Policy $Policy -DiskTable ([ref]$validDiskTargetTable) | |
| $selection = Read-Host ($BackupTargetsMenuText -f $backupTargetsText, $validVolumeTargetsText, $validDiskTargetsText) | |
| switch ($selection) | |
| { | |
| 1 { AddVolumeBackupTarget -Policy $Policy -VolumeTable $validVolumeTargetTable -InputValue ` | |
| (Read-Host $AddVolumeTargetText) } | |
| 2 { AddDiskBackupTarget -Policy $Policy -DiskTable $validDiskTargetTable -InputValue ` | |
| (Read-Host $AddDiskTargetText) } | |
| 3 { AddNetworkBackupTarget -Policy $Policy -InputValue ` | |
| (Read-Host $AddNetworkTargetText) } | |
| 4 { RemoveBackupTarget -Policy $Policy -TargetTable $currentTargetTable -InputValue ` | |
| (Read-Host $RemoveBackupTargetText) } | |
| } | |
| } | |
| until ($selection -eq '0') | |
| } | |
| function ManageBackupSourcesMenu | |
| { param ($Policy) | |
| do | |
| { | |
| $currentTargetTable = @() | |
| $currentSourceTable = @() | |
| $validSourceTable = @() | |
| GetBackupTargets -Policy $Policy -TargetTable ([ref]$currentTargetTable) | Out-Null | |
| $currentSourcesText = GetBackupSources -Policy $Policy -SourceTable ([ref]$currentSourceTable) | |
| $validSourcesText = GetValidBackupSources -Policy $Policy -SourceTable ([ref]$validSourceTable) | |
| $selection = Read-Host ($BackupSourcesMenuText -f $currentSourcesText, $validSourcesText) | |
| switch ($selection) | |
| { | |
| 1 { AddBackupSource -Policy $Policy -SourceTable $validSourceTable -InputValue ` | |
| (Read-Host $AddBackupSourceText) } | |
| 2 { RemoveBackupSource -Policy $Policy -SourceTable $currentSourceTable -InputValue ` | |
| (Read-Host $RemoveBackupSourceText) } | |
| } | |
| } | |
| until ($selection -eq '0') | |
| } | |
| function ManageVssOptionsMenu | |
| { param ($Policy) | |
| switch (Read-Host $VssOptionsMenuText) | |
| { | |
| 1 { Set-WBVssBackupOptions -Policy $Policy -VssFullBackup | Out-Null } | |
| 2 { Set-WBVssBackupOptions -Policy $Policy -VssCopyBackup | Out-Null } | |
| } | |
| } | |
| ############################################################################### | |
| #### Main Menu Functions ###################################################### | |
| ############################################################################### | |
| function FormatMainMenu | |
| { param ($Policy, $IsNewPolicy) | |
| $schedule = Get-WBSchedule -Policy $Policy | |
| $bmr = Get-WBBareMetalRecovery -Policy $Policy | |
| $ssb = Get-WBSystemState -Policy $Policy | |
| $vss = Get-WBVssBackupOptions -Policy $Policy | |
| $scheduleFormatted = $NotSet | |
| $times = $schedule | | |
| % { '{0:t}' -f $_ } | |
| if ($times -ne $null) | |
| { $scheduleFormatted = [String]::Join(', ', $times) } | |
| $policyText = | |
| if ($IsNewPolicy) | |
| { $NewPolicyText } | |
| else | |
| { $ExistingPolicyText } | |
| if (!$HaveHyperv) | |
| { $MainMenuTemplate = $MainMenuTemplate -replace $ConfigureHypervText, '' } | |
| ($MainMenuTemplate -f $policyText, $scheduleFormatted, $bmr, $ssb, $vss) | |
| } | |
| $WSBPolicy = New-WBPolicy | |
| # Main Loop | |
| do | |
| { | |
| $selection = Read-Host (FormatMainMenu -Policy $WSBPolicy -IsNewPolicy (GetPolicy -Policy $WSBPolicy)) | |
| switch ($selection) | |
| { | |
| 1 { SetPolicyBackupSchedule -Policy $WSBPolicy -Input (Read-Host $SetBackupScheduleText) } | |
| 2 { SetPolicyBareMetalRecovery -Policy $WSBPolicy -Input (Read-Host $SetBareMetalRecoveryText) } | |
| 3 { SetPolicySystemState -Policy $WSBPolicy -Input (Read-Host $SetSystemStateText) } | |
| 4 { ManageVssOptionsMenu -Policy $WSBPolicy } | |
| 5 { ManageBackupSourcesMenu -Policy $WSBPolicy } | |
| 6 { ManageBackupTargetsMenu -Policy $WSBPolicy } | |
| 7 { if ($HaveHyperv) { ManageHypervBackupSourcesMenu -Policy $WSBPolicy } } | |
| 8 { SavePolicy -Policy $WSBPolicy } | |
| 9 { Remove-WBPolicy -All -Force | Out-Null; $WSBPolicy = New-WBPolicy } | |
| 10 { Start-WBBackup -Policy $WSBPolicy } | |
| } | |
| } | |
| until ($Selection -eq '0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment