Jamf UAPI and Powershell scripting

hdsreid
Contributor III

Hey guys, powershell has worked great for me in the past with classic API, but it is giving me fits while trying to use the UAPI for the first time.

I need to script swapping computers between prestage scopes, but this is not available in the classic API.

This is my code so far:

function getAuth()
{
    $url = "https://mytenant.jamfcloud.com/uapi/auth/tokens"
    $creds = Get-Credential
    $call = Invoke-RestMethod -Method Post -Credential $creds -Authentication Basic -Uri $url -ContentType "application/json;charset=UTF-8"
    return $call

}

$token = getAuth
$token = $token.token

function apiCall()
{
    $url = "https://mytenant.jamfcloud.com/uapi/v1/computer-prestages/4"
    $headers = @{
        Authorization = "Bearer $token"
    }
    $api = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ContentType "application/json;charset=UTF-8"
    return $api
}

Same exact thing works fine with bash, but I get errors with powershell....

$api = Invoke-RestMethod -Method Get -Uri $url -Headers $headers ...
~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (Method: GET, Requesu2026PowerShell/6.2.4
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

basically im passing credentials to get a token, then trying to authenticate with the token. the token seems valid, and I can do a GET for all prestages as opposed to just doing a GET on specific scope. The account I am testing with is a global admin, so I should not have any access restrictions, unless it is a problem with the token (which works on other calls)

3 REPLIES 3

dkmansion
New Contributor II

Your code worked for me in PowerShell Core 6.2.4 using my on-prem jamf tenant and credentials.
I substituted a valid prestage ID in my system and received the object back without issue.

I am using VERSION 10.17.0-t1572881164 currently.

danne
New Contributor

I know this is and old thread, but when using the new JAMF API (with PS 5.1) you need to build authentication headers manually: check this: 

https://stackoverflow.com/questions/27951561/use-invoke-webrequest-with-a-username-and-password-for-... 

Look at the section:  Manually build the headers.

You will surely use this approach for other commerial API

robjschroeder
Contributor

Hey all, I have some more examples of using the Jamf Pro API with Powershell if you wanted to take a look: 

https://techitout.xyz/2024/02/03/automate-jamf-management-framework-redeployment-with-an-azure-runbo...