API User's Guide

PowerShell: Adding attachments

Introduced in 2022.2

You can use PowerShell scripts to attach files to Alloy Navigator object records. The script below attaches the file file_name to the Incident T000030 and adds the description description. It gets access to Alloy Navigator using user authentication.

Copy the script and replace the placeholders with your actual API URL and the credentials of an Alloy Navigator technician account.

NOTE: To get the proper output, make sure that the ticket T000030 exists in your database and the file file_name is available; otherwise, replace the sample values with your actual IDs.

PowerShell
$ApiUrl = 'https://API_URL'	# API URL without a trailing slash, i.e. https://example.com/api
$username = 'your_username'	# technician username
$password = 'your_password'	# technician password
$objectID = 'T000030'		# object ID
$fileName = 'file_name	'	# file to attach
$description = 'description'	# attachment description
 
$postParams = @{ 'grant_type' = 'password'; 'username' = $username; 'password' = $password; }
$htmlResponse = Invoke-WebRequest -UseBasicParsing -Uri "$ApiUrl/token" -Method POST -Body $postParams
$token = (ConvertFrom-Json $htmlResponse.Content).access_token
 
$bytes = [IO.File]::ReadAllBytes($fileName)
$body = @{
	'FileName' = [IO.Path]::GetFileName($fileName);
	'Description' = $description;
	'Data' = [Convert]::ToBase64String($bytes)
} | ConvertTo-Json
 
$htmlResponse = Invoke-WebRequest -Uri "$ApiUrl/Object/$objectID/Attachments" `
	-Method PUT `
	-Headers @{ 'authorization' = "bearer $token"; 'content-type' = 'application/json';  } `
	-Body $body
 
$htmlResponse