Export ALL attachments.

Hi all!

I need to export our ticket data weekly to be compliant. 

With that export, i also do need to periodically download all the attachments.

Now i know this question was already asked once, but that answer isnt an answer, its a pointer that i cannot divert to an answer.

How (step by step) do i download all my attachments and in such a manner that its connected to the CSV export of the ticket data (so correct attachment to the correct ticket).

And why isnt there a download button to do so? (i guess more customers need to comply to have a full backup of their data)

Thanks to anyone who can answer :)

Parents
  • Dear all,

    Thanks for the guidance, i have written a script that should work, but when i insert my created API key i receive the 401 error, not authorised.

    What am i doing wrong? This script should work and even showing download status.

    Thanks, Ro

    # Parameters
    param (
        [string]$apiKey = "YOUAPIKEYHERE",
        [string]$apiEndpoint = "https://apieu.samanage.com",
        # API endpoint can be api.samanage.com
        [string]$outputDirectory = "C:\DATA\Attachments"
    
    )
    
    
    # Function to call the API
    function Invoke-SolarWindsAPI {
        param (
            [string]$url,
            [string]$method = "GET",
            [hashtable]$headers = @{},
            [object]$body = $null
        )
    
        $headers.Add("Authorization", "Bearer $apiKey")
        $headers.Add("Accept", "application/json")
        $headers.Add("Content-Type", "application/json")
    
        try {
            $response = Invoke-RestMethod -Uri $url -Method $method -Headers $headers -Body ($body | ConvertTo-Json -Depth 100)
            return $response
        } catch {
            Write-Host "Error accessing $url"
            Write-Host $_.Exception.Message
        }
    }
    
    # Ensure the output directory exists
    if (-Not (Test-Path -Path $outputDirectory)) {
        New-Item -ItemType Directory -Path $outputDirectory
    }
    
    # Fetch all incidents (tickets)
    $incidentsUrl = "$apiEndpoint/incidents.json"
    Write-Host "Fetching incidents from $incidentsUrl"
    $incidents = Invoke-SolarWindsAPI -url $incidentsUrl
    
    if ($null -eq $incidents) {
        Write-Host "No incidents found or error fetching incidents."
        exit
    }
    
    # Initialize progress bar
    $totalIncidents = $incidents.Count
    $incidentCounter = 0
    
    Write-Host ("Starting download of attachments for {0} incidents..." -f $totalIncidents)
    
    # Loop through each incident (ticket) and fetch its attachments
    foreach ($incident in $incidents) {
        $incidentCounter++
        $incidentNumber = $incident.number
        $attachmentsUrl = "$apiEndpoint/incidents/$($incident.id)/attachments.json"
        Write-Host "Fetching attachments from $attachmentsUrl"
        $attachments = Invoke-SolarWindsAPI -url $attachmentsUrl
    
        if ($null -eq $attachments) {
            Write-Host "No attachments found or error fetching attachments for incident #$incidentNumber."
            continue
        }
    
        Write-Host ("Processing incident {0} of {1}: Incident #{2}" -f $incidentCounter, $totalIncidents, $incidentNumber)
    
        # Loop through each attachment
        $totalAttachments = $attachments.Count
        $attachmentCounter = 0
    
        foreach ($attachment in $attachments) {
            $attachmentCounter++
            $attachmentUrl = "$apiEndpoint/attachments/$($attachment.id).json"
            Write-Host "Downloading attachment from $attachmentUrl"
            $attachmentContent = Invoke-SolarWindsAPI -url $attachmentUrl
    
            if ($null -eq $attachmentContent) {
                Write-Host "Error downloading attachment $attachmentCounter of $totalAttachments for incident #$incidentNumber."
                continue
            }
    
            # Define the output file name
            $fileName = "{0}-{1}" -f $incidentNumber, $attachment.fileName
            $outputFilePath = Join-Path -Path $outputDirectory -ChildPath $fileName
    
            # Save the attachment
            [System.IO.File]::WriteAllBytes($outputFilePath, [System.Convert]::FromBase64String($attachmentContent))
    
            Write-Host ("Downloaded attachment {0} of {1} for incident #{2}" -f $attachmentCounter, $totalAttachments, $incidentNumber)
        }
    }
    
    Write-Host ("Download complete. All attachments have been saved to {0}" -f $outputDirectory)
    

  • Dear all, can anyone support me with the error i receive from the script (401 not authorised), API key is correct.

  • Hey  , I'd recommend opening a ticket through the customer service portal. I believe only the SolarWinds team will be able to see the error log for why a 401 was generated.

Reply Children
No Data