Public/Start-MSPBackupRestore.ps1

Function Start-MSPBackupRestore {
<#
    .SYNOPSIS
        Run MSP Backup restore
    .DESCRIPTION
        Run MSP Backup restore
    .Parameter Datasource
        Name of data source to restore
        (possible values are: Fs, SystemState, MsSql, Exchange, NetworkShares, VMWare, VssMsSql, VssSharePoint, Oracle, Sims, VssHyperV, MySql, LinuxSystemState).
    .Parameter Suffix
        Add suffix to restored files.
    .Parameter ExistingFilesPolicy
        Existing files restore policy. Possible values are Overwrite or Skip. Default value is Overwrite
    .Parameter OutdatedFilesPolicy
        Outdated files restore policy. Possible values are CheckContentOfAllFiles or CheckContentOfOutdatedFilesOnly. Default value is CheckContentOfAllFiles
    .Parameter RestoreTo
        Path where you want to restore data. Data will be restored to original location if empty.
    .Parameter Selection
        Data path to restore. Whole selected session will be restored if the parameter empty or not specified.
    .Parameter SessionSearchPolicy
        Backup session search policy. Possible values are ClosestToRequested or OldestIfRequestedNotFound. Default value is ClosestToRequested
    .Parameter Time
        Start time of backup session. Value must be provided in format "yyyy-mm-dd hh:mm:ss". Default is to restore the most recent session.
    .EXAMPLE
        Start-MSPBackupRestore -Datasource SystemState
        Restores the system state of the computer
    .INPUTS
        None
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType('System.String')]
    Param(
        [ValidateSet('Fs', 'SystemState', 'MsSql', 'Exchange', 'NetworkShares', 'VMWare', 'VssMsSql', 'VssSharePoint', 'Oracle', 'Sims', 'VssHyperV', 'MySql', 'LinuxSystemState')]
        [Parameter(Mandatory = $True)]
        [String]$Datasource,
        [String]$Suffix,
        [String]$ExistingFilesPolicy,
        [String]$OutdatedFilesPolicy,
        [String]$RestoreTo,
        [String]$Selection,
        [String]$SessionSearchPolicy,
        [datetime]$Time
    )
    Begin {
        Write-Verbose ('{0}:: Function started' -f $MyInvocation.MyCommand)
        $stdOutTempFile = [System.IO.Path]::GetTempFileName()
        $stdErrTempFile = [System.IO.Path]::GetTempFileName()
        Write-Verbose ('{0}:: Formatting parameters for clienttool.exe' -f $MyInvocation.MyCommand)
        If($Selection) { $Selection = "-selection " + $Selection }
        If($Suffix) { $Suffix = "-add-suffix " + $Suffix }
        If($ExistingFilesPolicy) { $ExistingFilesPolicy = "-existing-files-restore-policy " + $ExistingFilesPolicy }
        If($OutdatedFilesPolicy) { $OutdatedFilesPolicy = "-outdated-files-restore-policy " + $OutdatedFilesPolicy }
        If($SessionSearchPolicy) { $SessionSearchPolicy = "-session-search-policy " + $SessionSearchPolicy }
        If($RestoreTo) {
            $PathExists = Test-Path $RestoreTo
            If($PathExists -ne "True") {
                Write-Verbose ('{0}:: Creating folder for restore {1}' -f $MyInvocation.MyCommand, $RestoreTo)
                New-Item -Path $RestoreTo -ItemType Directory -Force | Out-Null
            }
            [String]$RestoreTo = "-restore-to " + $RestoreTo
        }
        If($Time) {
            Write-Verbose ('{0}:: Reformatting time parameter for clienttool.exe' -f $MyInvocation.MyCommand)
            $Date = Get-Date -Date $Time -Format "yyyy-MM-dd hh:mm:ss"
            [String]$Time = "-time " + $Date
        }
        $ArgArray = @(
            "-machine-readable",
            "control.restore.start",
            "-datasource $Datasource",
            "-selection $Selection",
            "-add-suffix $Suffix",
            "-existing-files-restore-policy $ExistingFilesPolicy",
            "-outdated-files-restore-policy $OutdatedFilesPolicy",
            "-session-search-policy $SessionSearchPolicy",
            "-restore-to $RestoreTo",
            "-time $Date"
        )
    }
    Process {
        Write-Verbose ('{0}:: Getting status' -f $MyInvocation.MyCommand)
        $Status = & $Script:CmdPath -machine-readable control.restore.start -datasource $Datasource $Selection $Suffix $ExistingFilesPolicy $OutdatedFilesPolicy $RestoreTo $SessionSearchPolicy $Time
    }
    End {
        Write-Verbose ('{0}:: Function ended' -f $MyInvocation.MyCommand)
        Return $Status
    }
}