FreshDeskPowerShell-20221225-Beta.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 8bf18584-bc3e-4cde-b9f6-695084a4a009 .AUTHOR Leonardo Jiang .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Read #> Param() #Contact the author leonardo.jiang@qima.com for any tech questions. #With this module, you can connect yourself to IT FreshDesk and manage your tickets and contacts. #Credentials can be persisted using https://www.powershellgallery.com/packages/CredentialsManager/ by setting the parameter UseCredentialsManager to $True. #$Credential -eq $Null -and $UseCredentialsManager -eq $Null -> credentials are requested and are not persisted #$Credential -ne $Null -and $UseCredentialsManager -eq $Null -> passed credentials are used and not persisted #$Credential -ne $Null -and $UseCredentialsManager -ne $Null -> passed credentials are used and persisted #$Credential -eq $Null -and $UseCredentialsManager -ne $Null -> Persisted credentials are used #PARAMETER UseCredentialsManager: If $True, credentials are read and written using the CredentialsManager. Only Password part, containing the APIKey, is used. #Define the API URL $baseUrl = "https://qima.freshdesk.com/api/v2" #Connect to FreshDesk Function Connect-FD { [CmdletBinding()] Param( [PSCredential]$Credential, [Boolean]$UseCredentialsManager = $False ) #Check module prerequisites If($UseCredentialsManager) { $module = Get-Module -ListAvailable -Name "CredentialsManager" If (!$module) { Throw "Module 'CredentialsManager' needed. Please install executing 'Install-Module -Name CredentialsManager' as Administrator." } } If ($UseCredentialsManager -and $Credential -eq $Null) { $Credential = Read-Credential -ListAvailable | Where-Object {$_.Environment -eq "Freshdesk"} } If(!$Credential) { $Credential = Get-Credential -Message "Please enter API Key for Freshdesk as username (password can be anything)." } If($UseCredentialsManager) { Write-Credential "Freshdesk" -Credential $Credential } $script:base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Credential.UserName,$Credential.Password))) } Function New-HttpQueryString { [CmdletBinding()] Param( [Parameter(Mandatory = $true)][String]$Uri, [Parameter(Mandatory = $true)][Hashtable]$QueryParameter ) #Add System.Web Add-Type -AssemblyName System.Web #Create a http name value collection from an empty string $nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) Foreach ($key in $QueryParameter.Keys) { $nvCollection.Add($key, $QueryParameter.$key) } #Build the uri $uriRequest = [System.UriBuilder]$uri $uriRequest.Query = $nvCollection.ToString() Return $uriRequest.Uri.OriginalString } Connect-FD $OutPara = @{} $getticketfields = New-HttpQueryString "${baseUrl}/ticket_fields" $OutPara $TicketFields = Invoke-RestMethod -Uri $getticketfields -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)} $TicketFieldsRequired = $TicketFields | Where-Object {$_.'required_for_agents' -eq $true} #Run this to connect to FDK if not connected. function ConnectTo-FreshDdesk { If(!$script:base64AuthInfo) { Connect-FD -UseCredentialsManager $True } } #Get Tickets by Filter from Freshdesk #PARAMETER FilterName: Name of filter to get tickets for. Function Get-FDTickets { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$TicketID, [string]$FilterName ) ConnectTo-FreshDdesk $parameters = @{} If($Name) { $parameters.Add("filter", $FilterName) } $uri = New-HttpQueryString "${baseUrl}/tickets/$TicketID" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)}; Return $response } #Add a Ticket Function Add-FDTicket { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Subject, [Parameter(Mandatory=$true)][string]$Mail, [Parameter(Mandatory=$true)][string]$User, [Parameter(Mandatory=$true)][string]$Status, [Parameter(Mandatory=$true)][string]$Priority, [Parameter(Mandatory=$true)][string]$Department, [Parameter(Mandatory=$true)][string]$QIMASystem, [Parameter(Mandatory=$true)][string]$Action, [Parameter(Mandatory=$true)][string]$Contact, [Parameter(Mandatory=$true)][string]$PhoneNumber, [Parameter(Mandatory=$true)][string]$ServiceLocation, [string]$Description = "<div></div>", [string]$Type ) DynamicParam { $attributes = New-Object System.Management.Automation.ParameterAttribute $attributes.ParameterSetName ="__AllParameterSets" $attributes.Mandatory = $true $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] $attributeCollection.Add($attributes) $values = ($TicketFieldsRequired | Where-Object {$_.name -eq "cf_category"}).choices $ValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($values) $attributeCollection.Add($ValidateSet) $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("RequestType",[string],$attributeCollection) $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary $paramDictionary.Add("RequestType",$dynParam1) return $paramDictionary } Process { ConnectTo-FreshDdesk $parameters = @{} $uri = New-HttpQueryString "${baseUrl}/tickets" $parameters #Also Subject and description should be optional but needed to be passed. So for description we use a defaultvalue of empty html If(!$Description) { $Description = "<div></div>" } $body = @{ subject=$Subject; email=$Mail; description=$Description} If($Type) { $body.Add("type", $Type) } $body.Add("priority", 1) $body.Add("status", 2) $bodyAsJson = ConvertTo-JSON($body) $response = Invoke-RestMethod -Uri $uri -Method Post -Body $bodyAsJson -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)} Return $response } } #Modify a Ticket Function Edit-FDTicket { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][int]$Id, [string]$Mail, [string]$Type ) ConnectTo-FreshDdesk $parameters = @{} $uri = New-HttpQueryString "${baseUrl}/tickets/${Id}" $parameters $body = @{} If($Mail) { $body.Add("email", $Mail) } If($Type) { $body.Add("type", $Type) } $bodyAsJson = ConvertTo-JSON( $body ) $response = Invoke-RestMethod -Uri $uri -Method Put -Body $bodyAsJson -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)}; Return $response } #Get Ticket by ticket ID Function Get-FDTicket { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][int]$Id ) ConnectTo-FreshDdesk $parameters = @{} $uri = New-HttpQueryString "${baseUrl}/tickets/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)}; Return $response } #Get Contact by Id Function Get-FDContact { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Id ) ConnectTo-FreshDdesk $parameters = @{} $uri = New-HttpQueryString "${baseUrl}/contacts/${Id}" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)}; Return $response } #Get Contact by Mail Function Get-FDContactByMail { [CmdletBinding()] Param( [Parameter(Mandatory=$true)][string]$Mail ) ConnectTo-FreshDdesk $parameters = @{"query"="`"email:'${Mail}'`""} $uri = New-HttpQueryString "${baseUrl}/search/contacts" $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)}; If($response.total -eq 0) { Return @() } Return $response.results } function Edit-FDContact { [CmdletBinding()] param ( [Parameter(Mandatory=$true)][string]$Mail, [Parameter(Mandatory=$false)][string]$Name, [Parameter(Mandatory=$false)][string]$Department, [Parameter(Mandatory=$false)][string]$Position, [Parameter(Mandatory=$false)][ValidateSet('True','False')][string]$Active ) process { ConnectTo-FreshDdesk $FindContact = Get-FDContactByMail -Mail $Mail $ContactProfile = "https://qima.freshdesk.com/api/v2/contacts/",$FindContact.Id -join '' $parameters = @{} $uri = New-HttpQueryString $ContactProfile $parameters $body = @{} if ($Firstname) { $body.Add('name',$Name) } if ($Department) { $DI = @{} $DI.Add("department",$Department) $body.Add("custom_fields",$DI) } if ($Position) { $body.Add("job_title",$Position) } if ($Active) { $body.Add('active',$Active) } $bodyAsJson = ConvertTo-Json($body) $response = Invoke-RestMethod -Uri $uri -Method Put -Body $bodyAsJson -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)} Return $response } } function Get-FDAgent { [CmdletBinding()] param ( [Parameter(Mandatory=$true)][string]$Mail ) ConnectTo-FreshDdesk $parameters=@{"term"="${Mail}"} $link = $baseUrl,'/agents/autocomplete?' -join '' $uri = New-HttpQueryString $link $parameters $response = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $script:base64AuthInfo)} Return $response }Function Demo-ScriptFunction {'Demo-ScriptFunction'} Workflow Demo-ScriptWorkflow {'Demo-ScriptWorkflow'} Demo-Demo-ScriptFunction Demo-ScriptWorkflow |