Created
May 7, 2018 08:34
-
-
Save EitanBlumin/25a7ec113212dbf0de9f806554770fc7 to your computer and use it in GitHub Desktop.
Change Zendesk Ticket Status Using Powershell
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 | |
( | |
[int] $ticketid, | |
[validateset ("new","open","pending","solved","closed","delete","same")] [string] $newstatus, | |
[string] $admincomment = "" | |
) | |
# Global Zendesk Settings: | |
$global:zendesk_address = "https://yourcompany.zendesk.com" | |
# Username (email address) must be followed by /token | |
$global:zendesk_user_name = "[email protected]/token" | |
# Username (email address) of the admin account | |
$global:admin_user_name = "[email protected]" | |
# Password is the API token | |
$global:zendesk_password = "your_API_token_here" | |
# Function to create authorization header for the API | |
function CreateAuthorizationHeader() | |
{ | |
$pair = "$($zendesk_user_name):$($zendesk_password)" | |
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) | |
$basicAuthValue = "Basic $encodedCreds" | |
$Headers = @{ | |
Authorization = $basicAuthValue | |
} | |
return $Headers | |
} | |
$header = CreateAuthorizationHeader | |
# find admin account | |
$uri = $zendesk_address + '/api/v2/search.json?query=type:user email:' + $global:admin_user_name | |
$adminuser = Invoke-RestMethod -Method Get -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header | |
if ($adminuser.results.Count -ne 1) | |
{ | |
throw "Invalid admin account " + $global:admin_user_name | |
} | |
# find existing ticket | |
$uri = $zendesk_address + '/api/v2/tickets/' + $ticketid.ToString() + '.json' | |
$ticket = Invoke-RestMethod -Method Get -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header | |
# delete ticket | |
if ($newstatus -eq "delete") | |
{ | |
if ($admincomment -ne "") | |
{ | |
$admincomment = '"comment": { "body": "' + $admincomment + '", "html_body": "' + $admincomment + '", "author_id": ' + $adminuser.results[0].id + ' }' | |
$ticketbody = '{"ticket": {' + $admincomment + '} }' | |
$response = Invoke-RestMethod -Method Put -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header -Body $ticketbody | |
} | |
$response = Invoke-RestMethod -Method Delete -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header | |
} | |
# udpate ticket | |
elseif ($ticket.ticket.status -ne $newstatus -or ($newstatus -eq "same" -and $admincomment -ne "") ) | |
{ | |
# prepare admin comment | |
if ($admincomment -ne "") | |
{ | |
$admincomment = ', "comment": { "body": "' + $admincomment + '", "html_body": "' + $admincomment + '", "author_id": ' + $adminuser.results[0].id + ' }' | |
} | |
if ($newstatus -eq "same") | |
{ | |
$newstatus = $ticket.ticket.status | |
} | |
$ticketbody = '{"ticket": {"status": "' + $newstatus + '"' + $admincomment + '} }' | |
$response = Invoke-RestMethod -Method Put -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header -Body $ticketbody | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment