functions/Get-PBMWorkspaceUser.ps1
<# .SYNOPSIS Returns user info for a workspace or all workspaces .DESCRIPTION This will return user & role informations for a specified Workspace or in all workspaces if no Workspace parameter is provided .PARAMETER authToken This is the required API authentication token (string) generated by the Get-PBIAuthTokenUnattended or Get-PBIAuthTokenPrompt commands. .PARAMETER workspaceID Optional parameter to restrict data to a specific Workspace ID .PARAMETER workspaceName Optional parameter to restrict data to a specific Workspace Name. The Workspace ID is retrieved using this name by the function .EXAMPLE Get-PBMWorkspaceUser -authToken $auth Get-PBMWorkspaceUser -authToken $auth -workspaceID 1530055f-XXXX-XXXX-XXXX-ee8c87e4a648 Get-PBMWorkspaceUser -authToken $auth -workspaceName 'Workspace Name' .NOTES General notes #> function Get-PBMWorkspaceUser { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [string] $authToken, [string] $workspaceID, [string] $workspaceName ) Begin { Write-Verbose 'Building Rest API header with authorization token' $authHeader = @{ 'Content-Type' = 'application/json' 'Authorization' = 'Bearer ' + $authToken } } Process { try { if ($workspaceID) { Write-Verbose 'Returning Users for specified Workspace' $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($WorkspaceID)/users" $workspaceUsers = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET $workspaceUsers.value | Add-Member -NotePropertyName "WorkspaceID" -NotePropertyValue $WorkspaceID } elseif ($workspaceName) { Write-Verbose 'Workspace Name provided. Matching to ID & building API call' $workspace = Get-PBMWorkspace -authToken $authToken -workspaceName $workspaceName Write-Verbose 'Returning Users for specified Workspace' $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($workspace.id)/users" $workspaceUsers = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET $workspaceUsers.value | Add-Member -NotePropertyName "WorkspaceID" -NotePropertyValue $workspace.id } else { Write-Verbose 'Fetching all Workspaces' $workspaces = Get-PBMWorkspace -authToken $authToken $workspaceUsers = @() Write-Verbose 'Returning Users for all Workspaces' foreach ($workspace in $workspaces) { $uri = "https://api.powerbi.com/v1.0/myorg/groups/$($Workspace.id)/users" $users = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET $users.value | Add-Member -NotePropertyName "WorkspaceID" -NotePropertyValue $Workspace.id $workspaceUsers += $users } } } catch { Write-Error "Error calling REST API: $($_.Exception.Message)" } } End { return $workspaceUsers.Value } } |