Skip to content

fix: correct MySite selection filter to prevent array conversion erro…#7000

Closed
TeamInfinixdevcom wants to merge 1 commit into
Microsoft365DSC:Devfrom
TeamInfinixdevcom:fix/spo-filter
Closed

fix: correct MySite selection filter to prevent array conversion erro…#7000
TeamInfinixdevcom wants to merge 1 commit into
Microsoft365DSC:Devfrom
TeamInfinixdevcom:fix/spo-filter

Conversation

@TeamInfinixdevcom
Copy link
Copy Markdown

This PR fixes an issue where Get-PnPTenantSite could return multiple results,
causing a conversion error when used with -Identity.

Changes:

  • Updated filter to use '*.my.sharepoint.com' for more precise matching
  • Preserved existing Template filtering logic
  • Ensured a single object is returned using Select-Object -First 1

This prevents System.Object[] to SPOSitePipeBind conversion errors.

Closes #6991

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -Filter URL pattern intended to narrow MySite host matching.
  • Kept the existing Template exclusion for redirect sites.
  • Forced a single result via Select-Object -First 1 to 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
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
$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

Copilot uses AI. Check for mistakes.
Comment on lines +500 to 501
$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
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/).

Suggested change
$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
}

Copilot uses AI. Check for mistakes.

# 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
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
$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

Copilot uses AI. Check for mistakes.
@FabienTschanz
Copy link
Copy Markdown
Collaborator

@TeamInfinixdevcom I'm struggling to understand why your proposed solution would fix the mentioned issue. It would exclude any other -my.xxx sites and only allow the ones with a .my.xxx, moving the issue away but not eliminating it.

@TeamInfinixdevcom
Copy link
Copy Markdown
Author

@FabienTschanz
Thanks for the feedback!

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 -Identity.

By narrowing the filter to *.my.sharepoint.com and using Select-Object -First 1, I aimed to avoid returning multiple objects while still targeting the MySite host.

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?

@FabienTschanz
Copy link
Copy Markdown
Collaborator

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.

@TeamInfinixdevcom
Copy link
Copy Markdown
Author

@FabienTschanz

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:

  • If a single site is returned → proceed as expected
  • If multiple sites are returned → either fail explicitly or log a warning
  • If no site is found → fail clearly

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?

@FabienTschanz
Copy link
Copy Markdown
Collaborator

Will close this PR. Working on a simple replacement on my own.

@TeamInfinixdevcom
Copy link
Copy Markdown
Author

@FabienTschanz Thanks for the update!

I’d be happy to help with any related fixes or improvements. Feel free to assign me anything suitable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SPO resources] PnP -match filter too greedy

3 participants