Iteration on Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-2119889%22%20slang%3D%22en-US%22%3EIteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2119889%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Folks!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20the%20requirement%20which%20i%20need%20to%20implement.%3C%2FP%3E%3CP%3EThis%20is%20in%20context%20to%20Power%20BI%20CMDLET%20in%20PowerShell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStep%20%3A1%20-%26gt%3B%26nbsp%3BGet-PowerBIWorkspace%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CTHIS%20will%3D%22%22%20give%3D%22%22%20all%3D%22%22%20the%3D%22%22%20workspaces%3D%22%22%20we%3D%22%22%20have%3D%22%22%20in%3D%22%22%20our%3D%22%22%20organization%3D%22%22%3E%3C%2FTHIS%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EStep%20%3A2%20-%26gt%3B%20Get-PowerBIDataFlow%20-WorkSpaceID%20%26lt%3B%3CSTRONG%3Eid%20of%20workspace%3C%2FSTRONG%3E%26gt%3B.%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20I%20need%20to%20pass%20WorkSpaceID%20retrieved%20in%20Step-1%20one%20by%20one%20to%20Step-2%20WorkSpaceID.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20to%20write%20PowerShell%20script%20that%20can%20do%20this%20activity%2C%20i.e.%20passing%20WorkSpaceID%20from%20step%201%20to%20step%202%20one%20by%20one%20and%20consolidate%20the%20output%20to%20one%20CSV%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20suggest!!%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAmit%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2119889%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2121500%22%20slang%3D%22en-US%22%3ERe%3A%20Iteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2121500%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F755992%22%20target%3D%22_blank%22%3E%40amsrivas28%3C%2FA%3E%26nbsp%3BShould%20be%2C%20if%20you%20can%20get%20me%20an%20example%20of%20what%26nbsp%3B%3C%2FP%3E%3CPRE%3EGet-PowerBIDataFlow%3C%2FPRE%3E%3CP%3Ereturns%20(as%20I%20don't%20have%20any%20PowerBI%20with%20data%20flows%20set%20up%20to%20try%20it%20with)%20I'll%20probably%20be%20able%20to%20put%20some%20code%20together.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2121291%22%20slang%3D%22en-US%22%3ERe%3A%20Iteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2121291%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F942392%22%20target%3D%22_blank%22%3E%40CoasterKaty%3C%2FA%3E%26nbsp%3B%20%3A)%3C%2Fimg%3E%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIts%20works%20though.%3A)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20a%20little%20more%20to%20ask%2C%20I%20want%20Workspace%20ID%20as%20well%20in%20the%20output%20file%20which%20is%20passed%20to%20second%20command.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWill%20this%20be%20possible.%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3CP%3EAmit%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2120992%22%20slang%3D%22en-US%22%3ERe%3A%20Iteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2120992%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F755992%22%20target%3D%22_blank%22%3E%40amsrivas28%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20Get-PowerBIWorkspace%20will%20show%20(by%20default)%20the%20first%20100%20workspaces%20your%20user%20is%20assigned%20to%2C%20sso%20you'll%20probably%20want%20to%20get%20all%20workspaces%20you're%20assigned%20to%20by%20calling%20Get-PowerBIWorkspace%20-All.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20not%20got%20any%20PowerBI%20workspaces%20that%20I%20can%20get%20%22Get-PowerBIDataFlow%22%20to%20run%20on%20but%20it%20apparently%20returns%20an%20object%2C%20which%20can%20be%20passed%20to%20ConvertTo-Csv%2C%20then%20pipe%20that%20to%20Out-File%20to%20stick%20it%20in%20a%20.csv.%20So%20hopefully%20something%20like%20this%20will%20do%20the%20job%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24workspaces%20%3D%20Get-PowerBIWorkspace%20-All%0Aforeach%20(%24workspace%20in%20%24workspaces)%20%7B%0A%20%20Get-PowerBIDataFlow%20-Workspace%20%24workspace%20%7C%20ConvertTo-Csv%20%7C%20Out-File%20-FilePath%20%22c%3A%5Coutput.csv%22%20-Append%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20not%20then%20you'll%20have%20to%20manipulate%20the%20data%20a%20bit%20-%20not%20sure%20what%20that%20cmdlet%20returns%20so%20can't%20say%20what%20that%20would%20be%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24workspaces%20%3D%20Get-PowerBIWorkspace%20-All%0Aforeach%20(%24workspace%20in%20%24workspaces)%20%7B%0A%20%20%24data%20%3D%20Get-PowerBIDataFlow%20-Workspace%20%24workspace%20%0A%20%20%23do%20something%20with%20%24data%20to%20get%20what%20you%20need%20into%20a%20variable%20called%20%24csv%0A%20%20%24csv%20%7C%20Out-File%20-FilePath%20%22c%3A%5Coutput.csv%22%20-Append%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEdit%3A%20Second%20attempt...%20first%20attempt%20replied%20to%20the%20wrong%20post%20and%20didn't%20read%20the%20question%20properly%2C%20haha.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2120676%22%20slang%3D%22en-US%22%3ERe%3A%20Iteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2120676%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F755992%22%20target%3D%22_blank%22%3E%40amsrivas28%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20need%20to%20store%20the%20result%20of%20Get-PowerBIWorkSpace%20into%20variable%20and%20the%20use%20the%20variable%20in%20Step%20to%3C%2FP%3E%3CP%3Eso%20it%20will%20be%20like%20this%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Result%3DGet-PowerBIWorkspace%0AGet-PowerBIDataFlow%20-WorkSpaceID%20%24Result%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThis%20should%20help.%3C%2FP%3E%3CP%3Eand%20also%20it%20might%20depend%20on%20the%20output%20of%20the%20first%20line%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-----%3C%2FP%3E%3CP%3EIf%20this%20answer%20helpful%2C%20please%20click%20on%20best%20Respone%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2136061%22%20slang%3D%22en-US%22%3ERe%3A%20Iteration%20on%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2136061%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F942392%22%20target%3D%22_blank%22%3E%40CoasterKaty%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPost%20running%20this%20-%26nbsp%3Bforeach%20(%24workspace%20in%20%24Workspaces)%20%7BGet-PowerBIDataFlow%20-Workspace%20%24workspace%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20getting%20below%20details%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EId%20%3A%20744b66d7-ce33-47v1-89fe-a3b4r944fdd4%3CBR%20%2F%3EName%20%3A%20Dataflow%20Name%3CBR%20%2F%3EDescription%20%3A%3CBR%20%2F%3EModelUrl%20%3A%3CBR%20%2F%3EConfiguredBy%20%3A%20%3CA%20href%3D%22mailto%3Aabc2%40abc.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eabc2%40abc.com%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20include%20Workspace%20Id%20which%20i%20have%20passed%20to%20%24Workspace%20above%20%7BGet-PowerBIDataFlow%20-Workspace%20%24workspace%7D%20as%20part%20of%20the%20output%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20suggest%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAmit%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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