This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

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.

  • Adding attachments to Incidents isn't properly documented in SWSD's API, however it should be possible. See my response on a similar question here:
    https://thwack.solarwinds.com/product-forums/solarwinds-service-desk-swsd/f/forum/91726/can-i-attach-files-to-samange-api-call

    As for the PowerShell bit, I'm not much of a PowerShell user, but this guide seems to give a pretty solid step-by-step of how to use PowerShell to make requests against an API with basic auth. https://adamtheautomator.com/invoke-restmethod/

  • Thanks Mitch, 

    I am able to do everything I want with the API except posting an attachment.

    I looked at your comments and google hunted some other examples of Invoke-RestMethod posting.  Unfortunately none of them seem to work for SWSD API.  So I'm clearly missing a key piece of the puzzle. 

    I did ask SWSD support for help but their first response wasn't very helpful.  

  • 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.