PowerShell Rest API V3

PowerShell Rest API V3

I figured it would be worthwhile to share the PowerShell script I have that uses API v3 for working with requests. Hopefully this will help some people that are trying to use this.

  1. $ApiKey = "API Key here" # Technician API Key
  2. $SdpUri = "http://sdpurl.com" # SDP URL

  3. # Gets information on an existing request
  4. function Get-Request
  5. {
  6. [CmdletBinding()]
  7. param
  8. (
  9. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] 
  10. [alias ("id")]
  11. [Int32]
  12. $RequestID,
  13. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] 
  14. [String]
  15. $ApiKey,
  16. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)] 
  17. [String]
  18. $SdpUri,
  19. [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
  20. [String] 
  21. $Manager, # Manager field
  22. [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
  23. [String] 
  24. $RequestFor, # Person Request is For
  25. [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)] 
  26. [Int32]
  27. $OracleID # Oracle iD 
  28. )

  29. $input= @"
  30. {
  31. "request":{
  32. "udf_fields":{
  33. "udf_sline_18083": "$RequestFor",
  34. "udf_sline_18081": "$Manager",
  35. "udf_long_18098": "$OracleID"
  36. }
  37. } 
  38. }
  39. "@


  40. $header = @{TECHNICIAN_KEY=$ApiKey} 
  41. $params = @{input_data=$input;format='json'}
  42. $Uri = $SdpUri + "/api/v3/requests/" + $RequestID
  43. $result = Invoke-RestMethod -Method Get -Uri $Uri -Headers $header -OutFile "\\server\users\username\Ticket.json"
  44. $result
  45. }

  46. # Creates a request using provided information
  47. function Add-Request
  48. {
  49. [CmdletBinding()]
  50. param
  51. (
  52. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] 
  53. [String]
  54. $ApiKey,
  55. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)] 
  56. [String]
  57. $SdpUri,
  58. [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
  59. [String] 
  60. $Requester
  61. )

  62. $input= @"
  63. {
  64. "request":{
  65. "subject": "Unable to fetch mails",
  66. "description": "I am unable to fetch mails from the mail server",
  67. "requester":{
  68. "name": "$Requester",
  69. },
  70. "template": {
  71. "name": "Software Request",
  72. },
  73. "udf_fields":{
  74. "udf_date_21049":{
  75. "display_value": "1551462180000",
  76. },
  77. "udf_sline_18083": "John Doe",
  78. "udf_sline_18081": "John Smith"
  79. },
  80. "status":{
  81. "name": "Open"
  82. }
  83. } 
  84. }
  85. "@

  86. $header = @{TECHNICIAN_KEY=$ApiKey} 
  87. $params = @{input_data=$input;format='json'}
  88. $Uri = $SdpUri + "/api/v3/requests"
  89. $result = Invoke-RestMethod -Method POST -Uri $Uri -Headers $header -Body $params -ContentType "application/x-www-form-urlencoded"
  90. $result
  91. }

  92. # Edits an existing request
  93. function Edit-Request
  94. {
  95. <#
  96. This function can edit any field of a request ticket. Input for field values would have to be added if additional input is needed.
  97. #>

  98. [CmdletBinding()]
  99. param
  100. (
  101. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] 
  102. [alias ("id")]
  103. [Int32]
  104. $RequestID,
  105. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] 
  106. [String]
  107. $ApiKey,
  108. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)] 
  109. [String]
  110. $SdpUri,
  111. [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
  112. [String] 
  113. $Manager, # Manager field
  114. [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] 
  115. [String]
  116. $OracleID # Oracle iD 
  117. )

  118. # Input for the request fields
  119. $input= @"
  120. {
  121. "request":{
  122. "udf_fields":{
  123. "udf_sline_18081": "$Manager",
  124. "udf_long_18098": "$OracleID"
  125. }
  126. } 
  127. }
  128. "@
  129. $header = @{TECHNICIAN_KEY=$ApiKey} 
  130. $params = @{input_data=$input;format='json'}
  131. $Uri = $SdpUri + "/api/v3/requests/" + $RequestID
  132. $result = Invoke-RestMethod -Method PUT -Uri $Uri -Headers $header -Body $params -ContentType "application/x-www-form-urlencoded"
  133. $result
  134. }  

  135. # Gets ticket information from the specified request ID. Response is stored in a json file then used as an input.
  136. function Update-Request 
  137. {
  138. Get-Request -ApiKey $ApiKey -SdpUri $SdpUri -RequestID 94036 
  139. $Json= Get-Content -Raw "\\server\users\username\Ticket.json" | Out-String | ConvertFrom-Json
  140. $oDataHash = @{}
  141. $Json.request | Get-Member -MemberType NoteProperty | ForEach-Object{
  142.     $oDataHash += @{
  143.         $_.name = $Json.request."$($_.name)" 
  144.     }
  145. }

  146. #Convert hashtable to pscustomobject
  147. $oData = New-Object -TypeName PSCustomObject

  148. $oData | Add-Member -MemberType ScriptMethod -Name AddNote -Value {
  149.     Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1]
  150. } 

  151. $oDataHash.Keys | Sort-Object | ForEach-Object{

  152.     $oData.AddNote($_,$oDataHash.$_)
  153. }

  154. Import-Module ActiveDirectory
  155. $adid= Get-ADUser -Filter "Name -like '$($oData.udf_fields.udf_sline_18083)'" -Properties EmployeeID,Manager | Select-Object EmployeeID,@{n="ManagerName";e={(Get-ADUser -Identity $_.Manager -Properties displayName).DisplayName}}
  156. Edit-Request -ApiKey $ApiKey -SdpUri $SdpUri -RequestID 94036 -OracleID "$($adid.employeeid)" -Manager "$($adid.managername)"
  157. }
  158. # Put function you want to use below here