Skip to content

Instantly share code, notes, and snippets.

@jamesbroome
Last active November 10, 2021 16:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesbroome/c84b599e42f37cf53b02105af21981b1 to your computer and use it in GitHub Desktop.
Save jamesbroome/c84b599e42f37cf53b02105af21981b1 to your computer and use it in GitHub Desktop.
# Set local variables from ADO Release environment variables
$DeploymentPrincipalAppId = "$(DeploymentPrincipalAppId)"
$DeploymentPrincipalAppSecret = "$(DeploymentPrincipalAppSecret)"
$TenantId = "$(TenantId)"
$WorkspaceName = "$(WorkspaceName)"
$ObjectId = "$(ObjectId)"
$AccessRights = "$(AccessRights)"
$PrincipalType = "$(PrincipalType)"
$moduleName = "MicrosoftPowerBIMgmt.Profile"
$module = Get-Module $moduleName -ListAvailable -ErrorAction SilentlyContinue
if (!$module)
{
Install-Module -Name $moduleName -Force -Scope CurrentUser -SkipPublisherCheck
}
$credentials = New-Object System.Management.Automation.PSCredential ($DeploymentPrincipalAppId, (convertto-securestring $DeploymentPrincipalAppSecret -asplaintext -force))
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credentials -Tenant $TenantId
$results = Invoke-PowerBIRestMethod -Url ('groups?$filter=name eq ''' + $WorkspaceName + '''') -Method GET | ConvertFrom-Json
if ($results.value)
{
Write-Host "Power BI App Workspace $WorkspaceName already exists..."
}
else
{
$body = '{"name": "' + $WorkspaceName + '"}'
$workspace = Invoke-PowerBIRestMethod -Url 'groups' -Method POST -Body $body | ConvertFrom-Json
Write-Host "Created Power BI App Workspace..."
$body = '{"identifier": "' + $ObjectId + '","groupUserAccessRight": "' + $AccessRights + '","principalType": "' + $PrincipalType + '"}'
Invoke-PowerBIRestMethod -Url ('groups/' + $workspace.Id + ' /users') -Method POST -Body $body
Write-Host "Added $AccessRights permission to $PrincipalType $ObjectId..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment