powershellmicrosoft-planner

Exporting Data to Tables from PNP Powershell


I'm trying to collect Bucket IDs from a number of MS Planner Plans into a CSV. When the script is run on its own in a single instance of powershell it pulls nine lines of info. How can I get that properly rolled out into a table?

$table = [System.Collections.ArrayList]::new()
foreach ($Group in $Groups) {
   Connect-PnPOnline -Url $Group.siteurl -Interactive

   # Pull Plan ID
   $groupname = Get-PnPMicrosoft365Group -identity $Group.groupname | Select-Object -ExpandProperty DisplayName
   $plannerid = Get-PnPPlannerPlan -Group $Group.groupname -Identity $Group.planTitle | Select-Object -ExpandProperty Title
   $bucketids = Get-PnPPlannerBucket -Group $group.groupname -Plan $group.planTitle | select-object -ExpandProperty Id

   $newRow = [pscustomobject]@{
           GroupName = $groupname
           PlannerName = $plannerid
           BucketIds = $bucketids
   }
   $table.Add($newRow) | out-null 
}
$table | Export-Csv -Path "$filesystempath\Pull Plan IDs\output-buckets.csv" -NoTypeInformation

The way it is currently, the Group Name and Planner Name show up properly, but the bucket id shows up in the CSV as "System.Object[]"


Solution

  • Since each plan can have multiple associated buckets, use another loop for them:

    $table = foreach ($Group in $Groups) {
       Connect-PnPOnline -Url $Group.siteurl -Interactive
    
       # Pull Plan ID
       $groupname = Get-PnPMicrosoft365Group -identity $Group.groupname | Select-Object -ExpandProperty DisplayName
       $plannerid = Get-PnPPlannerPlan -Group $Group.groupname -Identity $Group.planTitle | Select-Object -ExpandProperty Title
    
       foreach ($bucket in Get-PnPPlannerBucket -Group $group.groupname -Plan $group.planTitle) {
           [pscustomobject]@{
               GroupName = $groupname
               PlannerName = $plannerid
               BucketIds = $bucket.Id
           }
        }
    }
    
    $table | Export-Csv -Path "$filesystempath\Pull Plan IDs\output-buckets.csv" -NoTypeInformation