UPDATE: Using PowerShell with Jamf Pro API

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](MacAdmins Slack/GitHub) for letting me know!)

Updated Get-BearerToken function

PowerShell
# 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!

Leave a Reply

Discover more from Tech IT Out

Subscribe now to keep reading and get access to the full archive.

Continue reading