Guest User

Jira Asset Management - Merge Attributes

a guest
Oct 7th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     # Required Variables that you need to replace
  2.     $emailAddress = "[email protected]"
  3.     $apiToken     = "your_api_token_here"
  4.     $baseDomain   = "companydomain" # e.g. "companydomain" for https://companydomain.atlassian.net
  5.    
  6.     $objectIds = @{
  7.         Laptops  = 140
  8.         Software = 141
  9.     }
  10.     $attributeIds = @{
  11.         "All Software" = 695
  12.         "Software 1"   = 696
  13.         "Software 2"   = 697
  14.     }
  15.    
  16.     # Validate required variables
  17.     if (-not $emailAddress -or -not $apiToken -or -not $baseDomain) {
  18.         Write-Error "Missing required variables. Please ensure emailAddress, apiToken, and baseDomain are set."
  19.         exit 1
  20.     }
  21.    
  22.     # Function to encode credentials for Atlassian Basic Auth
  23.     function Create-AtlassianBasicAuthCredential {
  24.         [CmdletBinding()]
  25.         param(
  26.             [string]$emailAddress,
  27.             [string]$apiToken
  28.         )
  29.         try {
  30.             $authString   = "$($emailAddress):$($apiToken)"
  31.             $bytes        = [System.Text.Encoding]::UTF8.GetBytes($authString)
  32.             $base64String = [Convert]::ToBase64String($bytes)
  33.             return $base64String
  34.         } catch {
  35.             Write-Error "Failed to create Basic Auth credential: $($_.Exception.Message)"
  36.             exit 1
  37.         }
  38.     }
  39.    
  40.     # Create the auth header
  41.     $headers = @{
  42.         Authorization = "Basic $(Create-AtlassianBasicAuthCredential -emailAddress $emailAddress -apiToken $apiToken)"
  43.     }
  44.    
  45.     # Get workspace ID
  46.     try {
  47.         $workspaceResponse = Invoke-RestMethod -Uri "https://$baseDomain.atlassian.net/rest/servicedeskapi/assets/workspace" -Headers $headers -ErrorAction Stop
  48.         $workspaceId = $workspaceResponse.values.workspaceId
  49.         if (-not $workspaceId) {
  50.             Write-Error "No workspaceId returned from API. Check your credentials and permissions."
  51.             exit 1
  52.         }
  53.     } catch {
  54.         Write-Error "Failed to retrieve workspaceId. $($_.Exception.Message)"
  55.         if ($_.Exception.Response -ne $null) {
  56.             Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
  57.         }
  58.         exit 1
  59.     }
  60.    
  61.     # Set Asset API base URL
  62.     $assetsApiBaseUrl = "https://api.atlassian.com/jsm/assets/workspace/$workspaceId/v1"
  63.    
  64.     # Endpoints
  65.     $endpoints = @{
  66.         aql              = "$assetsApiBaseUrl/object/aql"
  67.         object           = "$assetsApiBaseUrl/object"
  68.         objectAttributes = "$assetsApiBaseUrl/object/attributes"
  69.     }
  70.    
  71.     # Get all laptops using AQL
  72.     $aqlQuery = "ObjectTypeId = $($objectIds.Laptops)"
  73.     $aqlBody  = @{ qlQuery = "$aqlQuery" } | ConvertTo-Json
  74.    
  75.     $isLast   = $false
  76.     $startAt  = 0
  77.     $laptops  = @()
  78.    
  79.     while (-not $isLast) {
  80.         try {
  81.             $aqlResponse = Invoke-RestMethod -Uri ($endpoints.aql + "?startAt=$startAt&includeAttributes=True") `
  82.                                              -Method Post -Headers $headers -Body $aqlBody `
  83.                                              -ContentType "application/json" -ErrorAction Stop
  84.             $laptops += $aqlResponse.values
  85.             $isLast   = $aqlResponse.isLast
  86.             $startAt += $aqlResponse.maxResults
  87.         } catch {
  88.             Write-Error "Failed to query laptops with AQL. $($_.Exception.Message)"
  89.             if ($_.Exception.Response -ne $null) {
  90.                 Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
  91.             }
  92.             exit 1
  93.         }
  94.     }
  95.    
  96.     if ($laptops.Count -eq 0) {
  97.         Write-Warning "No laptops found with the provided AQL query."
  98.         exit 0
  99.     }
  100.    
  101.     # Loop through each laptop and update its software attribute
  102.     foreach ($laptop in $laptops) {
  103.         try {
  104.             $software = ($laptop.attributes | Where-Object {
  105.                 $_.objectTypeAttributeId -eq $attributeIds."Software 1" -or
  106.                 $_.objectTypeAttributeId -eq $attributeIds."Software 2"
  107.             }).objectAttributeValues.referencedObject.objectKey
  108.    
  109.             if (-not $software) {
  110.                 Write-Warning "Laptop $($laptop.id) has no software attributes to update."
  111.                 continue
  112.             }
  113.    
  114.             # Build update body
  115.             $updateBody = @{
  116.                 attributes = @(
  117.                     @{
  118.                         objectTypeAttributeId = $attributeIds."All Software"
  119.                         objectAttributeValues = @(
  120.                             foreach ($s in $software) {
  121.                                 @{ value = $s }
  122.                             }
  123.                         )
  124.                     }
  125.                 )
  126.             } | ConvertTo-Json -Depth 5
  127.    
  128.             # Send update request
  129.             $updateResponse = Invoke-RestMethod -Uri ($endpoints.object + "/$($laptop.id)") `
  130.                                                 -Method Put -Headers $headers -Body $updateBody `
  131.                                                 -ContentType "application/json" -ErrorAction Stop
  132.    
  133.             Write-Host "Successfully updated laptop $($laptop.id) with software entries."
  134.    
  135.         } catch {
  136.             Write-Error "Failed to update laptop $($laptop.id). $($_.Exception.Message)"
  137.             if ($_.Exception.Response -ne $null) {
  138.                 Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
  139.             }
  140.         }
  141.     }
  142.    
Add Comment
Please, Sign In to add comment