1

My goal was to create a collection of objects (SharePoint sites) with in each object another collection of objects (Lists in that sites). After writing the code below I thought I succeeded but I don't know how to access the Lists objects.

$sites = @()

foreach ($s in $Subsites){
  Connect-PnPOnline -Url $s.Url
  $Lists = Get-PnPList

  $ctx = Get-PnPContext
  $web = $ctx.web
  $ctx.Load($web)
  $ctx.ExecuteQuery()

  $listsCollection = @()

  foreach ($list in $Lists) {
    $props = @{
       ListName         = $list.Title
       ListItems        = $list.ItemCount
       LastDeletedDate  = $list.LastItemDeletedDate
       LastModifiedDate = $list.LastItemUserModifiedDate
    }
    $listObj = New-Object -TypeName PSObject -Property $props
    $listsCollection += $listObj
  }

  $props = @{
     SiteName         = $s.Title 
     LastModified     = $web.LastItemUserModifiedDate
     URL              = $web.Url
     Lists            = $listsCollection
  }
  $webObj = New-Object -TypeName PSObject -Property $props
  $sites += $webObj
 }

After running the code I can access the site information like I expected to do $sites[0].SiteName gives me: "My site name" And I can see the list information in the object too but it seems to me that it is only string information and not real objects.
$sites[0].Lists gives me:

@{LastDeletedDate=06/12/2019 09:24:57; LastModifiedDate=06/12/2019 09:27:30; ListName=MyList1; ListItems=6}

@{LastDeletedDate=04/19/2019 12:48:14; LastModifiedDate=04/19/2019 12:48:14; ListName=MyList2; ListItems=0}

but I can't acces ListName by using $sites[0].Lists[0].ListName . Get-Member gives me just one property Length. The TypeName of the object is System.String. I tried several other things like using other ways to create a CustomObject and using select -ExpandProperty or Key and Value but no succes either.

Mustafa
  • 977
  • 3
  • 12
  • 25
Kees-Stack
  • 11
  • 3
  • 1
    I guess `$Lists` starts with an empty (`$Null`) item. Can you serialize the results (see: [Does PowerShell support HashTable Serialization?](https://stackoverflow.com/a/60623803/1701026)) and add that (as code) to the question? – iRon Mar 17 '20 at 16:32
  • 1
    On PowerShell 5.1, the only way I can get your results for `$sites[0].Lists` is to attempt to convert the objects to string --> `Write-Host $sites[0].Lists` or `[string[]]$sites[0].Lists`. Otherwise, `$sites[0].Lists` produces an output with properties that can be referenced. – AdminOfThings Mar 17 '20 at 16:37
  • 1
    The PowerShell version is 5.1. I will include the serialization, so far no luck with that – Kees-Stack Mar 17 '20 at 17:53
  • 1
    Sorry, I didn't include the intermediate steps I used. In that steps I output the `$sites` object with `$sites | ConvertTo-Json | Out-File .\sites.json` and later on I import it again with `Get-Content .\stes.json | ConvertFrom-Json`. After leaving out the JSON conversion it worked as AdminOfThings described. Many thanks – Kees-Stack Mar 17 '20 at 19:45
  • 1
    When using `ConvertTo-Json` you should consider using the `-Depth` parameter with a value greater than or equal to the levels of your object. PowerShell assumes a depth level of 2 without the parameter. Try `ConvertTo-Json -Depth 5` here. – AdminOfThings Mar 17 '20 at 19:47
  • You are right AdminOfThings that was the solution to get it right. Thanks again for your swift answer! – Kees-Stack Mar 17 '20 at 19:54
  • I recommend you to try to [avoid using the (ineffective) increase assignment operator `(+=)` to create a collection](https://stackoverflow.com/a/60708579/1701026), instead use the pipeline to build objects, like: `$Sites = foreach ($s in $Subsites){ ... $listsCollection = foreach ($list in $Lists) { ... } }` – iRon Mar 18 '20 at 06:57

1 Answers1

0

Sorry, I didn't include the intermediate steps I used. In that steps I output the $sites object with $sites | ConvertTo-Json | Out-File .\sites.json and later on I import it again with Get-Content .\stes.json | ConvertFrom-Json. After adding the Depth parameter with ConvertToJson -Depth 5 as suggested by AdminOfThings it worked perfectly

Kees-Stack
  • 11
  • 3