ARTIFACTORY: How to Run AQL Commands in Windows Using PowerShell

ARTIFACTORY: How to Run AQL Commands in Windows Using PowerShell

AuthorFullName__c
Yonatan Hen
articleNumber
000005774
FirstPublishedDate
2023-06-08T15:48:02Z
lastModifiedDate
2025-07-20
VersionNumber
2
Introduction

Running AQL commands in a Windows environment using PowerShell requires a slightly different approach. This article will guide you through the process of executing AQL queries in a Windows machine using PowerShell. By following the steps outlined below, you will be able to seamlessly run AQL commands and retrieve the desired results.



Step-by-Step Guide

 

Step 1: Set up the necessary variables

To begin, you need to set up the required variables in PowerShell. Replace <username>, <password>, and <url> with the appropriate values. This will enable authentication and specify the Artifactory platform's URL.

$username = "<username>"
$password = "<password>"
$platformUrl = "http://<url>/artifactory/api/search/aql"
Step 2: Establish the authorization and content type headers

To authenticate your request, you need to encode your username and password using Base64. PowerShell provides a convenient way to achieve this. Additionally, you need to specify the content type as "text/plain" for the request headers.

$base64Auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${username}:${password}"))
$headers = @{
    "Authorization" = "Basic $base64Auth"
    "Content-Type" = "text/plain"
}
Step 3: Define the AQL query

Next, you need to define your AQL query within the PowerShell script. Replace the value under the single quote (‘items.find(...)’)  with your specific query. For example, let's consider a query that finds items created or modified within the last day:

$data = @'
items.find(
    {
        "$or": [
            {"created": {"$last": "1d"}},
            {"modified": {"$last": "1d"}}
        ]
    }
)
'@


Step 4: Execute the AQL command and retrieve the results

With all the necessary setup in place, you can now execute the AQL command using the Invoke-RestMethod cmdlet. This will send a POST request to the Artifactory platform, passing the authentication headers and the AQL query. The results will be stored in the $result variable.

$result = Invoke-RestMethod -Uri $platformUrl -Method POST -Headers $headers -Body $data
Step 5: Display the results

To present the results in a readable format, you can use the Format-List cmdlet. This will provide a detailed view of the output.

$result | Format-List *

Here is an example output from the AQL query that finds items created or modified in the last day:

results : {@{repo=tst-gems-remote-cache; path=.; name=specs.4.8.gz; type=file; size=5018252; created=2023-05-30T14:30:44.584Z;
created_by=_system_; modified=2023-06-07T08:21:30.062Z; modified_by=_system_; updated=2023-06-07T08:21:30.290Z},
          @{repo=tst-gems-remote-cache; path=.; name=latest_specs.4.8.gz; type=file; size=1494120; created=2023-05-30T14:30:44.630Z;
created_by=_system_; modified=2023-06-07T08:21:30.318Z; modified_by=_system_; updated=2023-06-07T08:21:30.381Z},

        
The full command input is as follows:

$username = "<username>"
$password = "<password>"
$platformUrl = "http://<url>/artifactory/api/search/aql"

$base64Auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${username}:${password}"))
$headers = @{
    "Authorization" = "Basic $base64Auth"
    "Content-Type" = "text/plain"
}

$data = @'
<AQL query>
'@

$result = Invoke-RestMethod -Uri $platformUrl -Method POST -Headers $headers -Body $data
$result | Format-List *


For more information, refer to the AQL documentation.