Need help in powershell script for sending files using REST API

jansi rani krishnan 601 Reputation points
2021-08-14T10:58:23.5+00:00

Hi Team,

I have the below code which sends a file attached to an incident in SCSM to Service-Now. I need some help in sending multiple files attached to an incident in SCSM to SNOW. I have the files downloaded in the local repository with the folder name same as the incident (Eg. IR1234). Also, one of the header (content-type) is dynamically changing based on the file type. For example the equivalent MIME type can be as given in the link https://developer.devada.com/docs/mime-types

`$CRED=get-credential

$URI="https://service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=65fad8e21bf5b010d6557805464bcbe9&file_name=Sample.txt"
$headers=New-Object "System.Collections.Generic.Dictionary[[String], [String]]"
#$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add("Content-Type", 'text/plain')
$headers.Add("Accept", 'application/json')

# Specifiy file to attach
$fileToAttach = "C:\Users\jankrish1\Desktop\New_folder\Sample.txt"

# Specify HTTP method (POST, PATCH, PUT)
$method = "POST"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -credential $CRED -Uri $URI -Infile $fileToAttach

# Print response
$response.RawContent

Any help is greatly appreciated!!!

Regards,
Jansi

System Center Service Manager
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-08-14T14:53:55.063+00:00

    Based on what I've seen, Invoke-WebRequest isn't capable of creating multipart MIME messages. You have to construct the body yourself. A quick web search for "invoke-webrequest upload multiple files" produced lots of good examples.

    For example: upload-multiple-files-from-powershell-script

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.