Jamf UAPI and Powershell scripting

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)


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.