Public/Tags/Add-PopuliPersonTag.ps1
function Add-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 = 'addTag' } 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 $addTagResp = $response.response.result Write-Log "Adding Tag ""$TagName$TagId"" to ""$PersonId$OrganizationId"". Response: ""$addTagResp""" -PersonId $PersonId$OrganizationId return $addTagResp } catch { Write-Log "Unhandled exception" -LogType: error -ErrorObject $_ -PersonId $PersonId$OrganizationId Write-Error $_ return $null } } |