Public/Training/Get-KB4TrainingPolicy.ps1

<#
.SYNOPSIS
Gets KnowBe4 training policies.

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

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

.PARAMETER Id
The KnowBe4 training policy ID. This parameter also accepts the alias PolicyId.

.PARAMETER All
Retrieves all available pages.

.PARAMETER PageSize
The number of policies 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 policy objects.

.EXAMPLE
Get-KB4TrainingPolicy -PageSize 50

Gets the first page of training policies.

.EXAMPLE
Get-KB4TrainingPolicy -Id 12345

Gets a specific training policy.

.OUTPUTS
PSCustomObject.
#>

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

        [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/policies/{0}' -f (Format-KB4PathValue -Value $Id)
            return Get-KB4ResponseBody -Response (Invoke-KB4Request -Path $path) -Raw:$Raw
        }

        $pagedParameters = @{
            Path              = '/v1/training/policies'
            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
    }
}