aricade

RTPOSH

Jan 29th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.92 KB | None | 0 0
  1. <#
  2. .Synopsis
  3. Connects to RT Server and returns a Session.
  4. .DESCRIPTION
  5. Connects to RT Server and returns a Session So that other *-RequestTracker* commands can be authenticated when using the -Session paramenter.
  6. .EXAMPLE
  7. $MyRTSession=Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com"
  8. .INPUTS
  9. .OUTPUTS
  10. Microsoft.PowerShell.Commands.WebRequestSession
  11. .NOTES
  12. It gets you authenticated onto RT Server
  13. #>
  14. function Connect-RequestTracker{
  15. [cmdletbinding()]
  16. Param (
  17. [Parameter(
  18. Mandatory = $true,
  19. ValueFromPipeLine = $true,
  20. ValueFromPipelineByPropertyName = $true
  21. )]
  22. [ValidateNotNull()]
  23. [System.Management.Automation.PSCredential]
  24. [System.Management.Automation.Credential()]
  25. $Credential,
  26. [ValidateNotNull()]
  27. [string]$Server,
  28. [string]$VirtualDir="/rt/",
  29. [switch]$NoSSL
  30. )
  31. if ($NoSSL){
  32. $http="http://"
  33. }else{
  34. $http="https://"
  35. }
  36. $LoginPage="$http$Server$VirtualDir"
  37. $r = Invoke-WebRequest -Uri $LoginPage -SessionVariable RTSession -Credential $Credential
  38. $form = $r.Forms[0]
  39. $form.fields['user'] = $Credential.UserName
  40. $form.fields['pass'] = $Credential.GetNetworkCredential().Password
  41.  
  42. $r = Invoke-WebRequest -Uri ($http + $Server + $form.Action) -WebSession $RTSession -Method POST -Body $form.Fields
  43. if ($r.Content -notmatch "<li>Your username or password is incorrect</li>"){
  44. return $RTSession
  45.  
  46. }else{
  47. Write-Error "Server $($http + $Server): Your username or password is incorrect"
  48. }
  49. }
  50.  
  51.  
  52. <#
  53. .Synopsis
  54. GET RT (Request Tracker) Tickets.
  55. .DESCRIPTION
  56. Connects to RT Site with a valid session (From Connect-RequestTracker) and Get's a RT ticket.
  57. .EXAMPLE
  58. Get-RequestTrackerTicket -Session $mysession -Server rt.mydomain.com -Ticket 9507
  59. .INPUTS
  60. Ticket
  61. Session
  62. .OUTPUTS
  63. RT REST API Response
  64. .NOTES
  65. Get's the ticket
  66. #>
  67. Function Get-RequestTrackerTicket{
  68. [cmdletbinding()]
  69. Param (
  70. [Parameter(
  71. Mandatory = $true,
  72. ValueFromPipeLine = $true,
  73. ValueFromPipelineByPropertyName = $true
  74. )]
  75. [ValidateNotNull()]
  76. [Microsoft.PowerShell.Commands.WebRequestSession]
  77. $Session,
  78. [ValidateNotNull()]
  79. [string]$Server,
  80. [string]$VirtualDir="/rt/",
  81. [string]$Ticket,
  82. [switch]$NoSSL
  83. )
  84. if ($NoSSL){
  85. $http="http://"
  86. }else{
  87. $http="https://"
  88. }
  89.  
  90. $uri="https://" + $Server + $VirtualDir + "REST/1.0/ticket/$Ticket/history?format=l"
  91. $R = Invoke-WebRequest -Uri $uri -WebSession $Session
  92. return $R.Content
  93.  
  94. }
  95.  
  96. <#
  97. .Synopsis
  98. Closes RT (Request Tracker) Tickets.
  99. .DESCRIPTION
  100. Connects to RT Site with a valid session (From Connect-RequestTracker) and closes a ticket.
  101. .EXAMPLE
  102. Close-RequestTrackerTicket -Server "rt.mydomian.com" -Session $mysession -Ticket 123
  103. .EXAMPLE
  104. Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com" | Close-RequestTrackerTicket -Server "atlas.orbcomm.com" -Ticket 123
  105. .INPUTS
  106. Microsoft.PowerShell.Commands.WebRequestSession from Connect-RequestTracker.
  107. .OUTPUTS
  108. RT REST API Response
  109. .NOTES
  110. It does nothing but Close Tickets. It does it with stealth. No email NAGS are sent from RT. SIMPLE.
  111. #>
  112. Function Close-RequestTrackerTicket{
  113. [cmdletbinding()]
  114. Param (
  115. [Parameter(
  116. Mandatory = $true,
  117. ValueFromPipeLine = $true,
  118. ValueFromPipelineByPropertyName = $true
  119. )]
  120. [ValidateNotNull()]
  121. [Microsoft.PowerShell.Commands.WebRequestSession]
  122. $Session,
  123. [ValidateNotNull()]
  124. [string]$Server,
  125. [string]$VirtualDir="/rt/",
  126. [string]$Ticket,
  127. [switch]$NoSSL
  128. )
  129. if ($NoSSL){
  130. $http="http://"
  131. }else{
  132. $http="https://"
  133. }
  134. #Close ticket
  135. $content="id: ticket/edit`n"
  136. $content+="Status: resolved"
  137.  
  138. $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/$Ticket/edit"
  139. $R = Invoke-WebRequest -Uri $uri -Body @{content=$content} -Method Post -WebSession $Session
  140. return $R.Content
  141.  
  142. }
  143.  
  144. <#
  145. .Synopsis
  146. New Ticket.
  147. .DESCRIPTION
  148. Connects to RT Site with a valid session (From Connect-RequestTracker) and creates a new ticket.
  149. .EXAMPLE
  150. Close-RequestTrackerTicket -Server "rt.mydomian.com" -Session $mysession -Ticket 123
  151. .EXAMPLE
  152. Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com" | Close-RequestTrackerTicket -Server "atlas.orbcomm.com" -Ticket 123
  153. .INPUTS
  154. Microsoft.PowerShell.Commands.WebRequestSession from Connect-RequestTracker.
  155. .OUTPUTS
  156. RT REST API Response
  157. .NOTES
  158. It does nothing but Close Tickets. It does it with stealth. No email NAGS are sent from RT. SIMPLE.
  159. #>
  160. Function New-RequestTrackerTicket{
  161. [cmdletbinding()]
  162. Param (
  163. [Parameter(
  164. Mandatory = $true,
  165. ValueFromPipeLine = $true,
  166. ValueFromPipelineByPropertyName = $true
  167. )]
  168. [ValidateNotNull()]
  169. [Microsoft.PowerShell.Commands.WebRequestSession]
  170. $Session,
  171. [ValidateNotNull()]
  172. [string]$Server,
  173. [string]$VirtualDir="/rt/",
  174. [Parameter(Mandatory = $true)]
  175. [string]$Queue,
  176. [Parameter(Mandatory = $true)]
  177. [string]$Requestor,
  178. [Parameter(Mandatory = $true)]
  179. [string]$Subject,
  180. [string]$CC,
  181. [string]$AdminCC,
  182. [string]$Owner="Nobody in particular",
  183. [string]$Status,
  184. [string]$Priority,
  185. [string]$InitialPriority,
  186. [string]$FinalPriority,
  187. [string]$TimeEstimated,
  188. [string]$Starts,
  189. [string]$Due,
  190. [string]$Text,
  191. [switch]$NoSSL
  192. )
  193. if ($NoSSL){
  194. $http="http://"
  195. }else{
  196. $http="https://"
  197. }
  198. #Close ticket
  199. $content="id: ticket/new`n"
  200. $content+="Queue: $Queue`n"
  201. $content+="Requestor: $Requestor`n"
  202. if ($Subject){$content+="Subject: $Subject`n"}
  203. if ($Priority){$content+="Priority: $Priority`n"}
  204. if ($CC){$content+="CC: $CC`n"}
  205. if ($AdminCC){$content+="AdminCC: $AdminCC`n"}
  206. if ($Owner){$content+="Owner: $Owner`n"}
  207. if ($Status){$content+="Status: $Status`n"}
  208. if ($InitialPriority){$content+="InitialPriority: $InitialPriority`n"}
  209. if ($FinalPriority){$content+="FinalPriority: $FinalPriority`n"}
  210. if ($TimeEstimated){$content+="TimeEstimated: $TimeEstimated`n"}
  211. if ($Starts){$content+="Starts: $Starts`n"}
  212. if ($Due){$content+="Due: $Due`n"}
  213. if ($Text){
  214. $text=$text.Replace("`n", "`n ")
  215. $content+="Text: $Text`n"
  216. }
  217.  
  218.  
  219. $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/new"
  220. $R = Invoke-WebRequest -Uri $uri -Body @{content=$content} -Method Post -WebSession $Session
  221. if ($R -match "\# Ticket (?<ticket>\d*) created\."){
  222. return $Matches.ticket
  223. }else{
  224. return $R.content
  225. }
  226. }
  227.  
  228. <#
  229. .Synopsis
  230. Add a comment to an RT Ticket
  231. .DESCRIPTION
  232. Add a comment to an RT Ticket
  233. .EXAMPLE
  234. Add-RequestTrackerComment -Session $mysession -Server rt.mydomain.com -ticket 12345 -Comment "Ticket Complete`n Thank You!`n Have a Nice Day"
  235. .INPUTS
  236.  
  237. .OUTPUTS
  238. RT REST API Message
  239. .NOTES
  240. new lines on comment must be proceeded by a space eg: "Line1`n Line2`n Line3" and NOT "Line1`nLine2`nLine3"
  241. .RELATED LINKS
  242. https://rt-wiki.bestpractical.com/wiki/REST#Ticket_History_Comment
  243. #>
  244. function Add-RequestTrackerComment{
  245. [cmdletbinding()]
  246. Param (
  247. [Parameter(
  248. Mandatory = $true,
  249. ValueFromPipeLine = $true,
  250. ValueFromPipelineByPropertyName = $true
  251. )]
  252. [ValidateNotNull()]
  253. [Microsoft.PowerShell.Commands.WebRequestSession]
  254. $Session,
  255. [ValidateNotNull()]
  256. [string]$Server,
  257. [string]$VirtualDir="/rt/",
  258. [string]$Ticket,
  259. [string]$Comment,
  260. [switch]$NoSSL
  261. )
  262. if ($NoSSL){
  263. $http="http://"
  264. }else{
  265. $http="https://"
  266. }
  267. $blnCancel=$false
  268. $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/$ticket/comment"
  269. if (!$Comment){
  270. $Comment="The needful is done!"
  271. }
  272. if (!$blnCancel){
  273. $content = "id: $ticket`n"
  274. $blnFirstLine=$true
  275. foreach ($line in ($Comment.Split("`n"))){
  276. if ($blnFirstLine){
  277. $content += "Text: $line`n"
  278. $blnFirstLine=$false
  279. }else{
  280. $content += " $line`n"
  281. }
  282. }
  283. $content += "Action: Comment"
  284.  
  285. $R= Invoke-WebRequest -uri $uri -Body @{content=$content} -Method Post -WebSession $Session
  286. return $R.content
  287. }
  288. }
  289.  
  290. <#
  291. .Synopsis
  292. Send a query to RT server returns object
  293. .DESCRIPTION
  294. Send a RT Rest Query to RT Server and returns Object.
  295. .EXAMPLE
  296. $q="search/ticket?query=(Status='open') AND (Queue='Support')&format=l&fields=Subject,Status,Priority,CF.{Category},Owner,Creator"
  297. Connect-RequestTracker -Credential $cred -Server "rt.mydomain.com"|Get-RequestTrackerQuery -Server "rt.mydomain.com" -Query $q
  298. .INPUTS
  299. Query string
  300. .OUTPUTS
  301. PSOBject
  302. .NOTES
  303. Need to use &format=l(multi-line) in query format=s (Short ticket & subject) and form=i (ticket/ticketID)
  304. #>
  305. function Get-RequestTrackerQuery{
  306. [cmdletbinding()]
  307. Param (
  308. [Parameter(
  309. Mandatory = $true,
  310. ValueFromPipeLine = $true,
  311. ValueFromPipelineByPropertyName = $true
  312. )]
  313. [ValidateNotNull()]
  314. [Microsoft.PowerShell.Commands.WebRequestSession]$Session,
  315. [Parameter(Mandatory = $true)]
  316. [ValidateNotNull()]
  317. [string]$Server,
  318. [string]$VirtualDir="/rt/",
  319. [ValidateNotNull()]
  320. [string]$Query,
  321. [switch]$NoSSL
  322. )
  323. if ($NoSSL){
  324. $http="http://"
  325. }else{
  326. $http="https://"
  327. }
  328.  
  329. $uri=$http + $Server + $VirtualDir + "REST/1.0/" + $Query
  330. $RT=Invoke-WebRequest -Uri $uri -WebSession $Session
  331.  
  332.  
  333. [System.Collections.ArrayList]$tickets=$RT.Content.Split("`n")
  334.  
  335. $arr=@()
  336. $blnNewRecord=$true
  337. $blnNotFirst=$false
  338. foreach ($line in $tickets){
  339. if ($blnNotFirst -and $blnNewRecord){
  340. $Object=New-Object PSObject -Property $hash
  341. $arr+=$Object
  342. $hash=[ordered]@{}
  343. $blnNewRecord=$false
  344. }elseif(!$blnNotFirst){
  345. $hash=[ordered]@{}
  346. $blnNewRecord=$false
  347. $blnNotFirst=$true
  348. }
  349. if ($line -match "([A-Za-z0-9]+):\s+(.*)"){
  350. $hash.Add($Matches[1],$Matches[2])
  351. }
  352. if ($line -match "--"){
  353. $blnNewRecord=$true
  354. }
  355. }
  356. return $arr
  357. }
Advertisement
Add Comment
Please, Sign In to add comment