This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreGet Fabric Certified for FREE during AI Skills Fest. This week only. Secure your voucher now.
If you are following the Power BI blog on a regular basis, you probably have noticed the Power BI APIs and cmdlets announcement for administrators, which introduced a set of APIs and cmdlets to work with workspaces, dashboards, reports, datasets, and so forth in Power BI. But there is much more to this than could be covered in a brief announcement. For starters, the management cmdlets are not just for administrators; they are also for Power BI users and developers. This article takes a closer look to show you how to take advantage of these cmdlets provided your profile fits any of the following target groups:
Let’s cover these points in reverse order beginning with the community aspect. Power BI enjoys a vibrant community committed to helping each other getting the most out of Power BI. Community members frequently trailblaze through uncharted territory with solutions and tools that complement what’s available from Microsoft. PowerShell support for Power BI is a good example. Search for “power bi powershell” and you can find plenty of articles showing you how to use the Power BI APIs from the PowerShell console. You can even find a Microsoft.PowerBI.PowerShell module in the PowerShell Gallery and a collection of cmdlets for working with the PowerBI APIs on GitHub (https://github.com/DevScope/powerbi-powershell-modules). This is great, yet it creates a slight dilemma because cmdlets from different sources don’t work well together and they duplicate functionality while leaving gaps in other areas. It is much better to join forces, coordinate the work, merge what already exists, and deliver the richest possible collection of cmdlets consolidated in a single repository.
You can find this Microsoft-backed GitHub repo at https://github.com/Microsoft/powerbi-powershell. If you are interested in helping build cmdlets for Power BI, check out Contribute Code to PowerShell Cmdlets for Power BI and come on board as a project member. Or simply report any issues you encounter at https://github.com/Microsoft/powerbi-powershell/issues. All help is greatly appreciated.
One advantage of the Power BI management cmdlets is that they are available as a module from the PowerShell Gallery. Simply install them by using the command Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser. Another is that the management cmdlets do not require a separate app registration in Azure. So, if you are a Power BI app developer, it’s very straightforward and quick to prototype solutions that make Power BI Rest API calls. Just log in to Power BI by using the Login-PowerBI cmdlet. It supports user accounts, service principals, and application credentials (key or certificate). Then fire away by using one of the pre-built cmdlets or use the Invoke-PowerBIRestMethod cmdlet.
Invoke-PowerBIRestMethod is the Swiss army knife of Power BI cmdlets. You can use it to construct arbitrary REST API calls against Power BI. So, if you find that you are missing a specific management cmdlet, you can still get the job done if there is an appropriate REST API endpoint for you to call. For all the details about available REST API endpoints, see the Power BI REST API reference.
For working with workspaces, dashboards, reports, datasets, data sources, and imports, you don’t need to go to the Invoke-PowerBIRestMethod cmdlet. Instead, use the more specialized cmdlets Get-PowerBIWorkspace, Get-PowerBIReport, Get-PowerBIDataset, and so forth. These cmdlets are more convenient. Also, take a moment to check out the online help, which you can access through the Get-Help cmdlet. For example, use the command Get-Help Get-PowerBIWorkspace -Online to view the online documentation for working with workspaces.
Among other things, you might notice that almost all management cmdlets have an optional parameter called -Scope. This parameter can take one of two possible values: Individual and Organization. The default is Individual, which only returns those items that you can access as a regular Power BI user. On the other hand, if you are working as a Power BI admin, specify Organization to return all the items of a given type that exist in your Power BI tenant. Only Power BI users with admin rights (such as Office 365 Global Administrator or Power BI Service Administrator) can use the organization scope, but all users can use the individual scope, so the management cmdlets can really help any Power BI user automate repetitive tasks.
The following PowerShell script illustrates the difference between the individual and organization scope. The script illustrates both the use of the Get-PowerBIWorkspace cmdlet as well as comparable requests based on the Invoke-PowerBIRestMethod cmdlet, highlighting the fact that the individual scope invokes the user API whereas the organization scope relies on the admin API.
See also the screenshot below the listing for actual results in a test tenant.
## Login to Power BI using an account with service admin rights.
##
Login-PowerBI
## Get the list of workspaces as a Power BI user.
##
$myWorkspaces = Get-PowerBIWorkspace
Write-Host "The current user has --" $myWorkspaces.Count "-- workspaces."
## Get the list of workspaces as a Power BI admin.
##
$tentantWorkspaces = Get-PowerBIWorkspace -Scope Organization
Write-Host "There are --" $tentantWorkspaces.Count "-- workspaces in this Power BI tenant."
## Get the list of workspaces by making a direct REST API call against the user API.
## /v1.0/myorg/groups
##
$myRestResult = Invoke-PowerBIRestMethod -Url 'Groups' -Method Get | ConvertFrom-Json
Write-Host "The current user has --" $myRestResult.value.Count "-- workspaces."
## Get the list of workspaces by making a direct REST API call against the admin API.
## /v1.0/myorg/admin/groups
##
$tenantRestResult = Invoke-PowerBIRestMethod -Url 'admin/Groups' -Method Get | ConvertFrom-Json
Write-Host "There are --" $tenantRestResult.value.Count "-- workspaces in this Power BI tenant."
What happens if you call an admin API as a regular user without admin rights? As expected, PowerShell will display an error message informing you that the call was unauthorized. If you are among those users who would like to dig a little deeper, use the Resolve-PowerBIError cmdlet. If you run this cmdlet without any parameters, it shows the details for all errors that might have occurred in your current PowerShell session. You can specify -Last as a parameter to analyze only the last error or reference the desired error directly in the -Error parameter, such as -Error $Error[0]. The following script snippet uses the -Last parameter. The returned information highlights that the client tried to issue a GET request against https://api.powerbi.com/v1.0/myorg/admin/groups?$expand=users and that the service returned an HTTP 401 Unauthorized error.
## Login as a regular Power BI user without service admin rights.
##
Login-PowerBI
## Try to work with the organization scope.
##
Get-PowerBIWorkspace -Scope Organization
## Get additional troubleshooting information.
##
Resolve-PowerBIError -Last
Subsequent blog posts will cover typical scripting scenarios in more detail. Let’s finish this article with a brief list of typical tasks that the management cmdlets can help to accomplish efficiently:
That’s it for the moment. As mentioned, subsequent blog posts will dive deeper into some of these scenarios. So, stay tuned for more information about how to work with Power BI in the PowerShell console. And, as always, please send us your feedback via any of the available communication channels such as UserVoice and community forums or simply go to https://github.com/Microsoft/powerbi-powershell/issues. You can influence the evolution of the Power BI management cmdlets to the benefit of all Power BI users!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.