Public/Tags/Get-PopuliTaggedPeople.ps1

function Get-PopuliTaggedPeople
{
    [CmdletBinding(DefaultParametersetName='TagName', PositionalBinding=$true)]
    param
    (
        [Parameter(ParameterSetName='TagName', Mandatory=$true)][string]$TagName,
        [Parameter(ParameterSetName='TagId', Mandatory=$true)][string]$TagId,
        [Parameter(Mandatory=$false)][ValidateLength(200,999)][string]$PopuliToken = $env:PopuliToken,
        [Parameter(Mandatory=$false)][ValidatePattern('^(?i)https:\/\/\S+\.populiweb\.com\/?$')][string]$BaseUrl = $env:PopuliBaseUrl
    )


    try
    {

        if ($BaseUrl -match '\/$')
        {
            $BaseUrl = $BaseUrl -replace '\/$'
        }

        $header = @{
            Authorization = $PopuliToken
        }

        $body = @{
            task = 'getTaggedPeople'
        }

        if ($PSBoundParameters.Keys -contains 'TagName')
        {
            $body += @{tagName = $TagName}
        }
        elseif ($PSBoundParameters.Keys -contains 'TagId')
        {
            $body += @{tagId = $tagId}
        }
        else 
        {
            throw "-TagName or -TagId must be specified"
        }

        $response = Invoke-RestMethod -Uri "$BaseUrl/api/" -Method POST -ContentType 'application/x-www-form-urlencoded' -Body $body -Headers $header

        $taggedPeople = $response.response.person

        Write-Log "Returning $($taggedPeople.Count) people tagged with tag: ""$TagName$TagId"""

        return $taggedPeople
    }
    catch
    {
        Write-Log "Unhandled exception" -LogType: error -ErrorObject $_
        Write-Error $_

        return $null
    }


}