VaranAwesomenow
Mega Sage
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
02-12-2025
03:46 AM
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
}
Script to read files from a folder, process them and move to success or failed folders depending on response from API call
#Add logic to verify presence of file before attempting a move file.
$fileName ="50mb.pdf"
$fileNameNow = "abc50.pdf"
$filePathSource = "C:\ps\Source"
$FilePathSuccess = "C:\ps\Success"
$FilePathFailed = "C:\ps\Failed"
#function to parse file name and derive employee number, employee name, document type
#API call to servicenow to check employee number is valid.
#API call to create document record.
#Read all the files pass it as response to flow call.
#For each document flow will create record in ServiceNow and then send tat as input to processing powershell script.
function LoadFile {
param(
[string]$filePathSource,
[string]$fileName,
[string]$fileNameNow
)
# Eg. User name="admin", Password="admin" for this code sample.
#Use OAUTH authentication
$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'
#$filePathSource = "C:\ps"
#$fileName ="abc.pdf"
#$fileName ="testdoc.docx"
#$fileName ="testxl.xlsx"
#$fileName ="50mb.pdf"
#$fileName ="capture.png"
#$fileNameNow = "abcpdf"
#$fileNameNow = "abcdocx"
$file = $filePathSource +"\" + $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
try {
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri -Body $body
}
catch [Exception] {
Write-Host "An error occurred: $_.Exception.Message"
$response = $_.Exception.Message
Write-Host $response
Move-Item -Path $filePathSource\$fileName -Destination $FilePathFailed\$fileName
}
Finally{
# Code always executes, regardless if an exception was thrown or not
#Write-Host "reached finally"
}
# Print response
$response.RawContent
#Write-Host $response.StatusCode
Write-Host $response.RawContent
#Write-Host $response.StatusCode.Value
#Write-Host $response.result
try {
$sentence = "result is" + $response.result
$stringToSearch = "sys_updated_by"
if($sentence.contains($stringToSearch)) {
Write-Host "$stringToSearch found in $sentence"
Move-Item -Path $filePathSource\$fileName -Destination $FilePathSuccess\$fileName
} else {
Write-Host "$stringToSearch not found in $sentence"
Move-Item -Path $filePathSource\$fileName -Destination $FilePathFailed\$fileName
}
}
catch [Exception] {
Write-Host "An error occurred: $_.Exception.Message"
Move-Item -Path $filePathSource\$fileName -Destination $FilePathFailed\$fileName
}
}
# Get the list of files in the folder
$files = Get-ChildItem -Path $filePathSource -Recurse
# Loop through each file and get its name
foreach ($file in $files)
{
# Get the file name
$fileName = $file.Name
$fileNameNow = $fileName
# Invoke the function with the file name as a parameter
LoadFile $filePathSource $fileName $fileNameNow
}
References: How to Upload and Attach Files to ServiceNow Records Using PowerShell - YouTube
- 560 Views
1 Comment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.