Post

UPDATE: Using PowerShell with Jamf Pro API

Screenshot

If you haven’t read the post on using Jamf Pro API with Azure Runbooks, please see this: https://techitout.xyz/2024/02/03/automate-jamf-management-framework-redeployment-with-an-azure-runbook/

It seems that with recent changes with Jamf Pro, the script that was posted in that article was no longer working to get a bearer token (thanks @Matthew Cornell/@whetmat for letting me know!)

Updated Get-BearerToken function

PowerShell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Get a bearer token for Jamf Pro API authentication
function Get-BearerToken {
    # Prepare user credentials for generating a token
    $global:bearerTokenInformation = @{        
    }

    # Add header information to the bearerTokenInformation hash table
    $bearerTokenAuthHeaders = @{
        "Content-Type" = "application/x-www-form-urlencoded"
    }

    # Add body content for the request
    $bodyContent = @{
        client_id = $jamfProInformation['client_id']
        client_secret = $jamfProInformation['client_secret']
        grant_type = "client_credentials"
    }

    # Get a bearer token
    $bearerTokenAuthResponse = Invoke-WebRequest -Uri "$($jamfProInformation['URI'])/api/oauth/token" -Headers $bearerTokenAuthHeaders -Method Post -Body $bodyContent -ContentType "application/x-www-form-urlencoded"

    # Check the response code and see if it was not successful (IF) or if it was successful (ELSE)
    if ($($bearerTokenAuthResponse).StatusCode -ne 200) {
        Write-Output ""
        Write-Output "Error generating token. Status code: $($($bearerTokenAuthResponse).StatusCode)."
        exit
    }
    else {
        $bearerTokenInformation.Add("Token", "$(($bearerTokenAuthResponse).content | ConvertFrom-Json | Select-Object -ExpandProperty "access_token")")
    }    
}

After testing, it looks like the rest of the script is still good to go. Reach out if you happen to notice anything else that seems a bit off. Thanks!

This post is licensed under CC BY 4.0 by the author.