API - Sample Add Attachment via Powershell?

I'm very new to Invoke-RestMethod and have been successful at pulling the data that I want.   Thanks to a few posts I've seen here. 

But

One challenge that seems to stump me is how to add an attachment to an incident via powershell.  I see the api docs have a curl example.  I have not seen any PS examples and my poor brain is unable to convert the syntax of curl to Invoke-RestMethod.

Parents
  • Update: As noted Powershell 7 is required for -form support. 

    I was able to get things to work.  Posting here my notes to help any future people searching for clues.   

    Adding to the ticket.

    $file = @{
        "file[attachable_type]" = "Incident"
        "file[attachable_id]" = "123456"
        "file[attachment]" = (get-item -path "C:\temp\Test.txt")
    }
    
    $uri = "https://api.samanage.com/attachments.json"
    
    Invoke-RestMethod -Uri $uri -form $file -ContentType 'multipart/form-data' -ResponseHeadersVariable ResponseHeader -Method Post -WebSession $session

    Adding to a comment in the ticket.

    $file = @{
        "file[attachable_type]" = "Comment"
        "file[attachable_id]" = "123456"
        "file[attachment]" = (get-item -path "C:\temp\Test.txt")
    }
    
    $uri = "https://api.samanage.com/attachments.json"
    
    Invoke-RestMethod -Uri $uri -form $file -ContentType 'multipart/form-data' -ResponseHeadersVariable ResponseHeader -Method Post -WebSession $session

    Also found that when using the curl example on Windows you need to use double quotes not the single quote as shown in the API doc. 

  • Note: You must be using PowerShell 7.x+ for this as the Invoke-RestMethod from previous versions does not support the -Form parameter.

  • Yes that is correct.  Thanks for noticing that. 

Reply Children
No Data