API User's Guide

PowerShell: Downloading attachments

Introduced in 2022.2

You can use PowerShell scripts to download files attached to Alloy Navigator object records. The script below downloads the file file_name attached to the Incident T000030 . 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 7B5B7027-E73C-4003-8AD6-4A0478D3AAED is available; otherwise, replace the sample values with your actual IDs.

$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

$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

$htmlResponse = Invoke-WebRequest -Uri "$ApiUrl/Object/T000030/Attachments/7B5B7027-E73C-4003-8AD6-4A0478D3AAED/download" `
	-Method GET `
	-Headers @{ 'authorization' = "bearer $token"; 'content-type' = 'application/json'; }


$outFle = New-Item -Name $htmlResponse.Headers['Attachment-File-Name'] -ItemType File -Force

$fileStream = $outFle.OpenWrite();

$htmlResponse.RawContentStream.WriteTo($fileStream);

$fileStream.Dispose();