Private/PartnerActions/New-CustomPartnerAccessToken.ps1

function New-CustomPartnerAccessToken() {
    param(
        [Parameter(Mandatory)]
        [String]
        $Scopes,
        [Parameter(Mandatory)]
        [String]
        $CustomerTenantId
    )

    begin {
        if (!$SAMTokens) {
            $SAMTokens = Get-SAMTokens
        }  
    }
    process {

        # Connect to the partner center using our application
        $RequestBody = @{
            client_id     = $SAMTokens.ApplicationId
            client_secret = $SAMTokens.ApplicationSecret
            grant_type    = "refresh_token"
            refresh_token = $SAMTokens.RefreshToken
            scope         = $Scopes
        }
        $authEndpoint = "https://login.microsoftonline.com/$($CustomerTenantId)/oauth2/v2.0/token"

        # Get the access token needed for subsequent requests
        $AccessToken = ((Invoke-WebRequest -Uri $authEndpoint -Method POST -Body $RequestBody -ContentType 'application/x-www-form-urlencoded').Content | ConvertFrom-Json).access_token

        if ($AccessToken) {
            Write-Host "GOT A TOKEN WOOHOOOO." -ForegroundColor Green
            Write-Host $AccessToken
        }
    
        return $AccessToken 

    }
}