PowerShell script exchange
Post Reply
Vek17
Service Provider
Posts: 49
Liked: 15 times
Joined: May 29, 2018 8:42 pm
Contact:

Method to cleanly stop maintaince session

Post by Vek17 »

I currently have a script I use to prepare an SoBR to force active fulls to move backup chains between extents in preparation for an evacuation. Everything currently works more or less as I'd expect except stopping a running mainatince session when the script is ending. Idealy I'd like to be able to mimic the behavior of the stop command in the UI where running tasks are stopped and the session is canceled to prevent the extent from switching into maitaince mode at some undetermined time when the session completes. I have yet to find a solid supported or unsupported method to do so. Does any one have any where they can point me?

Exert of currently used code for this (This does not behave exactly how I'd like):

Code: Select all

foreach($Session in $MaintainceSessions){
    if([Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).IsWorking){
        #[Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).AbortSession()
        [Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).CompleteWithSuccess()
    }
}

Full function:

Code: Select all

function Optimize-ActiveFulls{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [Veeam.Backup.PowerShell.Infos.VBRScaleOutBackupRepository] $Repository
    )
    BEGIN{
        function CacheSanityCheck{
            $ExtentMaxTaskCounts = Get-ChildItem C:\ProgramData\Veeam\ReportingCache -Filter 'ExtentMaxTaskCounts*' | Sort-Object name -Descending | select -First 1 | Import-Clixml
            $MismatchedRepos = @()
            foreach($ID in $ExtentMaxTaskCountsMap.keys){
                $Repo = [Veeam.Backup.Core.CBackupRepository]::Get($ID)
                if($Repo.Options.MaxTaskCount -ne $ExtentMaxTaskCounts[$ID]){
                    $MismatchedRepos += [PSCustomObject]@{
                        RepoName     = $Repo.name
                        ID           = $Repo.ID
                        CachedTask   = $ExtentMaxTaskCounts[$ID]
                        CurrentTasks = $Repo.Options.MaxTaskCount
                    }
                    Write-Host "$($Repo.Options.MaxTaskCount) of $($ExtentMaxTaskCounts[$ID]) - Task count mismatch: $($Repo.Name)" -ForegroundColor Red
                }else{
                    Write-Host "$($Repo.Options.MaxTaskCount) of $($ExtentMaxTaskCounts[$ID]) - Task count match: $($Repo.Name)" -ForegroundColor Green
                }
            }
            $MismatchedRepos | Out-GridView -OutputMode Multiple -Title "Select Repo Settings to Reset to Cache" | ForEach-Object{
                $Repo = [Veeam.Backup.Core.CBackupRepository]::Get($_.Id)
                $Repo.Options.MaxTaskCount = $_.CachedTask
                $Repo.SaveOptions()
            }
        }
        #Pre-Run sanity checks
        CacheSanityCheck
        #Save Inital Task Counts
        $ExtentMaxTaskCounts = @{}
        forEach($Extent in [Veeam.Backup.Core.CExtendableRepository]::Get($Repository.Id).GetExtents()){
            $ExtentMaxTaskCounts.Add($Extent.ID,$Extent.Options.MaxTaskCount)
        }
        $ExtentMaxTaskCounts | Export-Clixml -Path "C:\ProgramData\Veeam\ReportingCache\ExtentMaxTaskCounts_$([DateTime]::Now.ToString('yyyyMMddHHmmss')).xml"
        #Enable PerformFullWhenExtentOffline
        Set-VBRScaleOutBackupRepository -Repository $Repository -PerformFullWhenExtentOffline:$true -Extent $Repository.Extent
        #Enable mainatiance mode
        $MaintainceExtents = $Repository | Get-VBRRepositoryExtent | Out-GridView -OutputMode Multiple -Title 'Extents to put into maintenance mode'    
        $MaintainceSessions = $MaintainceExtents | Enable-VBRRepositoryExtentMaintenanceMode -RunAsync
        #Set Task Counts to 1
        [Veeam.Backup.Core.CExtendableRepository]::Get($Repository.Id).GetExtents() | ForEach-Object {$_.Options.MaxTaskCount = 1; $_.SaveOptions()}
    } 
    PROCESS{
        Write-Host "To Exit Type: Done"
        Do{
            $input = Read-Host -Prompt "Update Task Limit (Y)"
            if($input -match '^Y'){
                $SelectedExtents = [Veeam.Backup.Core.CExtendableRepository]::Get($Repository.Id).GetExtents() | Out-GridView -OutputMode Multiple -Title 'Extents to modify'
                do{
                    $Input = $null
                    try{
                        $Input = [Int32](Read-Host -Prompt "New Task Limit")
                    }catch{
                        Write-Warning 'Please enter an integer'
                    }
                }while(!$Input)
                foreach($Extent in $SelectedExtents){
                    $Extent.Options.MaxTaskCount = $Input
                    $Extent.SaveOptions()
                }
            }
        }While($Input -notmatch '^Done$')
    }
    END{
        #Disable PerformFullWhenExtentOffline
        Set-VBRScaleOutBackupRepository -Repository $Repository -PerformFullWhenExtentOffline:$false -Extent $Repository.Extent
        #Reset Task Counts
        foreach($Extent in [Veeam.Backup.Core.CExtendableRepository]::Get($Repository.Id).GetExtents()){
            $Extent.Options.MaxTaskCount = $ExtentMaxTaskCounts[$Extent.Id]
            $Extent.SaveOptions()
        }
        #Kill still running mainatiance sessions
        foreach($Session in $MaintainceSessions){
            if([Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).IsWorking){
                #[Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).AbortSession()
                [Veeam.Backup.Core.CRepositoryMaintenanceSession]::Get($Session.ID).CompleteWithSuccess()
            }
        }
        #Remove extents from maintaince mode
        $MaintainceExtents | Disable-VBRRepositoryExtentMaintenanceMode -RunAsync > $null
        #Pre-Post sanity checks
        CacheSanityCheck
    }
}
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests