Obs/bin/ObsDep/content/Powershell/Common/BCDRActionHelper.psm1

<###################################################
 # #
 # Copyright (c) Microsoft. All rights reserved. #
 # #
 ##################################################>


 Import-Module $PSScriptRoot\Tracer.psm1

 $BackupStartedEventLogPattern = "Action {0} started with session name <{1}>, session shell ID {2} and process ID {3}."
 $BackupCompletedEventLogPattern = "Action {0} completed with session name <{1}>."
 $BackupFailedEventLogPattern = "Action {0} failed with session name <{1}>. Exception: {2}"
 $BCDRSessionNameBase = "AzsBCDRSession"
 $BCDRLogName = "AzSBCDR"
 $BCDRBackupEventSource = "AzSBackupAction"

 # TODO: restore actions are not utilizing the action helper at the moment.
 $BCDRRestoreEventSource = "AzSRestoreAction"

 $BCDRRemoteActionStartedEventId = 1
 $BCDRRemoteActionCompletededEventId = 2
 $BCDRRemoteActionFailedEventId = 3

 # 4 hour session idle timeout
 $BCDRRemoteSessionIdleTimeoutInSec = 14400

<#
.Synopsis
   Get the BCDR action plan constants
#>

function Get-BCDRActionPlanConsts
{
    $consts = @{}
    $consts.BackupStartedEventLogPattern = $BackupStartedEventLogPattern
    $consts.BackupCompletedEventLogPattern = $BackupCompletedEventLogPattern
    $consts.BackupFailedEventLogPattern = $BackupFailedEventLogPattern
    $consts.BCDRSessionNameBase = $BCDRSessionNameBase
    $consts.BCDRLogName = $BCDRLogName
    $consts.BCDRBackupEventSource = $BCDRBackupEventSource
    $consts.BCDRRestoreEventSource = $BCDRRestoreEventSource
    $consts.BCDRRemoteActionStartedEventId = $BCDRRemoteActionStartedEventId
    $consts.BCDRRemoteActionCompletededEventId = $BCDRRemoteActionCompletededEventId
    $consts.BCDRRemoteActionFailedEventId = $BCDRRemoteActionFailedEventId

    return $consts
}

<#
.Synopsis
   Get the BCDR action plan constant
#>

function Get-BCDRActionPlanConst
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('BackupStartedEventLogPattern', 'BackupCompletedEventLogPattern', 'BackupFailedEventLogPattern', `
            'BCDRSessionNameBase', 'BCDRLogName', 'BCDRBackupEventSource', 'BCDRRestoreEventSource', `
            'BCDRRemoteActionStartedEventId', 'BCDRRemoteActionCompletededEventId', 'BCDRRemoteActionFailedEventId')]
        [string] $ConstName
    )

    $consts = Get-BCDRActionPlanConsts
    return $consts[$ConstName]
}

<#
.Synopsis
    Create event source and log on target VMs
#>

function Ensure-BCDREventLog
{
    param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $ComputerName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(Mandatory=$true)]
        [string] $EventSource
    )

    Invoke-Command -ComputerName $ComputerName -Credential $Credential -ArgumentList @($BCDRLogName, $EventSource) `
        -ScriptBlock {
        param ($BCDRLogName, $EventSource)
        if ([System.Diagnostics.EventLog]::SourceExists($EventSource) -eq $false)
        {
            Trace-Execution "Creating event source $EventSource on event log $BCDRLogName"
            [System.Diagnostics.EventLog]::CreateEventSource($EventSource, $BCDRLogName)
            Trace-Execution "Event source $EventSource created"
            Limit-EventLog -LogName $BCDRLogName -RetentionDays 30
        }
    }
}

<#
.Synopsis
    Get BCDR action event logs with a specific backup from the target VM
#>

function Get-BCDRActionEventLog
{
    param 
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $ComputerName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $BackupId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $SessionName
    )

    return Invoke-Command -ComputerName $ComputerName -Credential $Credential -ArgumentList @($BackupId, $BCDRLogName, $SessionName) -ScriptBlock {
        param ($BackupId, $BCDRLogName, $SessionName)
        try
        {
            $logs = Get-EventLog -LogName $BCDRLogName -ErrorAction Stop `
                | ? { ($_.Message -like "*$BackupId*") -and ($_.Message -like "*<$SessionName>*") }

            # Log the last 5 logs about this backup session for diagnostics
            $numHistory = 5
            foreach ($log in ($logs | select -First $numHistory))
            {
                Trace-Execution "Last $numHistory logs for backup $BackupId and session '$SessionName'"
                Trace-Execution "$($log.TimeGenerated), EventId: $($log.EventID), Msg: $($log.Message)"
            }

            return $logs
        }
        catch
        {}
    }
}

<#
.Synopsis
    Check if the BCDR remote session process still exists and kill the process if requested to
#>

function Check-BCDRSessionProcess
{
    param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $ComputerName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $SessionPid,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [DateTime] $SessionCreationTime,

        [switch] $Kill
    )

    $proc = $null
    $retries = 10
    while (!$proc)
    {
        try
        {
            # Use Invoke-Command instead of 'Get-Process -CompueterName' to make sure process StartTime properties can
            # be returned
            $proc = Invoke-Command -ComputerName $ComputerName -Credential $Credential -ScriptBlock {
                Get-Process -ProcessName 'wsmprovhost' -ErrorAction Stop
            }
        }
        catch
        {
            $retries--
            if ($retries -gt 0)
            {
                Trace-Execution "Failed to get PowerShell remote host processes on $ComputerName. Exception: $_. Retrying..."
                Start-Sleep -s 10
                continue
            }

            throw
        }
    }

    $sessionProc = $proc | ? { ($_.Id -eq $SessionPid)}
    if ($sessionProc)
    {
        # Check the creation time to prevent accidental termination of other processes with the same Pid.
        # Session process must be started earlier than session log time. Give it 1 extra second to the session creation
        # time because ETL log time has seconds precision while the process start time has milliseconds precision.
        $sessionProcStartTimeUTC = $sessionProc.StartTime.ToUniversalTime()
        $sessionCreationTimeUTC = $SessionCreationTime.ToUniversalTime().AddSeconds(1)

        if ($sessionProcStartTimeUTC -le $sessionCreationTimeUTC)
        {
            if ($Kill.IsPresent)
            {
                Invoke-Command -ComputerName $ComputerName -Credential $Credential -ArgumentList @($SessionPid) `
                    -ScriptBlock {
                        param ($SessionPid)
                        Stop-Process -Id $SessionPid -Force -ErrorAction SilentlyContinue
                }
            }

            return $true
        }
        else
        {
            Trace-Execution "The process with Id $SessionPid isn't the remote PowerShell host process for this invocation."
            Trace-Execution "Session process start time: $sessionProcStartTimeUTC, Session creation time: $sessionCreationTimeUTC"
        }
    }
    else
    {
        $str = $null
        $proc | select Id, ProcessName | % { $str += "($($_.Id), $($_.ProcessName))`n" }
        Trace-Execution "All PowerShell remote host processes names on $ComputerName :`n$str"
        Trace-Execution "Failed to find a remote PowerShell host process with process Id $SessionPid on $ComputerName."
    }

    return $false
}

<#
.Synopsis
    Check previously started BCDR remote sessions.
 
    The BCDR remote sessions for each role (and each invocation if a role has more than one) are named with this pattern:
    '<BCDRSessionNameBase>-<FullRepositoryName>'
 
    This method searches the remote session name on all nodes for the current role, try to get the action status to
    determine next actions, and finally clean up the sessions.
#>

function Check-PreviousBCDRSession
{
    param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string[]] $NodesForCurrentRole,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $BackupId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $SessionName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $SessionInfoEventRegex,

        [Parameter(Mandatory=$false)]
        $ProvisionedPSSession = $null,

        [Parameter(Mandatory=$false)]
        [PSCredential] $ProvisionedPSSessionCredential = $null,

        [Parameter(Mandatory=$true)]
        [uint32] $ExpectedRuntimeInMin,

        [Parameter(Mandatory=$true)]
        [ref] $ShouldInvokeAction
    )

    $ShouldInvokeAction.Value = $true

    # Test if the session already exists on all the nodes
    $sessions = @()
    Trace-Execution "Searching for existing PSSessions on $NodesForCurrentRole"
    foreach ($Node in $NodesForCurrentRole)
    {
        if ($ProvisionedPSSessionCredential)
        {
            $sessions += Get-PSSession -ComputerName $Node -Credential $ProvisionedPSSessionCredential -Name $SessionName -ErrorAction SilentlyContinue
        }
        else
        {
            $sessions += Get-PSSession -ComputerName $Node -Credential $Credential -Name $SessionName -ErrorAction SilentlyContinue
        }
    }

    # Make sure to filter out the provisioned PSSession if specified.
    Trace-Execution "Found sessions: $sessions"
    if ($ProvisionedPSSession)
    {
        $sessions = $sessions | ? {
            if ($ProvisionedPSSession.InstanceId -ne $_.InstanceId)
            {
                return $true
            }
            else
            {
                Trace-Execution "Filter out the provisioned PSSession with ID $($ProvisionedPSSession.InstanceId) from the sessions."
                return $false
            }
        }
    }

    foreach ($session in $sessions)
    {
        # Properly working BCDR remote sessions should be in the 'Disconnected' state and ready to be connected
        try
        {
            Trace-Execution "The disconnected remote PSSession '$($session.Name)' exists on $($session.ComputerName)."
            Trace-Execution "State: $($session.State), Availability: $($session.Availability), Instance ID: $($session.InstanceId)."
            Trace-Execution "Check the previous backup status of BackupID $backupID."

            # Get the latest log with this backup ID
            $latestLog = Get-BCDRActionEventLog -ComputerName $session.ComputerName -Credential $Credential `
                -BackupId $backupId -SessionName $SessionName | select -First 1

            if ($latestLog)
            {
                switch ($latestLog.EventId)
                {
                    # Started
                    $BCDRRemoteActionStartedEventId
                    {
                        $expectedCompletion = $latestLog.TimeGenerated.ToUniversalTime().AddMinutes($ExpectedRuntimeInMin)
                        Trace-Execution "Action started at UTC $($latestLog.TimeGenerated.ToUniversalTime().ToString())"
                        Trace-Execution "Expected to complete by UTC $($expectedCompletion.ToUniversalTime().ToString())"
                        $actionPlanDone = $false

                        do
                        {
                            # Check for completion
                            $now = Get-Date
                            Trace-Execution "Current time is UTC $($now.ToUniversalTime().ToString())"

                            $log = Get-BCDRActionEventLog -ComputerName $session.ComputerName -Credential $Credential `
                                -BackupId $backupId -SessionName $SessionName | select -First 1
                            if ($log.EventId -eq $BCDRRemoteActionCompletededEventId)
                            {
                                $ShouldInvokeAction.Value = $false
                                $actionPlanDone = $true
                            }
                            elseif ($log.EventId -eq $BCDRRemoteActionFailedEventId)
                            {
                                Trace-Execution "The previous action failed, invoking the action again"
                                $actionPlanDone = $true
                            }

                            if ($actionPlanDone)
                            {
                                break
                            }

                            # Keep waiting
                            Start-Sleep -Seconds 30

                        } while ($now -le $expectedCompletion)

                        if (!$actionPlanDone)
                        {
                            Trace-Execution "The previous action plan didn't finish in time, restarting another one."
                        }
                    }

                    # Completed
                    $BCDRRemoteActionCompletededEventId { $shouldInvokeAction.Value = $false }
        
                    # Failed
                    $BCDRRemoteActionFailedEventId { Trace-Execution "The previous action failed, invoking the action again" }
                }
            }

            break
        }
        catch
        {}
    }

    if ($sessions)
    {
        Trace-Execution "Removing the disconnected PSSessions."
        $sessions | Remove-PSSession -ErrorAction SilentlyContinue
    }

    foreach ($Node in $NodesForCurrentRole)
    {
        try
        {
            Trace-Execution "Make sure the WSMan instance and the process are both gone on $Node"
            $instances = Get-WSManInstance -ConnectionURI "http://$Node`:5985/wsman" shell -Enumerate `
                -cred $Credential -ErrorAction SilentlyContinue | ? Name -eq $SessionName
    
            if ($ProvisionedPSSession)
            {
                $instances = $instances | ? {
                    if ($ProvisionedPSSession.InstanceId -ne $_.ShellId)
                    {
                        return $true
                    }
                    else
                    {
                        Trace-Execution "Filter out the provisioned PSSession ID $($ProvisionedPSSession.InstanceId) from the instances"
                        return $false
                    }
                }
            }
    
            foreach ($instance in $instances)
            {
                Trace-Execution "Try to remove the WSMan object with shell ID $($instance.ShellId)"
                Remove-WSManInstance -ConnectionURI "http://$Node`:5985/wsman" shell @{ShellID="$($instance.ShellId)"} `
                    -cred $Credential -ErrorAction SilentlyContinue
            }

            $logs = Get-BCDRActionEventLog -ComputerName $Node -Credential $Credential -BackupId $backupId `
                -SessionName $SessionName | ? EventID -eq $BCDRRemoteActionStartedEventId

            if ($logs)
            {
                Trace-Execution "Check the previous session PID."
                foreach ($log in $logs)
                {
                    Trace-Execution "Log message: $($log.Message)"
                    if ($log.Message -Match $SessionInfoEventRegex)
                    {
                        $sessionNameFromLog = $Matches[2]
                        if ($sessionNameFromLog -ne $SessionName)
                        {
                            Trace-Execution "Not for the current session, skipped."
                            continue
                        }
    
                        $wsPid = $Matches[4]
                        $logTime = $log.TimeGenerated.ToUniversalTime()
                        if (![string]::IsNullOrEmpty($wsPid))
                        {
                            Trace-Execution "Make sure process '$wsPid' is gone."
                            $null = Check-BCDRSessionProcess -ComputerName $Node -Credential $Credential `
                                -SessionPid $wsPid -SessionCreationTime $logTime -Kill
                        }
                    }
                }
            }
        }
        catch
        {
            Trace-Execution "Failed to clean up the WSMan instance and/or the process on $Node. Proceeding anyway. Exception: $_"
        }
    }
}

<#
.Synopsis
    A wrapper around actual execution on remote machines when the execution runs in a JEA session, in which case this
    method must be whitelisted in the session configuration (.psrc) or imported manually as part of the initialization
#>

function Start-BCDRExecutionOnRemoteComputer
{
    param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $BCDRRemoteExecutionScriptBlockAsStr,

        [Parameter(Mandatory=$true)]
        $ScriptBlockParameters
    )

    $BCDRRemoteExecutionScriptBlock = [scriptblock]::Create($BCDRRemoteExecutionScriptBlockAsStr)

    # Start the script block execution async
    Start-Job -ScriptBlock $BCDRRemoteExecutionScriptBlock -ArgumentList $ScriptBlockParameters
}

<#
.Synopsis
    Execute backup operations idempotently.
 
    BCDR ECE actions start remote PSSession on VMs for roles that implements backup/restore interfaces to perform
    backup/restore actions. This method ensures that these sessions can be tracked or cleanly canceled when ECE fails
    over and/or crashes by disconnecting the worker PSSessions and poll for completion.
 
.PARAMETER ComputerName
    The targe computer name where the execution is run. If ProvisionedPSSession isn't specified, a disconnected session
    will be created for the caller. Otherwise the computer name is used to poll action plan results.
 
.PARAMETER Credential
    The credential to access the computer.
 
.PARAMETER BackupID
    Backup ID
 
.PARAMETER FullRepositoryName
    The full repository name is used to construct the PSSession name, which should be distinct for each repository so
    that they can be tracked individually. The pattern is 'AzsBCDRSession-<FullRepostiroyName>'
 
.PARAMETER NodesForCurrentRole
    List of Nodes for the role where the method should look for previously started BCDR remote PSSessions
 
.PARAMETER EmbeddedScriptBlockAsStr
    The script block that contains the codes for the actual execution on the remote machine, passed in as a string.
    The author of the script block must ensure that the codes runs in constrained language mode, otherwise the author
    must manually set full language mode in this script block.
 
.PARAMETER ScriptBlockParameters
    The parameter list of the embedded script block
 
.PARAMETER ProvisionedPSSession
    The provisioned PSSession. The session name must follow this pattern: 'AzsBCDRSession-<FullRepositoryName>'.
    Once the codes in the EmbeddedScriptBlockAsStr is executed as a job in the session, the session will be disconnected
    and the caller should *NOT* try to reconnect to it if a reference is still held.
 
.PARAMETER ExpectedRuntimeInMin
    The method first checks if there were any previously started PSSessions, and if there is any, it finds the start
    time of the execution and poll for results until the run exceeds the expected run time.
 
.PARAMETER ConfigurationName
    The configuration name to start the disconnected session with. If not specified, default to use CredSSP
 
.PARAMETER ActionType
    Specifies whether the action is a backup or restore
#>

function Start-IdempotentBCDRRemoteExecution
{
    param
    (
        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $ComputerName,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [PSCredential] $Credential,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $BackupID,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [string] $FullRepositoryName,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string[]] $NodesForCurrentRole,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$true)]
        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $EmbeddedScriptBlockAsStr,

        [Parameter(Mandatory=$false)]
        [Array] $ScriptBlockParameters,

        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $ProvisionedPSSession = $null,

        [Parameter(ParameterSetName = "ProvisionedPSSessionSet", Mandatory=$true)]
        [ValidateNotNull()]
        [PSCredential] $ProvisionedPSSessionCredential = $null,

        [Parameter(Mandatory=$false)]
        [uint32] $ExpectedRuntimeInMin = 60 * 4,

        [Parameter(ParameterSetName = "DefaultSet", Mandatory=$false)]
        [string] $ConfigurationName = $null,

        [Parameter(Mandatory = $false)]
        [ValidateSet('Backup', 'Restore')]
        [string] $ActionType = 'Backup',

        [Parameter(Mandatory = $false)]
        [string] $BackupSnapshotFolder,

        [Parameter(Mandatory = $false)]
        [string[]] $BackupSnapshotFilters
    )

    try
    {
        $defaultSessionName = "$BCDRSessionNameBase-$FullRepositoryName"
        if ($ProvisionedPSSession)
        {
            if ($ProvisionedPSSession.Name -notlike "$defaultSessionName*")
            {
                throw "Unexpected PSSession name '$($ProvisionedPSSession.Name)'. The session must start with '$defaultSessionName'."
            }

            $sessionName = $ProvisionedPSSession.Name
        }
        else
        {
            $sessionName = $defaultSessionName
        }

        if ($ActionType -eq 'Backup')
        {
            $EventSource = $BCDRBackupEventSource
        }
        else
        {
            $EventSource = $BCDRRestoreEventSource
        }

        $sessionInfoEventRegex = $BackupStartedEventLogPattern -f "([\d\w\-]+)", "(.+)", "([\d\w\-]+)", "(\d+)"

        $shouldInvokeAction = $true
        Check-PreviousBCDRSession -NodesForCurrentRole $NodesForCurrentRole -Credential $Credential -BackupID $BackupID `
            -SessionName $sessionName -SessionInfoEventRegex $sessionInfoEventRegex `
            -ProvisionedPSSession $ProvisionedPSSession -ProvisionedPSSessionCredential $ProvisionedPSSessionCredential `
            -ExpectedRuntimeInMin $ExpectedRuntimeInMin -ShouldInvokeAction ([ref] $shouldInvokeAction)

        if ($shouldInvokeAction -eq $false)
        {
            Trace-Execution "The previous action is done, skip invoking the action again"
            return
        }

        Trace-Execution "Ensure the BCDR event source and log exists"
        Ensure-BCDREventLog -ComputerName $ComputerName -Credential $Credential -EventSource $EventSource

        $BCDREventParams = @{}
        $BCDREventParams.BCDRLogName = $BCDRLogName
        $BCDREventParams.EventSource = $EventSource
        $BCDREventParams.BCDRRemoteActionStartedEventId = $BCDRRemoteActionStartedEventId
        $BCDREventParams.BCDRRemoteActionCompletededEventId = $BCDRRemoteActionCompletededEventId
        $BCDREventParams.BCDRRemoteActionFailedEventId = $BCDRRemoteActionFailedEventId
        $BCDREventParams.BackupStartedEventLogPattern = $BackupStartedEventLogPattern
        $BCDREventParams.BackupCompletedEventLogPattern = $BackupCompletedEventLogPattern
        $BCDREventParams.BackupFailedEventLogPattern = $BackupFailedEventLogPattern

        $BCDRRemoteExecutionScriptBlockParams = @($BCDREventParams, $sessionName, $BackupID, `
            $EmbeddedScriptBlockAsStr, $ScriptBlockParameters, $BackupSnapshotFolder, $BackupSnapshotFilters)

        $BCDRRemoteExecutionScriptBlock = {
            param ($BCDREventParams, $sessionName, $BackupID, $EmbeddedScriptBlockAsStr, $ScriptBlockParameters, $BackupSnapshotFolder, $BackupSnapshotFilters)

            try
            {
                Import-Module OpenUpSession
                Set-FullLanguage
            }
            catch{}

            # Log the shell ID and the PID of the disconnected session
            $instance = Get-WSManInstance -ConnectionURI "http://localhost:5985/wsman" shell -Enumerate `
                | Where-Object Name -eq $sessionName
            $backupStartedEventLogMsg = $BCDREventParams.BackupStartedEventLogPattern -f $BackupID, $sessionName, `
                $instance.ShellId, $instance.ProcessId
            Write-EventLog -LogName $BCDREventParams.BCDRLogName -Source $BCDREventParams.EventSource `
                -EntryType Information -EventId $BCDREventParams.BCDRRemoteActionStartedEventId -Message $backupStartedEventLogMsg

            $sb = [scriptblock]::Create($EmbeddedScriptBlockAsStr)
            $succeeded = $false
            try
            {
                Invoke-Command -ArgumentList $ScriptBlockParameters -ScriptBlock $sb -ErrorAction Stop
                $succeeded = $true
            }
            catch
            {
                $errorRecord = $_
            }
            finally
            {
                $errorMsg = $errorRecord | select * | Out-String

                # Double check the snapshots to adjust the final result
                if ($BackupSnapshotFolder -and ($BackupSnapshotFilters.Count -gt 0))
                {
                    try
                    {
                        $logFileFullPath = Join-Path  "$env:SystemDrive\MASLogs" -ChildPath "$($sessionName)_$($BackupID)_$(Get-Date -Format "yyyyMMdd-HHmmss").log"
                        Start-Transcript -Append -Path $logFileFullPath

                        Write-Verbose "Check snapshots under $BackupSnapshotFolder with $BackupSnapshotFilters finally to adjust result"
                        $snapshotNotFound = $false
                        foreach ($filter in $BackupSnapshotFilters)
                        {
                            $snapshots = Get-ChildItem $BackupSnapshotFolder -Filter $filter -Force
                            if (!$snapshots)
                            {
                                Write-Verbose "Failed to find snapshot with $filter"
                                $succeeded = $false
                                $snapshotNotFound = $true
                                $errorMsg += "`nNo snapshot found under $BackupSnapshotFolder with filter $filter."
                            }
                        }

                        if (!$snapshotNotFound)
                        {
                            Write-Verbose "Succeeded to find all snapshots, adjust result from $succeeded to true"
                            $succeeded = $true
                        }
                    }
                    catch
                    {
                        Write-Verbose "Ignore exception $($_.Exception.Message) when checking snapshots"
                    }
                    finally
                    {
                        Stop-Transcript -ErrorAction Ignore
                    }
                }
                
                if ($succeeded)
                {
                    $backupCompletedEventLogMsg = $BCDREventParams.BackupCompletedEventLogPattern -f $BackupID, $sessionName
                    Write-EventLog -LogName $BCDREventParams.BCDRLogName -Source $BCDREventParams.EventSource `
                        -EntryType Information -EventId $BCDREventParams.BCDRRemoteActionCompletededEventId `
                        -Message $backupCompletedEventLogMsg
                }
                else
                {
                    $backupFailedEventLogMsg = $BCDREventParams.BackupFailedEventLogPattern -f $BackupID, $sessionName, $errorMsg
                    Write-EventLog -LogName $BCDREventParams.BCDRLogName -Source $BCDREventParams.EventSource `
                        -EntryType Error -EventId $BCDREventParams.BCDRRemoteActionFailedEventId `
                        -Message $backupFailedEventLogMsg
                    throw $errorMsg, $errorRecord
                }
            }
        }

        if ($PsCmdlet.ParameterSetName -eq 'ProvisionedPSSessionSet')
        {
            Trace-Execution "Using the provisioned PSSession."
            Trace-Execution "Name: $($ProvisionedPSSession.Name), State: $($ProvisionedPSSession.State)"
            Trace-Execution "Availability: $($ProvisionedPSSession.Availability), ConfigName: $($ProvisionedPSSession.ConfigurationName)"

            # Execute the script block in the provisioned session and disconnect
            #
            # Most cases where the caller passes in a provisioned PSSession is because the session was created with a
            # JEA endpoint and specific language mode is enforced. Therefore, Start-BCDRExecutionOnRemoteComputer must
            # be imported in the provisioned PSSession already and cannot be imported here to support this scenario.
            Invoke-Command -Session $ProvisionedPSSession `
                -ArgumentList @($BCDRRemoteExecutionScriptBlock.ToString(), $BCDRRemoteExecutionScriptBlockParams) `
                -ScriptBlock {
                    param ($BCDRRemoteExecutionScriptBlockAsStr, $BCDRRemoteExecutionScriptBlockParams)
                    Start-BCDRExecutionOnRemoteComputer `
                        -BCDRRemoteExecutionScriptBlockAsStr $BCDRRemoteExecutionScriptBlockAsStr `
                        -ScriptBlockParameters $BCDRRemoteExecutionScriptBlockParams
                }

            Trace-Execution "Disconnecting the remote PSSession."
            Disconnect-PSSession -Session $ProvisionedPSSession -IdleTimeoutSec $BCDRRemoteSessionIdleTimeoutInSec
            Trace-Execution "Session disconnected."
        }
        else
        {
            Trace-Execution "Starting a new disconnected PSSession"

            # Set the idle timeout to 4hrs, which is the same timeout as the entire backup action plan, so that the
            # execution doesn't get killed too soon and the session dies after the timeout expires.
            $BCDRRemoteSessionIdleTimeoutInMs = $BCDRRemoteSessionIdleTimeoutInSec * 1000
            $option = New-PSSessionOption -OutputBufferingMode Drop -IdleTimeout $BCDRRemoteSessionIdleTimeoutInMs

            $invokeParam = @{}
            $invokeParam.ComputerName = $ComputerName
            $invokeParam.Credential = $Credential
            $invokeParam.InDisconnectedSession = $true
            $invokeParam.SessionName = $sessionName
            $invokeParam.SessionOption = $option

            if (![string]::IsNullOrEmpty($ConfigurationName))
            {
                $invokeParam.ConfigurationName = $ConfigurationName
            }
            else
            {
                $invokeParam.Authentication = "Credssp"
            }

            $invokeParam.ArgumentList = $BCDRRemoteExecutionScriptBlockParams
            Invoke-Command @invokeParam -ScriptBlock $BCDRRemoteExecutionScriptBlock
        }

        # Try to get session PID and creation time from the latest log
        $wsPid = $null
        for ($i = 0; $i -lt 3; $i++)
        {
            Trace-Execution "Wait for 10 seconds..."
            Start-Sleep -s 10

            $latestStartLog = Get-BCDRActionEventLog -ComputerName $ComputerName -Credential $Credential `
                -BackupID $BackupID -SessionName $sessionName | ? EventID -eq $BCDRRemoteActionStartedEventId | select -First 1

            Trace-Execution "Latest start event: $($latestStartLog.Message)"
            if ($latestStartLog -and ($latestStartLog.Message -Match $sessionInfoEventRegex))
            {
                if ($ProvisionedPSSession -and ($Matches[3] -ne $ProvisionedPSSession.InstanceId))
                {
                    Trace-Execution "Waiting for the start action plan event."
                    continue
                }

                $wsPid = $Matches[4]
                $logTime = $latestStartLog.TimeGenerated.ToUniversalTime()
                break
            }
        }

        if ($i -eq 3)
        {
            Trace-Execution "Failed to get PSSession info. Wait for 5 min and try to determine if backup is completed."
            $timeout = (Get-Date).AddMinutes(5)
        }

        while ($true)
        {
            # Check for completion
            $log = Get-BCDRActionEventLog -ComputerName $ComputerName -Credential $Credential `
                -BackupID $BackupID -SessionName $sessionName | select -First 1
            if ($log)
            {
                if ($log.EventId -eq $BCDRRemoteActionCompletededEventId)
                {
                    Trace-Execution "$ActionType succeeded."
                    break
                }
                elseif ($log.EventId -eq $BCDRRemoteActionFailedEventId)
                {
                    throw "$ActionType $BackupId failed. Message: $($log.Message)"
                }
            }

            # Make sure the process is still alive
            if ($wsPid)
            {
                $sessionProcExists = Check-BCDRSessionProcess -ComputerName $ComputerName -Credential $Credential `
                    -SessionPid $wsPid -SessionCreationTime $logTime
                if (!$sessionProcExists)
                {
                    throw "The remote PSSession unexpectedly died. $ActionType failed."
                }
            }

            if ($timeout -and ((Get-Date) -gt $timeout))
            {
                throw "Could not determine if the $ActionType is finished or not. Failing the $ActionType."
            }

            Start-Sleep -Seconds 30
        }
    }
    finally
    {
        if ($ProvisionedPSSessionCredential)
        {
            $cleanupCredential = $ProvisionedPSSessionCredential
        }
        else
        {
            $cleanupCredential = $Credential
        }

        if (![string]::IsNullOrEmpty($sessionName))
        {
            $session = Get-PSSession -ComputerName $ComputerName -Credential $cleanupCredential `
                -Name $sessionName -ErrorAction SilentlyContinue
            $session | Remove-PSSession -ErrorAction SilentlyContinue
        }

        try
        {
            $instance = Get-WSManInstance -ConnectionURI "http://$ComputerName`:5985/wsman" shell -Enumerate `
            -cred $cleanupCredential -ErrorAction SilentlyContinue | ? Name -eq $SessionName

            foreach ($instance in $instances)
            {
                Trace-Execution "Removing WSMan objects with shell ID $($instance.ShellId)"
                Remove-WSManInstance -ConnectionURI "http://$ComputerName`:5985/wsman" shell @{ShellID="$($instance.ShellId)"} `
                    -cred $cleanupCredential -ErrorAction SilentlyContinue
            }
        }
        catch
        {
            Trace-Execution "Could not clean up the WSMan instance. Exception: $_"
        }
    }
}

<#
.Synopsis
   Get the first machine in the list where a remote PSSession can be created.
#>

function Get-FirstAvailableMachine
{
    param
    (
        [Parameter(Mandatory=$true)]
        [string[]] $Machines,

        [Parameter(Mandatory=$true)]
        [PSCredential] $Credential,

        [Parameter(Mandatory = $true, ParameterSetName = "JEA")]
        [ValidateNotNullOrEmpty()]
        [string] $ConfigurationName,

        [Parameter(Mandatory = $true, ParameterSetName = "Authentication")]
        [ValidateSet("Default","Basic","Credssp","Digest","Kerberos","Negotiate","NegotiateWithImplicitCredential")]
        [string] $Authentication = "Credssp",

        [Parameter(Mandatory = $false)]
        [uint32] $Retries = 0,

        [Parameter(Mandatory = $false)]
        [uint32] $RetrySleepTimeInSeconds = 5
    )

    $RemoteSession = $null
    $found = $null
    foreach ($Machine in $Machines)
    {
        Trace-Execution "Testing remote PSSession connectivities to $Machine with user $($Credential.UserName)"
        $attempt = 0;
        try
        {
            while ($true)
            {
                $attempt++

                try
                {
                    if ($PSCmdlet.ParameterSetName -eq "JEA")
                    {
                        $session = New-PSSession -ComputerName $Machine -Credential $Credential `
                            -ConfigurationName $ConfigurationName -ErrorAction Stop
                    }
                    else
                    {
                        $session = New-PSSession -ComputerName $Machine -Credential $Credential `
                            -Authentication $Authentication -ErrorAction Stop
                    }
                    break
                }
                catch
                {
                    Trace-Warning "Test connection failed. Exception: $_"
                    if ($attempt -lt $Retries)
                    {
                        Trace-Execution "Attempt $attempt of $Retries."
                        Start-Sleep -s $RetrySleepTimeInSeconds
                        continue
                    }
                    else
                    {
                        throw "Failed to connect to $Machine after $attempt attempts. Exception: $_"
                    }
                }
            }

            $found = $Machine
            break
        }
        catch
        {
            Trace-Warning "Failed to connect to $Machine with exception $($_ | Out-String)"
        }
        finally
        {
            $session | Remove-PSSession -ErrorAction SilentlyContinue
        }
    }

    if ([string]::IsNullOrEmpty($found))
    {
        throw "Could not create a PSSession to any of the specified machines."
    }

    return $found
}

Export-ModuleMember -Function Ensure-BCDREventLog
Export-ModuleMember -Function Get-BCDRActionPlanConst
Export-ModuleMember -Function Get-BCDRActionPlanConsts
Export-ModuleMember -Function Get-FirstAvailableMachine
Export-ModuleMember -Function Start-BCDRExecutionOnRemoteComputer
Export-ModuleMember -Function Start-IdempotentBCDRRemoteExecution

# SIG # Begin signature block
# MIIoKgYJKoZIhvcNAQcCoIIoGzCCKBcCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCDUkI9CuCg+ewEj
# UjnWERS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