Public/ImportBatches.ps1

function Get-TMImportBatch {
    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 "Import Batch $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 {
    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 {
    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)) import batches were queued"
        } else {
            Write-Host "Import Batch $BatchId was queued"
        }
    }
    else {
        Write-Host "$($Response.updated) out of $($BatchId.Count) Import Batch records were queued."
    }
}