API User's Guide
PowerShell: Adding attachments via workflow actions
You can use PowerShell scripts to attach files to Alloy Navigator objects within a specific action using a POST request. The script below attaches a file to an object by specifying the object ID, action workflow step ID, file name, and description. The script retrieves an access token using user authentication and converts the file into a base64-encoded format before sending it in the request. The attachment is uploaded with an optional comment.
$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 = 'SRQ000012' # object ID
$fileName = 'file_name' # full path to the attachment file, i.e. C:\Scripts\Example.txt
$description = 'description' # attachment description
$actionId = '131' # step action workflow ID
$comment = 'comment' # comment
$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 = @{
'Comment' = $comment
'Attachments' = @(
@{
'FileName' = [IO.Path]::GetFileName($fileName)
'Description' = $description
'Data' = [Convert]::ToBase64String($bytes)
}
)
} | ConvertTo-Json
$htmlResponse = Invoke-WebRequest -Uri "$ApiUrl/Object/$objectID/action/$actionId" `
-Method POST `
-Headers @{ 'authorization' = "bearer $token"; 'content-type' = 'application/json'; } `
-Body $body
$htmlResponse