Functions/Test-MSPCompleteTaskShouldSkip.ps1

<#
.SYNOPSIS
    This function checks if the current executing MSPComplete task should be skipped.
.DESCRIPTION
    This function checks if the current executing MSPComplete task should be skipped.
    It does so by:
        - Checking if 'SKIP_MSPC_TASK' is in any of the task string inputs (case insensitive)
        - Checking if the entry in $mspc.Data for 'SKIP_MSPC_TASK_taskInstanceId' is true
    The check is only done if the BitTitan runbook environment is not set to running on the local
    machine (i.e. running on the MSPComplete platform)
#>

function Test-MSPCompleteTaskShouldSkip {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([Bool])]
    param (
        # Specify if 'SKIP_MSPC_TASK' is to be checked
        [Parameter(Mandatory=$false)]
        [ValidateNotNull()]
        [Switch]$checkSkipMSPCTask = [Switch]::Present,

        # Specify if $mspc.Data is to be checked
        [Parameter(Mandatory=$false)]
        [ValidateNotNull()]
        [Switch]$checkMSPCData = [Switch]::Present
    )

    # Retrieve the BitTitan runbook environment
    $btRunbookEnvironment = Get-BT_RunbookEnvironment

    # Check if this task is running on the local machine
    if ($btRunbookEnvironment.IsRunningOnLocalMachine) {
        return $false
    }

    # Check if 'SKIP_MSPC_TASK' is in any of the task string inputs
    if ($checkSkipMSPCTask) {
        if (Find-StringInMSPCompleteTaskInputs -SearchString "SKIP_MSPC_TASK" -CaseSensitive:$false) {
            Write-Information "This task has been directed to be skipped using 'SKIP_MSPC_TASK' in the task string inputs."
            return $true
        }
    }

    # Check if the entry in $mspc.Data is true
    if ($checkMSPCData) {
        if ($mspc.Data."SKIP_MSPC_TASK_$($mspc.AutomationInstanceId)" -eq "true") {
            Write-Information "This task has been directed to be skipped using Task Execution Control."
            return $true
        }
    }

    # This task will not be skipped
    return $false
}