Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Saturday, February 13, 2021

Working with Freshdesk contacts in powershell

Get all freshdesk contacts in Powershell

function Get-LogetoFreshdeskContacts 
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
        [string]$FreshdeskDomain,
        [parameter(Mandatory=$true)]
        $ApiKey
    )

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("$($ApiKey):X")))

    $freshdeskContacts = New-Object System.Collections.ArrayList($null)
    $pagesPerRequest = 100
    $page = 1

    do
    {
        $url = "https://$FreshdeskDomain.freshdesk.com/api/v2/contacts?page=$page&per_page=$pagesPerRequest"
        $items = Invoke-RestMethod -Headers @{Authorization=("Basic $base64AuthInfo")} -Uri $url -Method Get
        $freshdeskContacts.AddRange($items)
        $page++
    } while ($items.Length -ne 0)

    return $freshdeskContacts
}

Add or update freshdesk contact in Powershell

function Set-LogetoFreshdeskContact 
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
        [string]$FreshdeskDomain,
        [parameter(Mandatory=$true)]
        $ApiKey,
        [parameter(Mandatory=$true)]
        $Contact       
    )

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("$($ApiKey):X")))

    $url = "https://$FreshdeskDomain.freshdesk.com/api/v2/contacts"

    if ($Contact.id)
    {
        $method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Put
        $url += "/$($Contact.id)"
        $Contact.Remove("id") ## Avoid Unexpected/invalid field in request
    }
    else 
    {
        $method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Post      
    }

    return Invoke-RestMethod -Headers @{Authorization=("Basic $base64AuthInfo")} -Uri $url -Method $method -Body ($Contact | ConvertTo-Json) -ContentType "application/json; charset=utf-8"
}

Working with Freshdesk contacts in powershell

Get all freshdesk contacts in Powershell function Get-LogetoFreshdeskContacts { [CmdletBinding()] param ( [parameter(Ma...