API User's Guide

PowerShell: Obtaining information about objects, application authentication

Introduced in 8.7

Updated in 2021.1

You can use PowerShell scripts to let third-party apps run API requests without a signed-in user present. Such apps can run various maintenance or service tasks, for example routinely create or update Alloy Navigator records associated with data in external systems, or retrieve information from Alloy Navigator to feed it into external systems. The script uses application authentication to retrieve all fields for the object with ID = C000009.

Copy the script and replace the placeholders with your actual API URL and the application ID and secret issued by Alloy Navigator.

NOTE: To get the proper output, make sure that the Computer record C000009 exists in your database; otherwise, replace the sample ID with the ID of any other existing Computer or another object.

PowerShell
$ApiUrl = 'https://API_URL'		# API URL without a trailing slash, i.e. https://example.com/api
$clientId = 'application_id'		# application ID
$clientSecret = 'application_secret'	# application secret
 
$postParams = @{ 'grant_type' = 'client_credentials'; 'client_id' = $clientId; 'client_secret' = $clientSecret; }
$htmlResponse = Invoke-WebRequest -UseBasicParsing -Uri "$ApiUrl/token" -Method POST -Body $postParams
$token = (ConvertFrom-Json $htmlResponse.Content).access_token
 
$HtmlResponse = Invoke-WebRequest -UseBasicParsing -Uri "$ApiUrl/object/C000009" `
	-Method GET `
	-Headers @{ 'authorization' = "bearer $token"; 'content-type' = 'application/json';    }
 
$HtmlResponse