logo

NJP

Bulk upload files from Client environment to ServiceNow platform

New article articles in ServiceNow Community · Feb 12, 2025 · article

Powershell script -> Simple text file with content provided as input

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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','text/csv') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=test" # Specify HTTP method $method = "post" $body = "This is content of the file not be confused and this is mandatory in order to attach a file with this content" # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Script to send PNG

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' $filePath = "C:\ps" #$fileName ="abc.pdf" $fileName ="capture.png" #$fileNameNow = "abcpdf" $fileNameNow = "capturepng1" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" $contentType = "application/pdf" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Script to send PDF

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' $filePath = "C:\ps" $fileName ="abc.pdf" #$fileName ="capture.png" #$fileNameNow = "abcpdf" $fileNameNow = "abc1pdf" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" $contentType = "application/pdf" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) #echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Script to upload .docx

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' $filePath = "C:\ps" #$fileName ="abc.pdf" $fileName ="testdoc.docx" #$fileName ="capture.png" #$fileNameNow = "abcpdf" #$fileNameNow = "abcdocx" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" #$contentType = "application/pdf" $contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) #echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Script to upload xlsx

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' $filePath = "C:\ps" #$fileName ="abc.pdf" #$fileName ="testdoc.docx" $fileName ="testxl.xlsx" #$fileName ="capture.png" #$fileNameNow = "abcpdf" #$fileNameNow = "abcdocx" $fileNameNow = "abc1xlsx" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" #$contentType = "application/pdf" #$contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" $contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) #echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Script to download large files ~50 MB

sample large files can be downloaded from https://examplefile.com/

# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' $filePath = "C:\ps" #$fileName ="abc.pdf" #$fileName ="testdoc.docx" #$fileName ="testxl.xlsx" $fileName ="50mb.pdf" #$fileName ="capture.png" #$fileNameNow = "abcpdf" #$fileNameNow = "abcdocx" $fileNameNow = "abc50pdf" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" $contentType = "application/pdf" #$contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" #$contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) #echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent

Iterate through a folder and upload file to ServiceNow

$fileName ="50mb.pdf" $fileNameNow = "abc50pdf" $filePath = "C:\ps\Source" function LoadFile { param( [string]$filePath, [string]$fileName, [string]$fileNameNow ) # Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "HYby5KaPud4g" # 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') $sysid = 'da0e92ab352312107f4425dd2b80edd9'; $table = 'incident' #$filePath = "C:\ps" #$fileName ="abc.pdf" #$fileName ="testdoc.docx" #$fileName ="testxl.xlsx" #$fileName ="50mb.pdf" #$fileName ="capture.png" #$fileNameNow = "abcpdf" #$fileNameNow = "abcdocx" $file = $filePath +"\" + $fileName #$contentType = "image/jpeg" $contentType = "application/pdf" #$contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" #$contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" $headers.Add('Content-Type',$contentType) # Specify endpoint uri $uri = "https://nowlearning-nlinst02469592-001.lab.service-now.com/api/now/attachment/file?table\_name=$table&table\_sys\_id=$sysid&file\_name=$fileNameNow" # Specify HTTP method $method = "post" #$body = "this is test" #$body = Get-Content -Path $file -Raw #$body = Get-Content -Path $file -AsByteStream #use this for jpg $body = [System.IO.File]::ReadAllBytes($file) #echo $body # Send HTTP request $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body # Print response $response.RawContent #Write-Host $response.StatusCode #Write-Host $response.result #Write-Host $response.StatusCode.Value Write-Host $response.result } # Get the list of files in the folder $files = Get-ChildItem -Path $filePath -Recurse # Loop through each file and get its name foreach ($file in $files) { # Get the file name $fileName = $file.Name # Invoke the function with the file name as a parameter LoadFile $filePath $fileName $fileNameNow }

References: How to Upload and Attach Files to ServiceNow Records Using PowerShell - YouTube

View original source

https://www.servicenow.com/community/developer-blog/bulk-upload-files-from-client-environment-to-servicenow-platform/ba-p/3175692