Wait-IpsJob.ps1

<#
.SYNOPSIS
Wait for an Image Portability Service job to complete.

.DESCRIPTION
Waits for an Image Portability Service (IPS) job to complete.

.PARAMETER CustomerId
Specifies the customer id of the Citrix customer running this command.

.PARAMETER SecureClientId
Specifies the client id of the Citrix customer's API client.

.PARAMETER SecureSecret
Specifies the client secret of the Citrix customer's API client.

.PARAMETER JobId
Specifies the id of the job to wait for.

.PARAMETER PollSeconds
Specifies the interval between polls for the job's status.

.PARAMETER Deployment
Specifies the service address to send the job request to. It defaults to api.layering.cloud.com. This can be used if necessary to send the request to a geo specific deployment such as api.eu.layering.cloud.com.

.PARAMETER LogFileDir
Specifies the path to the file to log to. The local directory is the default.

.PARAMETER LogFileName
Specifies the name of the file to log to.

.PARAMETER OverwriteLog
If specified the log file is overwritten otherwise it is appended to.

.INPUTS
PSCustomObject. The output from one of the IPS job starting cmdlets.

.OUTPUTS
PSCustomObject. Information about the job including the job's status.

.EXAMPLE
PS> Start-IpsAzurePrepareJob ... | Wait-IpsJob

Wait for an IPS job using the output from the command which started the job.

.EXAMPLE
PS> $CitrixCreds = @{
    CustomerId = 'a7f4wb1example'
    SecureClientId = '7fed2a1e-1495-46b7-8fd3-5644764af395'
    SecureSecret = '9T.3Q~MGlnB6NNgpNUUWrcquVzODrdGK~eXampLe'
}
PS> Wait-IpsJob @CitrixCreds -JobId fb067a8a-b20a-4889-ba3e-1ec580edd9c5

Wait for an IPS job explicitly providing the Citrix customer credentials and the job id.
#>


Function Wait-IpsJob
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$CustomerId,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$SecureClientId,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$SecureSecret,
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$JobId,
        [Parameter(Mandatory = $false)]
        [int]$PollSeconds = 60,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$Deployment,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$LogFileDir,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$LogFileName = 'WaitJob.log',
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [switch]$OverwriteLog
    )
    Begin
    {
        Add-PSSnapin Citrix.*
    }
    Process
    {
        try
        {
            LogInit $MyInvocation $LogFileDir $LogFileName $OverwriteLog $False

            if ($JobId -eq  "Job failed to start") {
                return
            }
            if ([string]::IsNullOrWhiteSpace($JobId)) {
                throw "Must supply a job id"
            }

            $parameters = AuthToCitrixCloud $CustomerId $SecureClientId $SecureSecret
            if ([string]::IsNullOrWhiteSpace($SecureClientId) -Or [string]::IsNullOrWhiteSpace($SecureSecret)) {
                $SecureClientId = $parameters.ApiKey
                $SecureSecret = $parameters.SecretKey
            }

            # Start waiting for job
            $job = $null
            $terminated = $false
            while (-Not $terminated)
            {
                # Send the GET
                try {
                    $job = Invoke-CCRestMethod 'Get' $Deployment "jobs/$JobId" $CustomerId $SecureClientId $SecureSecret @{} $null
                    LogIt "Job $JobId status: $($job.status) percent done: $($job.overallProgressPercent) parameters $($job.parameters)"
                } catch {
                    LogIt "GET jobs/$JobId status error: $($_)"
                    return New-Object PSObject -Property @{
                        status = "Failed"
                        error = $_
                        additionalInfo = @{
                            artifacts = @{}
                        }
                    }
                }
                if ('notStarted','inProgress' -contains $job.status) {
                    Start-Sleep $PollSeconds
                } else {
                    $terminated = $true
                }
            }
            $jobHash = Convert-ObjectToHashtable $job
            if ($job.status -eq "complete") {
                LogIt "Job $JobId final status: $($job.status)"
                LogIt "Completed job $(Convert-HashToString $jobHash) " $false
                LogIt "Job profile $(Convert-HashToString $jobHash.additionalInfo.profile))"
                if ($jobHash.additionalInfo.warnings.Length -gt 0)
                {
                    LogIt "Job warnings $(Convert-ArrayToString $jobHash.additionalInfo.warnings))"
                }
            } else {
                LogIt "Job $JobId final status: $(Convert-HashToString $jobHash) "
                throw "Final job $JobId status: $($job.status)"
            }

            $output = [PSCustomObject]@{
                Artifacts = $job.additionalInfo.artifacts
                Status = $job.status
                LogFileDir = $LogFileDir
                LogFileName = $LogFileName
            }
            Write-Output $output
        }
        finally
        {
            # Clear credentials at end of pipeline
            if ($PSCmdlet.MyInvocation.PipelinePosition -eq $PSCmdlet.MyInvocation.PipelineLength) {
                Clear-XDCredentials
            }
        }
    }
}