Automated Data Import - Using MID Server and PowerShell query

Zack Zap
Tera Contributor

Good day!

 

I tried to follow along the guide provided by Oscar here: https://www.servicenow.com/community/itom-articles/automated-data-import-using-mid-server-and-powers...

 

However, I am currently stuck, specifically in the PowerShell part of the guide. I also tried running it as batch file during a debugging session with one of my seniors.

I used the Attachment API documentation as a guide and followed that as well but still no luck. Any idea where I went wrong or what I might be missing here? 

 

PowerShell Script from Oscar's guide:

 

##############################################
#####         FILE UPLOAD                #####
##############################################

# Eg. User name="admin", Password="admin" for this code sample.
$user = "integration"
$pass = "queretaro"

# 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://dev99999.service-now.com/api/now/attachment/file?table_name=sys_data_source&table_sys_id=c6d53341474b01100d784168f36d436e&file_name=NewUsers.xlsx"

# Specifiy file to attach
$fileToAttach = "c:\report\NewUsers.xlsx"

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

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach -ContentType 'multipart/form-data'

# Print response
$response.RawContent

 

 

PowerShell Script on my side (edited to match the syntax in the official docs/edited as a batch file for testing):

 

: Eg. User name="admin", Password="admin" for this code sample.
$user = "username"
$pass = "password"

: 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','multipart/form-data')

: Specify endpoint uri was TeamsPhoneNumberAssignments
$uri = "https://instance_name.service-now.com/api/now/attachment/upload?table_name=sys_data_source&table_sys_id=0e32114e1bff429010454268b04bcba2&uploadFile=C:\TestRun\waluigi.csv"

: Specify file to attach
$fileToAttach = "C:\TestRun\waluigi.csv"

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

: Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach
::-ContentType 'text/csv'

: Print response
$response.RawContent

 

Thank you.

0 REPLIES 0