Forum Discussion
How to add Identity column on PowerShell Result
Try the below commands with Get-PowerBIWorkspace to get the required result
$result=@()
$id=0
$processes = Get-Process
ForEach ($process in $processes)
{
$id++
$result+=New-Object PsObject -Property ([ordered]@{
ColA=$id
OtherColumn1=$process.Name
})
}
Hi Kevin_Morgan ,
I am able to add new Identity column into the Variable $Workspaces however when i tried to pass workspace into another command, it crashes, detail as below.
First Part of the PowerShell Command ; works and added new column as desired.
$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 the command
$array = @()
foreach ($workspace in $Workspaces )
{
$Dataflow = Get-PowerBIDataFlow -Workspace $workspace
}
Here, when i am passing values from $workspaces (created above) , it throws below error
Error as below
Get-PowerBIDataflow : Cannot bind parameter 'Workspace'. Cannot convert value "@{Identity=136; id=b2wewe8b5-wew2-4we-awew-awewe81a84f7; Name=PowerBI-Test}" to type "Microsoft.PowerBI.Common.Api.Workspaces.Workspace". Error: "Cannot convert the "@{Identity=136;
id=b2wewe8b5-wew2-4we-awew-awewe81a84f7; Name=PowerBI-Test }" value of type "System.Management.Automation.PSCustomObject" to type
"Microsoft.PowerBI.Common.Api.Workspaces.Workspace"."
At line:18 char:57
+ $Dataflow = Get-PowerBIDataFlow -Workspace $workspace
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-PowerBIDataflow], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerBI.Commands.Data.GetPowerBIDataflow
Any suggestion on how to get rid of this error.
Thanks
Amit
- Kevin_MorganApr 16, 2021Iron Contributor
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 }
- amsrivas28Apr 16, 2021Copper Contributor
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
}
}