fix: correct MySite selection filter to prevent array conversion erro…#7000
fix: correct MySite selection filter to prevent array conversion erro…#7000TeamInfinixdevcom wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent Get-PnPTenantSite returning multiple matches for the OneDrive/MySite host, which can lead to System.Object[] → SPOSitePipeBind conversion errors when the result is passed to -Identity in the SPOSharingSettings DSC resource.
Changes:
- Updated the
Get-PnPTenantSite -FilterURL pattern intended to narrow MySite host matching. - Kept the existing
Templateexclusion for redirect sites. - Forced a single result via
Select-Object -First 1to avoid array-to-identity conversion failures.
|
|
||
| # Local filtering because server side filtering intermittently fails | ||
| $MySite = Get-PnPTenantSite -Filter "Url -like '-my.sharepoint.'" | Where-Object -FilterScript { $_.Template -notmatch '^RedirectSite#' } | ||
| $MySite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 |
There was a problem hiding this comment.
The filter pattern *.my.sharepoint.com likely won’t match the standard OneDrive host URL format (<tenant>-my.sharepoint.com, which has -my not .my). That can cause $MySite to be $null and also risks selecting an arbitrary personal site when multiple results exist. Prefer identifying the OneDrive host deterministically (e.g., filter by Template -eq 'SPSMSITEHOST#0' and/or exclude /personal/), rather than Select-Object -First 1 on a broad URL match.
| $MySite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 | |
| $MySite = Get-PnPTenantSite -ErrorAction Stop | | |
| Where-Object { $_.Template -eq 'SPSMSITEHOST#0' } | | |
| Select-Object -First 1 |
| $mysite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 | ||
| Set-PnPTenantSite -Identity $mysite.Url -SharingCapability $MySiteSharingCapability |
There was a problem hiding this comment.
Same issue as in Get-TargetResource: *.my.sharepoint.com likely won’t match <tenant>-my.sharepoint.com, and Select-Object -First 1 can pick an arbitrary personal site if multiple are returned. This can lead to $mysite being $null (then $mysite.Url throws) or updating the wrong site collection. Use a deterministic selection for the OneDrive host (e.g., Template -eq 'SPSMSITEHOST#0' / exclude /personal/).
| $mysite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 | |
| Set-PnPTenantSite -Identity $mysite.Url -SharingCapability $MySiteSharingCapability | |
| # Select the OneDrive/My Site host deterministically by template and exclude personal sites | |
| $mysite = Get-PnPTenantSite -Template 'SPSMSITEHOST#0' | | |
| Where-Object { $_.Template -notmatch '^RedirectSite#' -and $_.Url -notlike '*/personal/*' } | | |
| Select-Object -First 1 | |
| if ($null -eq $mysite -or [System.String]::IsNullOrWhiteSpace($mysite.Url)) | |
| { | |
| Write-Verbose -Message 'Unable to determine OneDrive (My Site) host site collection. Skipping MySiteSharingCapability update.' | |
| } | |
| else | |
| { | |
| Set-PnPTenantSite -Identity $mysite.Url -SharingCapability $MySiteSharingCapability | |
| } |
|
|
||
| # Local filtering because server side filtering intermittently fails | ||
| $MySite = Get-PnPTenantSite -Filter "Url -like '-my.sharepoint.'" | Where-Object -FilterScript { $_.Template -notmatch '^RedirectSite#' } | ||
| $MySite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 |
There was a problem hiding this comment.
This change fixes a real-world failure mode (multiple Get-PnPTenantSite results causing array conversion errors), but there’s no unit test asserting the selection logic when multiple sites are returned. Consider adding a Pester test that mocks Get-PnPTenantSite to return multiple entries and verifies the code picks the intended OneDrive host site (and that Set uses a single URL).
| $MySite = Get-PnPTenantSite -Filter "Url -like '*.my.sharepoint.com'" | Where-Object { $_.Template -notmatch '^RedirectSite#' } | Select-Object -First 1 | |
| $tenantSites = Get-PnPTenantSite | |
| $MySite = $tenantSites | | |
| Where-Object { $_.Url -like '*.my.sharepoint.com' -and $_.Template -notmatch '^RedirectSite#' } | | |
| Sort-Object -Property Url | | |
| Select-Object -First 1 |
|
@TeamInfinixdevcom I'm struggling to understand why your proposed solution would fix the mentioned issue. It would exclude any other |
|
@FabienTschanz The goal of this change was to ensure the command consistently returns a single site object, since the issue seemed to come from multiple results being returned and failing when used with By narrowing the filter to That said, I see your point that this might not fully address all scenarios and could just shift the issue. Would you recommend a better approach to ensure a single valid result without restricting the filter too much? |
|
I'm honestly not sure. I don't work much with SharePoint so I'm not familiar with the site configuration and how they can be differentiated. I asked for some clarification in the linked issue. If the tenant site always has a specific template that it's derived from, then we could use that, but I don't know if that's the case for all tenants or just my lab. |
|
Thanks for the clarification — that helps a lot. If the template is not guaranteed to be consistent across all tenants, relying on it might introduce the same issue in a different way. Maybe instead of trying to narrow it down too much at the filtering level, it would be safer to explicitly handle multiple results. For example:
This would avoid making assumptions about the environment while still preventing the conversion error. What do you think about handling it this way instead of relying on stricter filtering? |
|
Will close this PR. Working on a simple replacement on my own. |
|
@FabienTschanz Thanks for the update! I’d be happy to help with any related fixes or improvements. Feel free to assign me anything suitable. |
This PR fixes an issue where Get-PnPTenantSite could return multiple results,
causing a conversion error when used with -Identity.
Changes:
This prevents System.Object[] to SPOSitePipeBind conversion errors.
Closes #6991