How to properly use API to upload file in Powershell.

How to properly use API to upload file in Powershell.

Hello,

I am looking for "up to date" solution allowing me to update request with attachment.

In my scenario, I am creating request via preventive maintenance tasks and next I would like to add attachment. An xlsx file is generated by separate script. And then I would like to attach this file to the request.

I am facing issues with attaching the file. In documentation I have this script:

#Powershell version - 5.1

  1. $url = "https://servicedesk /api/v3/requests/requestID/upload"
  2. $technician_key = @{"authtoken":"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"}
  3. $response = Invoke-RestMethod -Uri $url -Method put -Headers $technician_Key
  4. $response

 

However, this do not show how to compile the input data. I was looking on the forum but most of the info is from past 6 to 10 years and then we had API v1 and v2.

Also please note that I am using Servicedesk Plus On Prem. I found online documentation for Cloud:

#Powershell version - 5.1

  1. $url = "<url>/api/v3/requests/{request_id}/_uploads"

    $filePath = "local_file_path"                   

    $addtoattachment = "true"

     

    $boundary = [System.Guid]::NewGuid().ToString()

    $headers = @{

        "Accept" = "application/vnd.manageengine.sdp.v3+json"

        "Authorization" = "Zoho-oauthtoken 1000.7xxx98976ab0xxxxxx19901e7551be57.bxxxx921ed64c04f79622bebcfxxxxxx"

        "Content-Type" = "multipart/form-data; boundary=`"$boundary`""

    }

     

    $content = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString([System.IO.File]::ReadAllBytes($filePath))

    $body = (

        "--$boundary",

        "Content-Disposition: form-data; name=`"addtoattachment`"`r`n",

        "$addtoattachment",

        "--$boundary",

        "Content-Disposition: form-data; name=`"filename`"; filename=`"$(Split-Path $filePath -Leaf)`"",

        "Content-Type: $([System.Web.MimeMapping]::GetMimeMapping($filePath))`r`n",

        $content,

        "--$boundary--`r`n"

    ) -join "`r`n"

    $response = Invoke-RestMethod -Uri $url -Method post -Headers $headers -Body $body

    $response

 

The issue hire is that I am unable to properly connect the two parts to make it work. Key difference is "Authorization" and “Method”. In cloud API I suppose to use POST, in on prem. PUT.

If someone could help me, that would be much appreciated.


Best Regards

Pawel.



                  New to ADSelfService Plus?