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 $RestSplat = @{ # If there's a batch ID provided, then use /api/importBatch/$BatchId?project=2445 # If not, list all using /api/importBatch?project=2445 Uri = 'https://{0}/tdstm/api/importBatch{1}?project={2}' -f $TMSession.TMServer, ($BatchId ? '/' + $BatchId : ''), $TMSession.UserContext.Project.Id Method = 'GET' WebSession = $TMSession.TMRestSession SkipHttpErrorCheck = $true StatusCodeVariable = 'StatusCode' } try { $Response = Invoke-RestMethod @RestSplat if ($StatusCode -eq 200) { 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 } } elseif (-not [String]::IsNullOrWhiteSpace($Response.ToString())) { throw "Error getting Import Batch$($BatchId ? " # $BatchId" : 'es'): $Response" } else { throw "The response status code $StatusCode does not indicate success." } } catch { throw "Error getting Import Batch$($BatchId ? " # $BatchId" : 'es'): $_" } } 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 $RestSplat = @{ # If there's a batch ID provided, then use /api/importBatch/$BatchId?project=2445 # If not, list all using /api/importBatch?project=2445 Uri = 'https://{0}/tdstm/api/importBatch/queue' -f $TMSession.TMServer Method = 'PATCH' WebSession = $TMSession.TMRestSession SkipHttpErrorCheck = $true StatusCodeVariable = 'StatusCode' Body = ( @{ ids = $BatchId project = $TMSession.UserContext.Project.Id } | ConvertTo-Json -Compress ) } try { $Response = Invoke-RestMethod @RestSplat if ($StatusCode -eq 200) { 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." } return } else { throw "The response status code $StatusCode does not indicate success: $Response" } } catch { throw "Error while starting import batches ""$($BatchId -join ', ')"": $_" } } |