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
# MIInvwYJKoZIhvcNAQcCoIInsDCCJ6wCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCDUkI9CuCg+ewEj
# UjnWERSej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