Upload file to import set table using powershell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2020 12:47 AM
Hi All, If anyone can help with this it would be appreciated.
Powershell returns a 200 ok message but ServiceNow either does not log anything at all and no import set or if i change the context type to text/csv ServiceNow reports an import log that the attachment could not be succesfully extracted.
The CSV is a file that contains a upn and mobile number.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "XXXXXX"
$pass = "XXXXXX"
# 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('Content-Type','multipart/form-data')
#$headers.Add('Content-Type','text/csv') - Tried with no success
# Specify endpoint uri
$uri = "https://<instanceName>.service-now.com/sys_import.do?sysparm_import_set_tablename=u_imp_user&sysparm_transform_after_load=true"
# Specifiy file to attach
$fileToAttach = "C:\Script\Azureexport.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
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2020 03:53 AM
Hello Nik,
Refer below links, it may help you to solve your query.
Kindly mark my answer correct & helpful; if it's useful to you.
Thanks & Regards,
Pooja Devkar
DxSherpa Technologies Pvt. Ltd

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2020 03:54 AM
Refer below links, it may help you to solve your query.
Kindly mark my answer correct & helpful; if it's useful to you.
Thanks & Regards,
Pooja Devkar
DxSherpa Technologies Pvt. Ltd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2020 10:36 AM
You need to sent the actual file content, not just the filename, If you use postman, you can code generate the power shell code.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "multipart/form-data")
$headers.Add("Authorization", "<BASIC AUTH>")
$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$multipartFile = '<FULL PATH TO FULL>'
$FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open)
$fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$fileHeader.Name = "file"
$fileHeader.FileName = "<FULL PATH TO FULL>"
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$fileContent.Headers.ContentDisposition = $fileHeader
$multipartContent.Add($fileContent)
$body = $multipartContent
$response = Invoke-RestMethod 'https://<INSTANCE NAME>.service-now.com/sys_import.do?sysparm_import_set_tablename=<STAGING TABLE NAME>&sysparm_transform_after_load=true' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
This works in PS 3.0 and above.
$stagingTableName = "u_testing"
$env = "<YOUR INSTANCE URL>"
$file = "<YOUR FILE NAME>"
$url = ("https://$env/sys_import.do?sysparm_import_set_tablename=$stagingTableName&sysparm_transform_after_load=true")
$user = "testuser"
$pass = "testuser"
try {
$webClient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$webclient.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$webClient.UploadFile($url,$file)
$webClient.ResponseHeaders
}
catch {
Write-Warning "$($error[0])"
}
This works in PS 2.0