Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Synopsis
- Connects to RT Server and returns a Session.
- .DESCRIPTION
- Connects to RT Server and returns a Session So that other *-RequestTracker* commands can be authenticated when using the -Session paramenter.
- .EXAMPLE
- $MyRTSession=Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com"
- .INPUTS
- .OUTPUTS
- Microsoft.PowerShell.Commands.WebRequestSession
- .NOTES
- It gets you authenticated onto RT Server
- #>
- function Connect-RequestTracker{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [System.Management.Automation.PSCredential]
- [System.Management.Automation.Credential()]
- $Credential,
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- $LoginPage="$http$Server$VirtualDir"
- $r = Invoke-WebRequest -Uri $LoginPage -SessionVariable RTSession -Credential $Credential
- $form = $r.Forms[0]
- $form.fields['user'] = $Credential.UserName
- $form.fields['pass'] = $Credential.GetNetworkCredential().Password
- $r = Invoke-WebRequest -Uri ($http + $Server + $form.Action) -WebSession $RTSession -Method POST -Body $form.Fields
- if ($r.Content -notmatch "<li>Your username or password is incorrect</li>"){
- return $RTSession
- }else{
- Write-Error "Server $($http + $Server): Your username or password is incorrect"
- }
- }
- <#
- .Synopsis
- GET RT (Request Tracker) Tickets.
- .DESCRIPTION
- Connects to RT Site with a valid session (From Connect-RequestTracker) and Get's a RT ticket.
- .EXAMPLE
- Get-RequestTrackerTicket -Session $mysession -Server rt.mydomain.com -Ticket 9507
- .INPUTS
- Ticket
- Session
- .OUTPUTS
- RT REST API Response
- .NOTES
- Get's the ticket
- #>
- Function Get-RequestTrackerTicket{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [Microsoft.PowerShell.Commands.WebRequestSession]
- $Session,
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [string]$Ticket,
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- $uri="https://" + $Server + $VirtualDir + "REST/1.0/ticket/$Ticket/history?format=l"
- $R = Invoke-WebRequest -Uri $uri -WebSession $Session
- return $R.Content
- }
- <#
- .Synopsis
- Closes RT (Request Tracker) Tickets.
- .DESCRIPTION
- Connects to RT Site with a valid session (From Connect-RequestTracker) and closes a ticket.
- .EXAMPLE
- Close-RequestTrackerTicket -Server "rt.mydomian.com" -Session $mysession -Ticket 123
- .EXAMPLE
- Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com" | Close-RequestTrackerTicket -Server "atlas.orbcomm.com" -Ticket 123
- .INPUTS
- Microsoft.PowerShell.Commands.WebRequestSession from Connect-RequestTracker.
- .OUTPUTS
- RT REST API Response
- .NOTES
- It does nothing but Close Tickets. It does it with stealth. No email NAGS are sent from RT. SIMPLE.
- #>
- Function Close-RequestTrackerTicket{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [Microsoft.PowerShell.Commands.WebRequestSession]
- $Session,
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [string]$Ticket,
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- #Close ticket
- $content="id: ticket/edit`n"
- $content+="Status: resolved"
- $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/$Ticket/edit"
- $R = Invoke-WebRequest -Uri $uri -Body @{content=$content} -Method Post -WebSession $Session
- return $R.Content
- }
- <#
- .Synopsis
- New Ticket.
- .DESCRIPTION
- Connects to RT Site with a valid session (From Connect-RequestTracker) and creates a new ticket.
- .EXAMPLE
- Close-RequestTrackerTicket -Server "rt.mydomian.com" -Session $mysession -Ticket 123
- .EXAMPLE
- Connect-RequestTracker -Credential (Get-Credential) -Server "rt.mydomain.com" | Close-RequestTrackerTicket -Server "atlas.orbcomm.com" -Ticket 123
- .INPUTS
- Microsoft.PowerShell.Commands.WebRequestSession from Connect-RequestTracker.
- .OUTPUTS
- RT REST API Response
- .NOTES
- It does nothing but Close Tickets. It does it with stealth. No email NAGS are sent from RT. SIMPLE.
- #>
- Function New-RequestTrackerTicket{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [Microsoft.PowerShell.Commands.WebRequestSession]
- $Session,
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [Parameter(Mandatory = $true)]
- [string]$Queue,
- [Parameter(Mandatory = $true)]
- [string]$Requestor,
- [Parameter(Mandatory = $true)]
- [string]$Subject,
- [string]$CC,
- [string]$AdminCC,
- [string]$Owner="Nobody in particular",
- [string]$Status,
- [string]$Priority,
- [string]$InitialPriority,
- [string]$FinalPriority,
- [string]$TimeEstimated,
- [string]$Starts,
- [string]$Due,
- [string]$Text,
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- #Close ticket
- $content="id: ticket/new`n"
- $content+="Queue: $Queue`n"
- $content+="Requestor: $Requestor`n"
- if ($Subject){$content+="Subject: $Subject`n"}
- if ($Priority){$content+="Priority: $Priority`n"}
- if ($CC){$content+="CC: $CC`n"}
- if ($AdminCC){$content+="AdminCC: $AdminCC`n"}
- if ($Owner){$content+="Owner: $Owner`n"}
- if ($Status){$content+="Status: $Status`n"}
- if ($InitialPriority){$content+="InitialPriority: $InitialPriority`n"}
- if ($FinalPriority){$content+="FinalPriority: $FinalPriority`n"}
- if ($TimeEstimated){$content+="TimeEstimated: $TimeEstimated`n"}
- if ($Starts){$content+="Starts: $Starts`n"}
- if ($Due){$content+="Due: $Due`n"}
- if ($Text){
- $text=$text.Replace("`n", "`n ")
- $content+="Text: $Text`n"
- }
- $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/new"
- $R = Invoke-WebRequest -Uri $uri -Body @{content=$content} -Method Post -WebSession $Session
- if ($R -match "\# Ticket (?<ticket>\d*) created\."){
- return $Matches.ticket
- }else{
- return $R.content
- }
- }
- <#
- .Synopsis
- Add a comment to an RT Ticket
- .DESCRIPTION
- Add a comment to an RT Ticket
- .EXAMPLE
- Add-RequestTrackerComment -Session $mysession -Server rt.mydomain.com -ticket 12345 -Comment "Ticket Complete`n Thank You!`n Have a Nice Day"
- .INPUTS
- .OUTPUTS
- RT REST API Message
- .NOTES
- new lines on comment must be proceeded by a space eg: "Line1`n Line2`n Line3" and NOT "Line1`nLine2`nLine3"
- .RELATED LINKS
- https://rt-wiki.bestpractical.com/wiki/REST#Ticket_History_Comment
- #>
- function Add-RequestTrackerComment{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [Microsoft.PowerShell.Commands.WebRequestSession]
- $Session,
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [string]$Ticket,
- [string]$Comment,
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- $blnCancel=$false
- $uri=$http + $Server + $VirtualDir + "REST/1.0/ticket/$ticket/comment"
- if (!$Comment){
- $Comment="The needful is done!"
- }
- if (!$blnCancel){
- $content = "id: $ticket`n"
- $blnFirstLine=$true
- foreach ($line in ($Comment.Split("`n"))){
- if ($blnFirstLine){
- $content += "Text: $line`n"
- $blnFirstLine=$false
- }else{
- $content += " $line`n"
- }
- }
- $content += "Action: Comment"
- $R= Invoke-WebRequest -uri $uri -Body @{content=$content} -Method Post -WebSession $Session
- return $R.content
- }
- }
- <#
- .Synopsis
- Send a query to RT server returns object
- .DESCRIPTION
- Send a RT Rest Query to RT Server and returns Object.
- .EXAMPLE
- $q="search/ticket?query=(Status='open') AND (Queue='Support')&format=l&fields=Subject,Status,Priority,CF.{Category},Owner,Creator"
- Connect-RequestTracker -Credential $cred -Server "rt.mydomain.com"|Get-RequestTrackerQuery -Server "rt.mydomain.com" -Query $q
- .INPUTS
- Query string
- .OUTPUTS
- PSOBject
- .NOTES
- Need to use &format=l(multi-line) in query format=s (Short ticket & subject) and form=i (ticket/ticketID)
- #>
- function Get-RequestTrackerQuery{
- [cmdletbinding()]
- Param (
- [Parameter(
- Mandatory = $true,
- ValueFromPipeLine = $true,
- ValueFromPipelineByPropertyName = $true
- )]
- [ValidateNotNull()]
- [Microsoft.PowerShell.Commands.WebRequestSession]$Session,
- [Parameter(Mandatory = $true)]
- [ValidateNotNull()]
- [string]$Server,
- [string]$VirtualDir="/rt/",
- [ValidateNotNull()]
- [string]$Query,
- [switch]$NoSSL
- )
- if ($NoSSL){
- $http="http://"
- }else{
- $http="https://"
- }
- $uri=$http + $Server + $VirtualDir + "REST/1.0/" + $Query
- $RT=Invoke-WebRequest -Uri $uri -WebSession $Session
- [System.Collections.ArrayList]$tickets=$RT.Content.Split("`n")
- $arr=@()
- $blnNewRecord=$true
- $blnNotFirst=$false
- foreach ($line in $tickets){
- if ($blnNotFirst -and $blnNewRecord){
- $Object=New-Object PSObject -Property $hash
- $arr+=$Object
- $hash=[ordered]@{}
- $blnNewRecord=$false
- }elseif(!$blnNotFirst){
- $hash=[ordered]@{}
- $blnNewRecord=$false
- $blnNotFirst=$true
- }
- if ($line -match "([A-Za-z0-9]+):\s+(.*)"){
- $hash.Add($Matches[1],$Matches[2])
- }
- if ($line -match "--"){
- $blnNewRecord=$true
- }
- }
- return $arr
- }
Advertisement
Add Comment
Please, Sign In to add comment