Private/Resolve-FileRefMap.ps1
|
function Resolve-FileRefMap { <# Build a UniqueId -> item lookup for a specific set of GUIDs, by walking the document libraries once and keeping only what we need. The SharingLinks group name gives us a file GUID, but Get-PnPFileSharingLink needs a URL. Get-PnPFile has no -UniqueId, so this metadata match is the reliable resolution path. It walks items, not links, so it is one cheap paged pass regardless of how many links exist - and it stops as soon as every needed GUID is found. Returns a hashtable keyed by lower-case GUID: @{ FileRef; FileName; IsFolder; Label } #> param( [Parameter(Mandatory)] [string[]] $NeededGuids ) $want = [System.Collections.Generic.HashSet[string]]::new() foreach ($g in $NeededGuids) { [void]$want.Add($g.ToLower()) } $map = @{} $libs = Invoke-WithRetry -Because 'Get-PnPList' -Action { Get-PnPList | Where-Object { -not $_.Hidden -and $_.BaseTemplate -eq 101 } } foreach ($lib in $libs) { if ($map.Count -ge $want.Count) { break } $items = Invoke-WithRetry -Because "items in $($lib.Title)" -Action { Get-PnPListItem -List $lib -PageSize 500 } foreach ($item in $items) { $uid = "$($item.FieldValues['UniqueId'])".ToLower() if ($uid -and $want.Contains($uid) -and -not $map.ContainsKey($uid)) { $map[$uid] = @{ FileRef = $item.FieldValues['FileRef'] FileName = $item.FieldValues['FileLeafRef'] IsFolder = $item.FileSystemObjectType -eq 'Folder' Label = $item.FieldValues['_DisplayName'] # sensitivity label if present } if ($map.Count -ge $want.Count) { break } } } } return $map } |