Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Required Variables that you need to replace
- $apiToken = "your_api_token_here"
- $baseDomain = "companydomain" # e.g. "companydomain" for https://companydomain.atlassian.net
- $objectIds = @{
- Laptops = 140
- Software = 141
- }
- $attributeIds = @{
- "All Software" = 695
- "Software 1" = 696
- "Software 2" = 697
- }
- # Validate required variables
- if (-not $emailAddress -or -not $apiToken -or -not $baseDomain) {
- Write-Error "Missing required variables. Please ensure emailAddress, apiToken, and baseDomain are set."
- exit 1
- }
- # Function to encode credentials for Atlassian Basic Auth
- function Create-AtlassianBasicAuthCredential {
- [CmdletBinding()]
- param(
- [string]$emailAddress,
- [string]$apiToken
- )
- try {
- $authString = "$($emailAddress):$($apiToken)"
- $bytes = [System.Text.Encoding]::UTF8.GetBytes($authString)
- $base64String = [Convert]::ToBase64String($bytes)
- return $base64String
- } catch {
- Write-Error "Failed to create Basic Auth credential: $($_.Exception.Message)"
- exit 1
- }
- }
- # Create the auth header
- $headers = @{
- Authorization = "Basic $(Create-AtlassianBasicAuthCredential -emailAddress $emailAddress -apiToken $apiToken)"
- }
- # Get workspace ID
- try {
- $workspaceResponse = Invoke-RestMethod -Uri "https://$baseDomain.atlassian.net/rest/servicedeskapi/assets/workspace" -Headers $headers -ErrorAction Stop
- $workspaceId = $workspaceResponse.values.workspaceId
- if (-not $workspaceId) {
- Write-Error "No workspaceId returned from API. Check your credentials and permissions."
- exit 1
- }
- } catch {
- Write-Error "Failed to retrieve workspaceId. $($_.Exception.Message)"
- if ($_.Exception.Response -ne $null) {
- Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
- }
- exit 1
- }
- # Set Asset API base URL
- $assetsApiBaseUrl = "https://api.atlassian.com/jsm/assets/workspace/$workspaceId/v1"
- # Endpoints
- $endpoints = @{
- aql = "$assetsApiBaseUrl/object/aql"
- object = "$assetsApiBaseUrl/object"
- objectAttributes = "$assetsApiBaseUrl/object/attributes"
- }
- # Get all laptops using AQL
- $aqlQuery = "ObjectTypeId = $($objectIds.Laptops)"
- $aqlBody = @{ qlQuery = "$aqlQuery" } | ConvertTo-Json
- $isLast = $false
- $startAt = 0
- $laptops = @()
- while (-not $isLast) {
- try {
- $aqlResponse = Invoke-RestMethod -Uri ($endpoints.aql + "?startAt=$startAt&includeAttributes=True") `
- -Method Post -Headers $headers -Body $aqlBody `
- -ContentType "application/json" -ErrorAction Stop
- $laptops += $aqlResponse.values
- $isLast = $aqlResponse.isLast
- $startAt += $aqlResponse.maxResults
- } catch {
- Write-Error "Failed to query laptops with AQL. $($_.Exception.Message)"
- if ($_.Exception.Response -ne $null) {
- Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
- }
- exit 1
- }
- }
- if ($laptops.Count -eq 0) {
- Write-Warning "No laptops found with the provided AQL query."
- exit 0
- }
- # Loop through each laptop and update its software attribute
- foreach ($laptop in $laptops) {
- try {
- $software = ($laptop.attributes | Where-Object {
- $_.objectTypeAttributeId -eq $attributeIds."Software 1" -or
- $_.objectTypeAttributeId -eq $attributeIds."Software 2"
- }).objectAttributeValues.referencedObject.objectKey
- if (-not $software) {
- Write-Warning "Laptop $($laptop.id) has no software attributes to update."
- continue
- }
- # Build update body
- $updateBody = @{
- attributes = @(
- @{
- objectTypeAttributeId = $attributeIds."All Software"
- objectAttributeValues = @(
- foreach ($s in $software) {
- @{ value = $s }
- }
- )
- }
- )
- } | ConvertTo-Json -Depth 5
- # Send update request
- $updateResponse = Invoke-RestMethod -Uri ($endpoints.object + "/$($laptop.id)") `
- -Method Put -Headers $headers -Body $updateBody `
- -ContentType "application/json" -ErrorAction Stop
- Write-Host "Successfully updated laptop $($laptop.id) with software entries."
- } catch {
- Write-Error "Failed to update laptop $($laptop.id). $($_.Exception.Message)"
- if ($_.Exception.Response -ne $null) {
- Write-Error "HTTP Status: $($_.Exception.Response.StatusCode.value__) $($_.Exception.Response.StatusDescription)"
- }
- }
- }
Add Comment
Please, Sign In to add comment