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.
- $ApiKey = "API Key here" # Technician API Key
- $SdpUri = "http://sdpurl.com" # SDP URL
- # Gets information on an existing request
- function Get-Request
- {
- [CmdletBinding()]
- param
- (
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
- [alias ("id")]
- [Int32]
- $RequestID,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
- [String]
- $ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
- [String]
- $SdpUri,
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
- [String]
- $Manager, # Manager field
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
- [String]
- $RequestFor, # Person Request is For
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
- [Int32]
- $OracleID # Oracle iD
- )
- $input= @"
- {
- "request":{
- "udf_fields":{
- "udf_sline_18083": "$RequestFor",
- "udf_sline_18081": "$Manager",
- "udf_long_18098": "$OracleID"
- }
- }
- }
- "@
- $header = @{TECHNICIAN_KEY=$ApiKey}
- $params = @{input_data=$input;format='json'}
- $Uri = $SdpUri + "/api/v3/requests/" + $RequestID
- $result = Invoke-RestMethod -Method Get -Uri $Uri -Headers $header -OutFile "\\server\users\username\Ticket.json"
- $result
- }
- # Creates a request using provided information
- function Add-Request
- {
- [CmdletBinding()]
- param
- (
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
- [String]
- $ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
- [String]
- $SdpUri,
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
- [String]
- $Requester
- )
- $input= @"
- {
- "request":{
- "subject": "Unable to fetch mails",
- "description": "I am unable to fetch mails from the mail server",
- "requester":{
- "name": "$Requester",
- },
- "template": {
- "name": "Software Request",
- },
- "udf_fields":{
- "udf_date_21049":{
- "display_value": "1551462180000",
- },
- "udf_sline_18083": "John Doe",
- "udf_sline_18081": "John Smith"
- },
- "status":{
- "name": "Open"
- }
- }
- }
- "@
- $header = @{TECHNICIAN_KEY=$ApiKey}
- $params = @{input_data=$input;format='json'}
- $Uri = $SdpUri + "/api/v3/requests"
- $result = Invoke-RestMethod -Method POST -Uri $Uri -Headers $header -Body $params -ContentType "application/x-www-form-urlencoded"
- $result
- }
- # Edits an existing request
- function Edit-Request
- {
- <#
- This function can edit any field of a request ticket. Input for field values would have to be added if additional input is needed.
- #>
- [CmdletBinding()]
- param
- (
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
- [alias ("id")]
- [Int32]
- $RequestID,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)]
- [String]
- $ApiKey,
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=3)]
- [String]
- $SdpUri,
- [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
- [String]
- $Manager, # Manager field
- [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
- [String]
- $OracleID # Oracle iD
- )
- # Input for the request fields
- $input= @"
- {
- "request":{
- "udf_fields":{
- "udf_sline_18081": "$Manager",
- "udf_long_18098": "$OracleID"
- }
- }
- }
- "@
- $header = @{TECHNICIAN_KEY=$ApiKey}
- $params = @{input_data=$input;format='json'}
- $Uri = $SdpUri + "/api/v3/requests/" + $RequestID
- $result = Invoke-RestMethod -Method PUT -Uri $Uri -Headers $header -Body $params -ContentType "application/x-www-form-urlencoded"
- $result
- }
- # Gets ticket information from the specified request ID. Response is stored in a json file then used as an input.
- function Update-Request
- {
- Get-Request -ApiKey $ApiKey -SdpUri $SdpUri -RequestID 94036
- $Json= Get-Content -Raw "\\server\users\username\Ticket.json" | Out-String | ConvertFrom-Json
- $oDataHash = @{}
- $Json.request | Get-Member -MemberType NoteProperty | ForEach-Object{
- $oDataHash += @{
- $_.name = $Json.request."$($_.name)"
- }
- }
- #Convert hashtable to pscustomobject
- $oData = New-Object -TypeName PSCustomObject
- $oData | Add-Member -MemberType ScriptMethod -Name AddNote -Value {
- Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1]
- }
- $oDataHash.Keys | Sort-Object | ForEach-Object{
- $oData.AddNote($_,$oDataHash.$_)
- }
- Import-Module ActiveDirectory
- $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}}
- Edit-Request -ApiKey $ApiKey -SdpUri $SdpUri -RequestID 94036 -OracleID "$($adid.employeeid)" -Manager "$($adid.managername)"
- }
- # Put function you want to use below here
New to ADSelfService Plus?