Private/Write-MITOutput.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function Write-MITOutput
{
    [CmdletBinding()]
    param (
        #Response from Invoke-RESTMethod. Might include paging information
        [psobject]$Response,
        #Typename to change output objects to for formatting
        [string]$Typename
    )

    #Determine if the response contains paging information and, if so, if it should be display.
    if (($Response.psobject.properties['paging']) -and ($Response.paging.totalPages -gt 1)) {
        Write-Host -NoNewLine -ForegroundColor Green "Total items: $($Response.paging.totalItems) "
        Write-Host -NoNewLine -ForegroundColor Green "Items per page: $($Response.paging.perPage) "
        Write-Host -ForeGroundColor Green "Displaying Page: $($Response.paging.page) of $($Response.paging.totalPages)"
        $results = $Response.items
    }
    elseif ($Response.psobject.properties['items']) {
        $results = $Response.items
    }
    else {
        $results = $Response
    }

    foreach ($r in $results) {
        $r.PSOBject.TypeNames.Insert(0,$TypeName)
    }

    Write-Output $results
}