Public/ImportBatches.ps1

function Get-TMImportBatch {
    <#
    .SYNOPSIS
        Gets ImportBatches from TransitionManager.
 
    .DESCRIPTION
        Retrieves ImportBatches and optionally filters them to a single
        batch identifier.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER BatchId
        The ImportBatch identifier to retrieve.
 
    .EXAMPLE
        Get-TMImportBatch -BatchId 42
 
        Retrieves the ImportBatch with id `42`.
 
    .NOTES
        Omit `-BatchId` to retrieve the available ImportBatches.
    #>

    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $false)][int]$BatchId
    )

    ## Get Session Configuration
    $TMSession = Get-TMSession $TMSession

    # If there's a batch ID provided, then use /api/importBatch/$BatchId?project=2445
    # If not, list all using /api/importBatch?project=2445
    $api = 'importBatch{0}' -f ($BatchId ? '/' + $BatchId : '')

    $Response = Invoke-TMRestMethod -Api $api -Method Get
    if ( $PSBoundParameters.ContainsKey('BatchId') ) {
        if ( -not $Response.id ) {
            throw "ImportBatch $BatchId not found"
        } else {
            return , $Response
        }
    } else {
        # Do not throw if there are none, just return an empty array
        return , $Response # The comma operator will keep the array format returned from the API
    }
}


function Get-TMImportBatchRecord {
    <#
    .SYNOPSIS
        Gets ImportBatchRecords from TransitionManager.
 
    .DESCRIPTION
        Retrieves record details for a ImportBatch and optionally filters them
        to a specific record identifier.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER BatchId
        The ImportBatch identifier whose records should be retrieved.
 
    .PARAMETER RecordId
        An optional ImportBatchRecord identifier to filter on.
 
    .EXAMPLE
        Get-TMImportBatchRecord -BatchId 42
 
        Retrieves all records for ImportBatch `42`.
 
    .NOTES
        Use `-RecordId` when you need a single record from a known batch.
    #>

    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $true)][int]$BatchId,
        [Parameter(Mandatory = $false)][int]$RecordId
    )
    $TMSession = Get-TMSession $TMSession

    #Honor SSL Settings
    $TMCertSettings = @{SkipCertificateCheck = $TMSession.AllowInsecureSSL }

    ## Construct the server URI
    $Instance = $TMSession.TMServer.Replace('/tdstm', '')
    $instance = $instance.Replace('https://', '')
    $instance = $instance.Replace('http://', '')

    if (-not $RecordId) {
        ## Get a List of the records if no specific record was requested
        $uri = "https://$Instance/tdstm/ws/import/batch/$($BatchId)/records"
    } else {
        ## Get a specific record
        $uri = "https://$Instance/tdstm/ws/import/batch/$($BatchId)/record/$($RecordId)"
    }

    ## Attempt the Request
    try {
        $response = Invoke-WebRequest -Method Get -Uri $uri -WebSession $TMSession.TMWebSession @TMCertSettings
        if ($response.StatusCode -eq 200) {
            $Result = ($response.Content | ConvertFrom-Json).data
        }
        return @($Result)
    } catch {
        throw $_
    }
}

function Start-TMImportBatch {
    <#
    .SYNOPSIS
        Starts ImportBatches in TransitionManager.
 
    .DESCRIPTION
        Submits one or more ImportBatch identifiers for processing.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER BatchId
        One or more ImportBatch identifiers to start.
 
    .EXAMPLE
        Start-TMImportBatch -BatchId 42, 43
 
        Starts processing for the specified ImportBatches.
 
    .NOTES
        Use `Get-TMImportBatch` to identify batch ids before starting them.
    #>

    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $true)][int[]]$BatchId
    )

    ## Get Session Configuration
    $TMSession = Get-TMSession $TMSession

    $bodyParams = @{
        ids     = $BatchId
    }
    $Response = Invoke-TMRestMethod -Api importBatch/queue -Method Patch -bodyParams $bodyParams

    if ($Response.updated -eq $BatchId.Count) {
        if ($BatchId.Count -gt 1) {
            Write-Host "All ($($Response.updated)) ImportBatches were queued"
        } else {
            Write-Host "ImportBatch $BatchId was queued"
        }
    }
    else {
        Write-Host "$($Response.updated) out of $($BatchId.Count) ImportBatchRecords were queued."
    }
}