ARTIFACTORY: Power shell script to get the size of the artifacts present inside respective folders under the corresponding repository
In the article, we will learn how to use a script using Powershell on a Windows environment to get the size of the artifacts present inside corresponding folders under the respective repositories.
Here we are using Access tokens as input Bearer token for authentication to Artifactory for executing the script.
Sample Script:
$ cat script.ps1
# Get folder names - This step first will collect the number of folders that exist in the provided repository and add them to the file “folders.txt”.
$headers = @{
"Authorization" = "Bearer ACCESS-TOKEN";"Content-Type" = "text/plain"}
$folders = (Invoke-RestMethod -Method Post -Uri "https://servername.jfrog.io/artifactory/api/search/aql" -Headers $headers -Body 'items.find({"type":"folder","repo":"repo-name","depth":1})').results.name | Where-Object { $_.length -gt 1 } | Out-File -FilePath "folders.txt"
# Get size of each folder - This step will run for a loop based on the number of folders and accumulate the total size of all the artifacts present in the respective folders and provide the output individually.
foreach ($folder in Get-Content "folders.txt") {
Write-Host "Folder size for -->" $folder
$body = @{ "name" = $folder; "repositoryPath" = "test-repository-local/$folder/" } | ConvertTo-Json
Invoke-RestMethod -Uri "https://servername.jfrog.io/artifactory/api/storage/test-repository-local/$folder/?list&deep=1" -Method GET -Headers $headers | Select-Object -ExpandProperty files | Select-Object -ExpandProperty size | Measure-Object -Sum | Select-Object -ExpandProperty Sum | %{ "{0:N2} {1}" -f ($_ / 1MB), "MB" }
Write-Host
}
Sample output:
$ ./script.ps1 Folder size for --> test-folder1 1,098.48 MB Folder size for --> test-folder2 5.57 MB ……………………… Folder size for --> test-foldern 44.35 MB