I'm writing a PowerShell script to loop through an organization's PowerBI workspaces and spit out the report IDs, names, and URLs for each workspace - so, a nested loop.
Here's my working script thus far:
Install-Module -Name MicrosoftPowerBIMgmt
$myArray = @()
Login-PowerBIServiceAccount
Get-PowerBIWorkspace -Scope Organization | ForEach-Object {
$ws = $_
Get-PowerBIReport -WorkspaceId $_.Id | ForEach-Object {
$x = [pscustomobject]@{
workspaceId = $ws.Id
workspaceName = $ws.Name
reportId = $_.id
reportName = $_.Name
reportUrl = $_.WebUrl
}
$myArray += $x
}
}
$myArray | Export-Csv "PowerBIWorkspaceReports.csv"
Four questions:
1) Is there a better way to add the module dependency at the top? I can't find any info on how PowerBI looks for existing module dependencies.
2) Are the ForEach-Object calls redundant? I see in simple code people assume whatever command runs after the pipe runs just fine whether on a single object or a collection of objects.
3) Is there a better way to manage my objects? Since I need data from the first loop ( the Workspace name and ID) and the second loop, is there an easier way to join them (or export them as a flattened list) than looping?
4) Should I just append my rows directly to the CSV? There doesn't seem to be the equivalent of While(File.Open() ...) in PowerShell.
And any other advice appreciated.
$myArray = Get-PowerBIWorkspace …
omitting both$myArray = @()
and$myArray +=
(and note that you could omit the auxiliary variable$x
at all). \$\endgroup\$