Public/Training/Get-KB4TrainingCampaign.ps1

<#
.SYNOPSIS
Gets KnowBe4 training campaigns.

.DESCRIPTION
Retrieves training campaigns from the KnowBe4 Reporting API. Without Id, the
command returns a page of campaigns or all campaigns when All is specified.
With Id, the command returns a specific training campaign.

Normal output unwraps KnowBe4 page data and returns campaign objects. Use Raw
to inspect the full response envelope and page wrapper.

.PARAMETER Id
The KnowBe4 training campaign ID. This parameter also accepts the alias
CampaignId.

.PARAMETER ExcludePercentages
Requests that KnowBe4 exclude completion percentage fields from list responses,
which can improve response time.

.PARAMETER All
Retrieves all available pages.

.PARAMETER PageSize
The number of training campaigns to request per page. Valid range is 1 through
500.

.PARAMETER Page
The page number to request when using page-based pagination.

.PARAMETER Cursor
The cursor value to request. Use 'true' to start cursor pagination manually.

.PARAMETER UsePagePagination
Uses page-based pagination when All is specified. Cursor pagination is preferred.

.PARAMETER Raw
Returns the full PSKB4Reporting response envelope instead of unwrapped campaign objects.

.EXAMPLE
Get-KB4TrainingCampaign -ExcludePercentages -PageSize 50

Gets the first page of training campaigns without percentage fields.

.EXAMPLE
Get-KB4TrainingCampaign -Id 12345

Gets a specific training campaign.

.OUTPUTS
PSCustomObject.
#>

function Get-KB4TrainingCampaign
{
    [CmdletBinding(DefaultParameterSetName = 'List')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [Alias('CampaignId')]
        [int] $Id,

        [Parameter(ParameterSetName = 'List')]
        [switch] $ExcludePercentages,

        [Parameter(ParameterSetName = 'List')]
        [switch] $All,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, 500)]
        [int] $PageSize = 100,

        [Parameter(ParameterSetName = 'List')]
        [ValidateRange(1, [int]::MaxValue)]
        [int] $Page,

        [Parameter(ParameterSetName = 'List')]
        [string] $Cursor,

        [Parameter(ParameterSetName = 'List')]
        [switch] $UsePagePagination,

        [Parameter()]
        [switch] $Raw
    )

    process
    {
        if ($PSCmdlet.ParameterSetName -eq 'ById')
        {
            $path = '/v1/training/campaigns/{0}' -f (Format-KB4PathValue -Value $Id)
            return Get-KB4ResponseBody -Response (Invoke-KB4Request -Path $path) -Raw:$Raw
        }

        $query = @{}
        if ($ExcludePercentages)
        {
            $query['exclude_percentages'] = $true
        }

        $pagedParameters = @{
            Path              = '/v1/training/campaigns'
            Query             = $query
            All               = $All
            PageSize          = $PageSize
            UsePagePagination = $UsePagePagination
            Raw               = $Raw
        }
        if ($PSBoundParameters.ContainsKey('Page'))
        {
            $pagedParameters['Page'] = $Page
        }
        if ($PSBoundParameters.ContainsKey('Cursor'))
        {
            $pagedParameters['Cursor'] = $Cursor
        }

        Invoke-KB4PagedRequest @pagedParameters
    }
}