Public/Tags/Remove-PopuliPersonTag.ps1

function Remove-PopuliPersonTag
{

    [CmdletBinding(DefaultParametersetName='PersonId_TagName', PositionalBinding=$true)]
    param
    (
        [Parameter(ParameterSetName='PersonId_TagName', Mandatory=$true)]
        [Parameter(ParameterSetName='PersonId_TagId', Mandatory=$true)]
        [string]$PersonId,

        [Parameter(ParameterSetName='OrganizationId_TagName', Mandatory=$true)]
        [Parameter(ParameterSetName='OrganizationId_TagId', Mandatory=$true)]
        [string]$OrganizationId,

        [Parameter(ParameterSetName='PersonId_TagName', Mandatory=$true)]
        [Parameter(ParameterSetName='OrganizationId_TagName', Mandatory=$true)]
        [string]$TagName,

        [Parameter(ParameterSetName='PersonId_TagId', Mandatory=$true)]
        [Parameter(ParameterSetName='OrganizationId_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 = 'removeTag'
        }

        if ($PSBoundParameters.Keys -contains 'PersonId')
        {
            $body += @{person_id = $PersonId}
        }
        elseif ($PSBoundParameters.Keys -contains 'OrganizationId')
        {
            $body += @{organization_id = $OrganizationId}
        }
        else 
        {
            throw "-PersonId or -OrganizationId must be specified"
        }

        if ($PSBoundParameters.Keys -contains 'TagName')
        {
            $body += @{tag = $TagName}
        }
        elseif ($PSBoundParameters.Keys -contains 'TagId')
        {
            $body += @{tag_id = $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

        $removeTagResp = $response.response.result

        Write-Log "Removing Tag ""$TagName$TagId"" from ""$PersonId$OrganizationId"". Response: ""$removeTagResp""" -PersonId $PersonId$OrganizationId

        return $removeTagResp
    }
    catch
    {
        Write-Log "Unhandled exception" -LogType: error -ErrorObject $_ -PersonId $PersonId$OrganizationId

        return $null
    }


}