Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. $user_names = "My User", "Another User"
  2. $search_since = "2017-03-01"
  3.  
  4. $global:zendesk_user_name = "username"
  5. $global:zendesk_password = "password"
  6. $global:zendesk_address = "zendesk_address"
  7.  
  8. function CreateAuthorizationHeader()
  9. {
  10. $pair = "$($zendesk_user_name):$($zendesk_password)"
  11.  
  12. $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
  13.  
  14. $basicAuthValue = "Basic $encodedCreds"
  15.  
  16. $Headers = @{
  17. Authorization = $basicAuthValue
  18. }
  19.  
  20. return $Headers
  21. }
  22.  
  23. function GetUserIdForUsers($users)
  24. {
  25. $header = CreateAuthorizationHeader
  26. $users_objects = @()
  27. foreach ($user in $users)
  28. {
  29. $user_info = Invoke-RestMethod -Uri "$zendesk_address/api/v2/users/autocomplete.json?name=$user" -Headers $header
  30. $number_of_users = $user_info.users.Count
  31. if ($number_of_users -gt 1)
  32. {
  33. write-host "User: $user found $number_of_users times in the zendesk portal"
  34. foreach ($u in $user_info.users)
  35. {
  36. write-host "Name:" $u.name ", Email: " $u.email ", id: " $u.id -ForegroundColor Magenta
  37. }
  38. $use_all_users = Read-Host "Do you want to use all users for the query? (yes / no)"
  39. if ($use_all_users -match "yes")
  40. {
  41. foreach ($u in $user_info.users)
  42. {
  43. $properties = @{'username'=$user_info.users[0].name;
  44. 'id'=$user_info.users[0].id;
  45. 'email'=$user_info.users[0].email}
  46. $new_user = New-Object psobject -Property $properties
  47. $users_objects += $new_user
  48. }
  49. }
  50. else
  51. {
  52. throw "There is more then one user with the same name!"
  53. }
  54. }
  55. else
  56. {
  57. $properties = @{'username'=$user_info.users[0].name;
  58. 'id'=$user_info.users[0].id;
  59. 'email'=$user_info.users[0].email}
  60. $new_user = New-Object psobject –Prop $properties
  61. $users_objects += $new_user
  62. }
  63. }
  64. return $users_objects
  65.  
  66. }
  67.  
  68. function GetTickets($search_since, $next_page)
  69. {
  70. $result = @()
  71. $header = CreateAuthorizationHeader
  72. $tickets = Invoke-RestMethod -Uri "$zendesk_address/api/v2/search.json?query=type:ticket created>$search_since custom_field_21803188:False" -Headers $header
  73. Write-Host "HI! Found" $tickets.count "tickets in the provided period" -ForegroundColor Yellow
  74. $result += $tickets.results
  75.  
  76. while ($tickets.next_page -ne $null)
  77. {
  78. $tickets = Invoke-RestMethod -Uri $tickets.next_page -Headers $header
  79. $result += $tickets.results
  80. }
  81.  
  82. return $result
  83. }
  84.  
  85. function GetTicketAudits($ticketId)
  86. {
  87. $header = CreateAuthorizationHeader
  88. $ticket_information = Invoke-RestMethod -Uri "$zendesk_address/api/v2/tickets/$ticketId/audits.json" -Headers $header
  89. return $ticket_information.audits | select -Property author_id | Select-Object -ExpandProperty author_id | select -uniq
  90. }
  91.  
  92. function execute(){
  93. $user_ids = GetUserIdForUsers $user_names
  94. $all_tickets = GetTickets $search_since
  95.  
  96. $proceed = Read-Host "would you like to proceed? (yes / no)"
  97. if ($proceed -ne "yes") {throw "stopped by the user..."}
  98.  
  99. $counter = 1
  100. $results = @()
  101. foreach ($ticket in $all_tickets)
  102. {
  103. $precentage = [math]::Round(($counter/$all_tickets.Count)*100)
  104. $counter++
  105. Write-Progress -Activity "Search in Progress" -Status "$precentage% Complete:" -PercentComplete $precentage
  106. $audits = GetTicketAudits $ticket.id
  107. foreach ($audit in $audits){
  108. foreach ($user in $user_ids)
  109. {
  110. if ($audit -eq $user.id)
  111. {
  112. $properties = @{'ticket_id'=$ticket.id;
  113. 'ticket_url'=$ticket.url;
  114. 'ticket_subject'=$ticket.subject;
  115. 'username'=$user.username;
  116. 'id'= $user.id;
  117. 'email'= $user.email}
  118. $record = New-Object psobject -Property $properties
  119. $results += $record
  120. write-host "match!" $ticket.id ", user: " $user.username
  121. }
  122. }
  123. }
  124. }
  125. return $results
  126. }
  127.  
  128. execute | Export-Csv -Path "c:\a\results.csv"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement