Search code examples
restpowershellservicenow

How to attach CSV file to Service Now incident via REST API using PowerShell?


I need to attach the file either xlsx or CSV to a particular incident via SNOW REST API using PowerShell script. I have tried with the below code:

if (!$script:ServiceNowCreds) {
    $script:ServiceNowCreds = Get-Credential
}
$snow_url = 'https://dev652xx.service-now.com/api/now/table/incident'
$Body = @{
    'number' = 'INC00xx059'    
}
$result = Invoke-RestMethod -Uri  $snow_url -Credential $script:ServiceNowCreds -Body $Body -ContentType "application/json" 
$result.result | select sys_id, number | ForEach-Object {
    $Upload_snow_url ='https://dev652xx.servicenow.com/api/now/attachment/upload'
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add('Content-Type','text/csv')
    $headers.Add('Accept','*/*')
    $sys_id = $_.sys_id
    $incident_number = $_.number
    $UploadBody = @{
        'table_name'='incident'; 
        'table_sys_id'=$sys_id;
        'file_name' = 'C:\Users\suganthanraj.p\Documents\Servers.csv'
    }
    $uploadParam = $UploadBody | ConvertTo-JSon
    Write-Host $sys_id
    Write-Host $incident_number
    $UploadResult = Invoke-RestMethod -Uri $Upload_snow_url -Credential $script:ServiceNowCreds -Body $uploadParam -Method Post -Headers $headers
    $UploadResult
}

When I execute the above script I am getting the below error:

Invoke-RestMethod : The remote server returned an error: (415) Unsupported 
Media Type.
At C:\Users\suganthanraj.p\Desktop\SNOW-UploadAttachment.ps1:39 char:21
+ ... oadResult = Invoke-RestMethod -Uri  $Upload_snow_url -Credential $scr ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Solution

  • Finally i found answer from the below link

    https://community.servicenow.com/community?id=community_question&sys_id=d3707023dbaceb8023f4a345ca961949 and below is the code:

     # Eg. User name="admin", Password="admin" for this code sample.
     $user = "admin"
     $pass = "XXX"
    
     # Build auth header
     $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
    
    # Set proper headers
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
    $headers.Add('Accept','application/json')
    $headers.Add('Content-Type','application/json')
    
    
    # Specify endpoint uri
    $uri = "https://dev652XX.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=850XXXXX2200e0ef563dbb9a71c1&file_name=TreeSizeReport.csv"
    
    # Specifiy file to attach
    $fileToAttach = "C:\Users\suganthanraj.p\Desktop\TreeSizeReport.csv"
    
    # Specify HTTP method (POST, PATCH, PUT)
    $method = "POST"
    
    # Send HTTP request
    $response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach
    
    # Print response
    $response.RawContent