Skip to content

Instantly share code, notes, and snippets.

@junecastillote
Last active May 14, 2019 12:48
Show Gist options
  • Save junecastillote/19d233c7f12c968db41ef3ab2e655bfc to your computer and use it in GitHub Desktop.
Save junecastillote/19d233c7f12c968db41ef3ab2e655bfc to your computer and use it in GitHub Desktop.
PowerBI Workspace List Export
#must have MicrosoftPowerBIMgmt module installed (Install-Module -Name MicrosoftPowerBIMgmt)
#must have PowerBI Admin rights
#must be connected to PowerBI as admin
$credential = Get-Credential
Connect-PowerBIServiceAccount -Credential $credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
$pbws = Get-PowerBIWorkspace -Scope Organization -ALL | Sort-Object Name
$workSpaceCollection = @()
$dashBoardCollection = @()
$i=1
foreach ($ws in $pbws) {
Write-Host "WorkSpace [$i / $($pbws.count)] : $($ws.Name)"
$i++
#Region WorkSpace and Users
#type if not GROUP
if ($ws.Type -ne 'Group')
{
foreach ($user in $ws.users)
{
$wsTemp = "" | Select-Object wsId,wsName,wsDescription,wsType,wsState,wsIsReadOnly,wsIsOrphaned,wsIsOnDedicatedCapacity,wsCapacityId,wsUser,wsAccessRight
$wsTemp.wsId = $ws.Id
$wsTemp.wsName = $ws.Name
$wsTemp.wsDescription = $ws.Description
$wsTemp.wsType = $ws.Type
$wsTemp.wsState = $ws.State
$wsTemp.wsIsReadOnly = $ws.IsReadOnly
$wsTemp.wsIsOrphaned = $ws.IsOrphaned
$wsTemp.wsIsOnDedicatedCapacity = $ws.IsOnDedicatedCapacity
$wsTemp.wsCapacityId = $ws.CapacityId
$wsTemp.wsUser = $user.UserPrincipalName
$wsTemp.wsAccessRight = $user.AccessRight
if (!$user.UserPrincipalName)
{
$wsTemp.wsUser = ""
$wsTemp.wsAccessRight = ""
}
$workSpaceCollection += $wsTemp
}
}
#type is GROUP
elseif ($ws.Type -eq 'Group')
{
$groupOwners = Get-UnifiedGroupLinks $ws.id.ToString() -LinkType Owner -ErrorAction SilentlyContinue
#owners found
if ($groupOwners)
{
foreach ($groupOwner in $groupOwners)
{
$wsTemp = "" | Select-Object wsId,wsName,wsDescription,wsType,wsState,wsIsReadOnly,wsIsOrphaned,wsIsOnDedicatedCapacity,wsCapacityId,wsUser,wsAccessRight
$wsTemp.wsId = $ws.Id
$wsTemp.wsName = $ws.Name
$wsTemp.wsDescription = $ws.Description
$wsTemp.wsType = $ws.Type
$wsTemp.wsState = $ws.State
$wsTemp.wsIsReadOnly = $ws.IsReadOnly
$wsTemp.wsIsOrphaned = $ws.IsOrphaned
$wsTemp.wsIsOnDedicatedCapacity = $ws.IsOnDedicatedCapacity
$wsTemp.wsCapacityId = $ws.CapacityId
$wsTemp.wsUser = $groupOwner.WindowsLiveID
$wsTemp.wsAccessRight = "Admin"
$workSpaceCollection += $wsTemp
}
}
#no owners found
elseif (!$groupOwners)
{
$wsTemp = "" | Select-Object wsId,wsName,wsDescription,wsType,wsState,wsIsReadOnly,wsIsOrphaned,wsIsOnDedicatedCapacity,wsCapacityId,wsUser,wsAccessRight
$wsTemp.wsId = $ws.Id
$wsTemp.wsName = $ws.Name
$wsTemp.wsDescription = $ws.Description
$wsTemp.wsType = $ws.Type
$wsTemp.wsState = $ws.State
$wsTemp.wsIsReadOnly = $ws.IsReadOnly
$wsTemp.wsIsOrphaned = $ws.IsOrphaned
$wsTemp.wsIsOnDedicatedCapacity = $ws.IsOnDedicatedCapacity
$wsTemp.wsCapacityId = $ws.CapacityId
$wsTemp.wsUser = ""
$wsTemp.wsAccessRight = ""
$workSpaceCollection += $wsTemp
}
}
#EndRegion
#Region WorkSpace and Dashboards
$dashboards = Invoke-PowerBIRestMethod -Url "admin/groups/$($ws.id.Guid)/dashboards" -Method Get -ErrorAction SilentlyContinue
if ($dashboards)
{
$dashboards = ($dashboards | ConvertFrom-JSON).value
foreach ($dashboard in $dashboards)
{
$dbTemp = "" | Select-Object wsID,wsName,dbID,dbDisplayName,dbEmbedURL
$dbTemp.wsID = $ws.Id
$dbTemp.wsName = $ws.Name
$dbTemp.dbID = $dashboard.Id
$dbTemp.dbDisplayName = $dashboard.DisplayName
$dbTemp.dbEmbedURL = $dashboard.EmbedURL
$dashBoardCollection += $dbTemp
}
}
#EndRegion
}
#Export Results
$workSpaceCollection | Export-Csv -NoTypeInformation .\workspaceAndUser.csv
$dashBoardCollection | Export-Csv -NoTypeInformation .\workspaceAndDashboard.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment