Wait-QlikExecution.ps1

function Wait-QlikExecution {
    <#
    .SYNOPSIS
    This function ...
 
    .DESCRIPTION
    A bit more description
 
    .PARAMETER FromPipeline
    Shows how to process input from the pipeline, remaining parameters or by named parameter.
 
    .EXAMPLE
    Wait-QlikExecution 'abc'
 
    Description of the example.
 
    #>


    <# Enable -Confirm and -WhatIf. #>
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
         [parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,Position=0,ParameterSetName="Execution")]
    [alias("value")]
    [string]$executionId,

    [parameter(Mandatory=$true,ValueFromPipelinebyPropertyName=$True,Position=0,ParameterSetName="Task")]
    [alias("id")]
    [string]$taskId
    )

    begin {
    }

    process {
    if ($executionId)
    {
      $execution = Invoke-QlikGet "/qrs/executionSession/$executionId"
      $resultId = $execution.executionResult.Id
      $taskName = $execution.reloadTask.name
    }
    else
    {
      $task = Invoke-QlikGet "/qrs/reloadTask/$taskId"
      $resultId = $task.operational.lastExecutionResult.id
      $taskName = $task.name
    }
    do {
        # Get task status
        $rawOutput = $true
        $result = Invoke-QlikGet "/qrs/executionResult/$resultId"

        # Get internal task status code
        $taskstatuscode = $result.status

        $result = FormatOutput($result)
        Write-Progress -Activity $taskName -Status $result.status -CurrentOperation ($result.details | select -Last 1).message

        # Wait for 1 second, in a Production setting this should be set much higher to avoid stressing the QRS API
        Start-Sleep -Seconds 1

    } until ($taskstatuscode -gt 3) #status code of more than 3 is a completion (both success and fail)
    return $result
  }

    end {
    }
}

if ($loadingModule) {
    Export-ModuleMember -Function 'Wait-QlikExecution'
}