Iteration on Powershell

Copper Contributor

Hi Folks!!

 

Below is the requirement which i need to implement.

This is in context to Power BI CMDLET in PowerShell.

 

Step :1 -> Get-PowerBIWorkspace

                <This will give all the workspaces we have in our organization>

Step :2 -> Get-PowerBIDataFlow -WorkSpaceID <id of workspace>.

                  I need to pass WorkSpaceID retrieved in Step-1 one by one to Step-2 WorkSpaceID.

 

How to write PowerShell script that can do this activity, i.e. passing WorkSpaceID from step 1 to step 2 one by one and consolidate the output to one CSV file.

 

Please suggest!!

Thanks

Amit 

 

 

7 Replies

@amsrivas28 

You need to store the result of Get-PowerBIWorkSpace into variable and the use the variable in Step to

so it will be like this

$Result=Get-PowerBIWorkspace
Get-PowerBIDataFlow -WorkSpaceID $Result

This should help.

and also it might depend on the output of the first line

 

 

-----

If this answer helpful, please click on best Respone

 

@amsrivas28 

So Get-PowerBIWorkspace will show (by default) the first 100 workspaces your user is assigned to, sso you'll probably want to get all workspaces you're assigned to by calling Get-PowerBIWorkspace -All.

 

I've not got any PowerBI workspaces that I can get "Get-PowerBIDataFlow" to run on but it apparently returns an object, which can be passed to ConvertTo-Csv, then pipe that to Out-File to stick it in a .csv. So hopefully something like this will do the job:

$workspaces = Get-PowerBIWorkspace -All
foreach ($workspace in $workspaces) {
  Get-PowerBIDataFlow -Workspace $workspace | ConvertTo-Csv | Out-File -FilePath "c:\output.csv" -Append
}

 

If not then you'll have to manipulate the data a bit - not sure what that cmdlet returns so can't say what that would be:

 

 

 

 

 

$workspaces = Get-PowerBIWorkspace -All
foreach ($workspace in $workspaces) {
  $data = Get-PowerBIDataFlow -Workspace $workspace 
  #do something with $data to get what you need into a variable called $csv
  $csv | Out-File -FilePath "c:\output.csv" -Append
}

 

 

Edit: Second attempt... first attempt replied to the wrong post and didn't read the question properly, haha.

@CoasterKaty  :) :)

 

Its works though.:)

 

Just a little more to ask, I want Workspace ID as well in the output file which is passed to second command.

 

Will this be possible.

Thanks

Amit

@amsrivas28 Should be, if you can get me an example of what 

Get-PowerBIDataFlow

returns (as I don't have any PowerBI with data flows set up to try it with) I'll probably be able to put some code together. 

@CoasterKaty 

Post running this - foreach ($workspace in $Workspaces) {Get-PowerBIDataFlow -Workspace $workspace}

 

I am getting below details

 

Id : 744b66d7-ce33-47v1-89fe-a3b4r944fdd4
Name : Dataflow Name
Description :
ModelUrl :
ConfiguredBy : abc2@abc.com

 

I want to include Workspace Id which i have passed to $Workspace above {Get-PowerBIDataFlow -Workspace $workspace} as part of the output

 

Please suggest

 

Amit 

@amsrivas28 In theory this should work:

 

$workspaces = Get-PowerBIWorkspace -All
"WorkspaceId,WorkspaceName,Id,Name,Description,ModelUrl,ConfiguredBy" | Out-File -FilePath "c:\output.csv"
foreach ($workspace in $workspaces) {
  $data = Get-PowerBIDataFlow -Workspace $workspace 
  $csv = $workspace.Id.ToString() + "," +  $workspace.Name.ToString() + "," + $data.Id.ToString() + "," + $data.Name.ToString() + "," + $data.Description.ToString() + "," + $data.ModelUrl.ToString() + "," + $data.ConfiguredBy.ToString()
  $csv | Out-File -FilePath "c:\output.csv" -Append
}

to give you the workspace ID and Name, plus the data from the Get-PowerBIDataFlow cmdlet.

 

@CoasterKaty 

 

I am getting below error post execution of the provided scripts.

 

Error : 
You cannot call a method on a null-valued expression.
At line:4 char:3
+ $csv = $workspace.Id.ToString() + "," + $workspace.Name.ToString() ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

 

 

Please suggest!!

Thanks

Amit