Skip to content

Instantly share code, notes, and snippets.

@EitanBlumin
Created August 22, 2019 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EitanBlumin/7475cbdc79997c06a2d71c7906224e79 to your computer and use it in GitHub Desktop.
Save EitanBlumin/7475cbdc79997c06a2d71c7906224e79 to your computer and use it in GitHub Desktop.
Powershell script to open Zendesk support tickets, or add comment to an existing ticket (based on requester and subject). Useful for automatic monitoring and alerting systems.
param
(
[string] $Subject = "This is a test ticket from Powershell",
[string] $Body = "This is test description",
[string] $RequesterEmail = "dbmonitor@madeiradata.com",
[string] $SourceServer = "SqlDev2016",
[string] $SQLVersion = "2016",
[string] $SQLEdition = "Developer",
[string] $Priority = "low",
[string] $StartDate = "2018-04-17 23:30",
[string] $Severity = "2"
)
# Global Zendesk Settings:
$global:zendesk_address = "https://your_sub_domain.zendesk.com"
# Username (email address) must be followed by /token
$global:zendesk_user_name = "submitter_account_email/token"
# Password is the API token
$global:zendesk_password = "your_api_token_here"
# Custom Field IDs:
$StartDateFieldID = "360003783873"
$SQLVersionFieldID = "360003784933"
$SQLEditionFieldID = "360003857974"
$SQLMachineFieldID = "360003763113"
# Convert internal Severity to Zendesk Priority
if ($Severity -ne "")
{
$Priority = switch($Severity)
{
{($_ -eq "0" -or $_ -eq "1")} {"low"}
{($_ -eq "2")} {"normal"}
{($_ -eq "3")} {"high"}
{($_ -eq "4")} {"urgent"}
default { "normal" }
}
}
# 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
# use Zendesk's Search API to narrow down amount of results and to reduce network traffic:
$uri = $zendesk_address + '/api/v2/search.json?query=type:ticket status<solved requester:"' + $RequesterEmail + '"'
$tickets = Invoke-RestMethod -Method Get -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header
# check if alert already exists as a ticket:
$statuses = "new", "open", "pending", "onhold"
$ticket = $tickets.results | Where-Object {$_.subject -eq $Subject -and $_.description -eq $Body -and $statuses -contains $_.status}
if (!$ticket)
{
# ticket doesn't already exist. create new and get its info in return:
$ticketbody = '{"ticket": {"subject": "' + $Subject + '", "comment": { "body": "' + $Body + '" }, "type" : "incident", "priority" : "' + $Priority + '", "requester": { "locale_id": 8, "name": "' + $RequesterEmail + '", "email": "' + $RequesterEmail + '" }'
$ticketbody = $ticketbody + ', "custom_fields": [{ "id": ' + $StartDateFieldID + ', "value": "' + $StartDate + '" }, { "id": ' + $SQLMachineFieldID + ', "value": "' + $SourceServer + '" }, { "id": ' + $SQLEditionFieldID + ', "value": "' + $SQLEdition + '" }, { "id": ' + $SQLVersionFieldID + ', "value": "' + $SQLVersion + '" }]'
$ticketbody = $ticketbody + ' }}'
$ticket = Invoke-RestMethod -Method Post -UseBasicParsing -ContentType "application/json" -Uri "$zendesk_address/api/v2/tickets.json" -Headers $header -Body $ticketbody
$ticket = $ticket.ticket
}
else
{
# alert already exists as a ticket. add comment to indicate recurrence:
$uri = $ticket.url
$commentbody = '{"ticket": {"comment": { "body": "The alert has just fired again (' + $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss')) + '). This is an automated message.", "author_id": ' + $ticket.submitter_id + ' }}}'
$comment = Invoke-RestMethod -Method Put -UseBasicParsing -ContentType "application/json" -Uri $uri -Headers $header -Body $commentbody
}
# output the relevant Zendesk ticket id:
"ticket_id: " + $ticket.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment