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

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

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

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

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

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

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

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

  143. #Convert hashtable to pscustomobject
  144. $oData = New-Object -TypeName PSCustomObject

  145. $oData | Add-Member -MemberType ScriptMethod -Name AddNote -Value {
  146.     Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1]

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

  148.     $oData.AddNote($_,$oDataHash.$_)
  149. }

  150. Import-Module ActiveDirectory
  151. $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}}
  152. Edit-Request -ApiKey $ApiKey -SdpUri $SdpUri -RequestID 94036 -OracleID "$($adid.employeeid)" -Manager "$($adid.managername)"
  153. }
  154. # Put function you want to use below here

                  New to ADSelfService Plus?