Created
July 24, 2017 13:27
-
-
Save TomerAdmon/c9b1a6f3181c63b5d8b1b1eaf1dcb549 to your computer and use it in GitHub Desktop.
PowerShell script that query Zendesk using REST API to find all tickets that some users were involved in since a specific date.
This file contains 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
$user_names = "My User", "Another User" | |
$search_since = "2017-03-01" | |
$global:zendesk_user_name = "username" | |
$global:zendesk_password = "password" | |
$global:zendesk_address = "zendesk_address" | |
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 | |
} | |
function GetUserIdForUsers($users) | |
{ | |
$header = CreateAuthorizationHeader | |
$users_objects = @() | |
foreach ($user in $users) | |
{ | |
$user_info = Invoke-RestMethod -Uri "$zendesk_address/api/v2/users/autocomplete.json?name=$user" -Headers $header | |
$number_of_users = $user_info.users.Count | |
if ($number_of_users -gt 1) | |
{ | |
write-host "User: $user found $number_of_users times in the zendesk portal" | |
foreach ($u in $user_info.users) | |
{ | |
write-host "Name:" $u.name ", Email: " $u.email ", id: " $u.id -ForegroundColor Magenta | |
} | |
$use_all_users = Read-Host "Do you want to use all users for the query? (yes / no)" | |
if ($use_all_users -match "yes") | |
{ | |
foreach ($u in $user_info.users) | |
{ | |
$properties = @{'username'=$user_info.users[0].name; | |
'id'=$user_info.users[0].id; | |
'email'=$user_info.users[0].email} | |
$new_user = New-Object psobject -Property $properties | |
$users_objects += $new_user | |
} | |
} | |
else | |
{ | |
throw "There is more then one user with the same name!" | |
} | |
} | |
else | |
{ | |
$properties = @{'username'=$user_info.users[0].name; | |
'id'=$user_info.users[0].id; | |
'email'=$user_info.users[0].email} | |
$new_user = New-Object psobject –Prop $properties | |
$users_objects += $new_user | |
} | |
} | |
return $users_objects | |
} | |
function GetTickets($search_since, $next_page) | |
{ | |
$result = @() | |
$header = CreateAuthorizationHeader | |
$tickets = Invoke-RestMethod -Uri "$zendesk_address/api/v2/search.json?query=type:ticket created>$search_since custom_field_21803188:False" -Headers $header | |
Write-Host "HI! Found" $tickets.count "tickets in the provided period" -ForegroundColor Yellow | |
$result += $tickets.results | |
while ($tickets.next_page -ne $null) | |
{ | |
$tickets = Invoke-RestMethod -Uri $tickets.next_page -Headers $header | |
$result += $tickets.results | |
} | |
return $result | |
} | |
function GetTicketAudits($ticketId) | |
{ | |
$header = CreateAuthorizationHeader | |
$ticket_information = Invoke-RestMethod -Uri "$zendesk_address/api/v2/tickets/$ticketId/audits.json" -Headers $header | |
return $ticket_information.audits | select -Property author_id | Select-Object -ExpandProperty author_id | select -uniq | |
} | |
function execute(){ | |
$user_ids = GetUserIdForUsers $user_names | |
$all_tickets = GetTickets $search_since | |
$proceed = Read-Host "would you like to proceed? (yes / no)" | |
if ($proceed -ne "yes") {throw "stopped by the user..."} | |
$counter = 1 | |
$results = @() | |
foreach ($ticket in $all_tickets) | |
{ | |
$precentage = [math]::Round(($counter/$all_tickets.Count)*100) | |
$counter++ | |
Write-Progress -Activity "Search in Progress" -Status "$precentage% Complete:" -PercentComplete $precentage | |
$audits = GetTicketAudits $ticket.id | |
foreach ($audit in $audits){ | |
foreach ($user in $user_ids) | |
{ | |
if ($audit -eq $user.id) | |
{ | |
$properties = @{'ticket_id'=$ticket.id; | |
'ticket_url'=$ticket.url; | |
'ticket_subject'=$ticket.subject; | |
'username'=$user.username; | |
'id'= $user.id; | |
'email'= $user.email} | |
$record = New-Object psobject -Property $properties | |
$results += $record | |
write-host "match!" $ticket.id ", user: " $user.username | |
} | |
} | |
} | |
} | |
return $results | |
} | |
execute | Export-Csv -Path "c:\a\results.csv" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment