Created
September 20, 2017 19:36
-
-
Save jdmills-edu/b61f0077984811347cf0f86478140033 to your computer and use it in GitHub Desktop.
A PowerShell script that posts a comment (public or private) to a Zendesk ticket. Supports rich and HTML comments and author impersonation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)][Int32]$ticketID, | |
[Parameter(Mandatory=$true)][String]$body, | |
[Parameter(Mandatory=$true)][ValidateSet('rich', 'html')][String]$body_type, | |
[Parameter(Mandatory=$true)][ValidateSet('private', 'public')][string]$scope, | |
[String][ValidateSet('assignee', 'apiuser')]$author | |
) | |
if($body_type -eq "html"){ | |
$zd_body_type = "html_body" | |
} | |
elseif(($body_type -eq "rich") -or (!$body_type)){ | |
$zd_body_type = "body" | |
} | |
if($scope -eq "public"){ | |
$public = "True" | |
} | |
elseif($scope -eq "private"){ | |
$public = "False" | |
} | |
#Reference to another Zendesk script, see comments in the Gist. | |
$ticket = &ZendeskAPI-GetTicket.ps1 $ticketID | |
$assignee_id = $ticket.assignee_id | |
#Stupid hack to replace special character. | |
$body = $body.trim().replace("\","/").replace("`n","\n").replace("`r","\n").replace(" ","").replace(" ","").replace("`"","`'").replace("*","").Replace("'","'") | |
#Zendesk API Connection Headers Referencing System Environmental Variables for username and API token. | |
$headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($env:ZendeskAPI_Username):$($env:ZendeskAPI_Token)"));} | |
#Zendesk API: Update Ticket | |
$uri = "https://yourdomain.zendesk.com/api/v2/tickets/$ticketID.json" | |
if($author -eq "assignee"){ | |
$json = '{ | |
"ticket": { | |
"comment": { "'+$zd_body_type+'": "'+$body+'", "public": "'+$public+'", "author_id": "'+$assignee_id+'" } | |
} | |
}' | |
} | |
else { | |
$json = '{ | |
"ticket": { | |
"comment": { "'+$zd_body_type+'": "'+$body+'", "public": "'+$public+'" } | |
} | |
}' | |
} | |
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -ContentType "application/json" -Body $json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ZendeskAPI-GetTicket.ps1 (referenced in the script above).