Forum Discussion
How to add Identity column on PowerShell Result
Try the below script. The error message indicates that the command can't able to convert the custom ps object into Workspace object. So, we can use the parameter "WorkspaceId" and pass Workspace Id to get only dataflows belonging to that workspace.
$array = @()
foreach ($workspace in $Workspaces )
{
$Dataflow = Get-PowerBIDataFlow -WorkspaceId $workspace.id
}
Hi Kevin_Morgan , Thanks for the help.
Can understand, am asking too much 😞 but with your help, it seems I will achieve my requirement.
To be precise, your suggestion works and below code is able to retrieve data as required however need your expertise/help to achieve one more requirement related to the same PowerShell Command, detailed as below
First Part of Code : This is fine 🙂
$Workspaces=@()
$processes = Get-PowerBIWorkspace
$id=0
ForEach ($process in $processes)
{$id++
$Workspaces+=New-Object PsObject -Property ([ordered]@{
Identity=$id
id=$process.Id
Name=$process.Name
})}
Second Part of Code : Requirement is to Iterate FOREACH for 10 records based on IDENTITY column (Identity is a column which is been created in First Part of the code)
$array = @()
foreach ($workspace in $Workspaces)
{
$wspid = $workspace.id
$wspiden = $workspace.Identity
$Dataflow = Get-PowerBIDataFlow -WorkspaceId $wspid
$dflowId = $Dataflow.ID
$array += "$wspiden,$wspid , $dflowId"
}
$body = $array -join "`n"
Actual requirement is to - I need to process records from identity 1 to 10 in one batch, and next records with identity (11-20 ) on another batch and so on. To be precise, I need to come out of the FOREACH after every 10 records.
Please suggest!!
Thanks
Amit Srivastava
- Kevin_MorganApr 16, 2021Iron Contributor
I can't understand your requirement clearly. But what my suggestion is, instead of breaking loop, use the counter variable, temporary array and result array., process 10 items and add the processed items in result array, reset the counter variable and temporary array once the count reached to 10.
$resultArray = @()
$counter=1
$tempArray = @()
foreach ($workspace in $Workspaces)
{
### Your inner loop script ###
$tempArray += "$wspiden,$wspid , $dflowId"
if($counter -eq 10)
{
#Add temp array in result array
$resultArray += $tempArray
#Reset counter and temp array
$array = @()
$counter=1
}
}- amsrivas28Apr 16, 2021Copper Contributor
Hi Kevin_Morgan ,
Let me explain more on requirement.
There is one limitation on PowerShell command, i.e. when passing more than 200 record in a FOREACH loop, loop will crash, specifically for few command which will be executed by Power BI Tenant Admin.
So what requires is to move out from the FOREACH loop after 200th records and loop in for another 200 records (201-400) and so on, i,e, to pass records in the batch of 200 records and each time after 200 record, control move from loop.
That's the reason why I have included identity column in the result set so that on the basis of that, I can iterate the same.
But anyhow, I am not able to achieve my requirement.
Hopefully, this will define my requirement more clearer.
Anyway, via which I can do that will be very helpful.
Thanks
Amit