Created
October 3, 2023 08:49
-
-
Save JayDoubleu/8ec4db1cde233220e946dfbcf93eadd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-DatabricksAdminCreation { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, HelpMessage = 'Databricks instance URL')] | |
[string]$databricksUrl, | |
[Parameter(Mandatory = $true, HelpMessage = 'Databricks JWT token')] | |
[string]$bearerToken, | |
[Parameter(Mandatory = $true, HelpMessage = 'Object ID for the user')] | |
[string]$userObjectId, | |
[Parameter(Mandatory = $true, HelpMessage = 'Databricks group ID to associate with the user')] | |
[string]$adminsGroupId, | |
[Parameter(Mandatory = $true, HelpMessage = 'Databricks email of the new user')] | |
[string]$email | |
) | |
# Define the header with the Authorization token | |
$headers = @{ | |
"Authorization" = "Bearer $bearerToken" | |
} | |
# Define the body as a PowerShell object | |
$body = @{ | |
externalId = $userObjectId | |
groups = @( | |
@{ | |
display = "admins" | |
type = "direct" | |
value = $adminsGroupId | |
'$ref' = "Groups/$adminsGroupId" | |
} | |
) | |
userName = $email | |
} | |
$jsonBody = ConvertTo-Json -InputObject $body -Depth 10 | |
try { | |
Invoke-RestMethod -Uri "$databricksUrl/api/2.0/preview/scim/v2/Users" -Method POST -Headers $headers -Body $jsonBody -ContentType "application/json" | |
} | |
catch { | |
$statusCode = $_.Exception.Response.StatusCode.value__ | |
$response = $_ | |
Write-Output "Status Code: $statusCode" | |
if ($statusCode -eq 409) { | |
$responseDetail = ConvertFrom-Json -InputObject $response | |
if ($responseDetail.detail -like "*already exists*") { | |
Write-Output "Response: $($responseDetail.detail)" | |
} | |
} | |
else { | |
Write-Error "Unexpected error: $_" | |
} | |
} | |
} | |
# Get the Databricks JWT token | |
$DATABRICKS_TOKEN = az account get-access-token --resource 2ff814a6-3304-4ab8-85cb-cd0e6f879c1d --query "accessToken" --output tsv | |
# Invoke the Create-DatabricksAdmin function | |
Invoke-DatabricksAdminCreation -databricksUrl "https://adb-xxx.azuredatabricks.net" -bearerToken $DATABRICKS_TOKEN -userObjectId "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx" -adminsGroupId "879630308009886" -email "[email protected]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment