Skip to content

feat: apply site version policy only on drift + app-only limitation - #14

Merged
luigilink merged 4 commits into
mainfrom
feature/site-policy-drift
Jul 15, 2026
Merged

feat: apply site version policy only on drift + app-only limitation#14
luigilink merged 4 commits into
mainfrom
feature/site-policy-drift

Conversation

@luigilink

Copy link
Copy Markdown
Owner

Closes #13

Summary

Makes the site version policy modes (added in #11) idempotent by only calling Set-PnPSiteVersionPolicy when the current policy differs from the desired settings, and documents/guards the Azure Automation app-only limitation. Validated live against a demo tenant.

Changes

  • Drift detection (Test-SiteVersionPolicyDrift): reads the current policy with Get-PnPSiteVersionPolicy and compares using the real returned fields — DefaultTrimMode (e.g. ExpireAfter), DefaultExpireAfterDays, MajorVersionLimit. Compliant sites are skipped; unreadable policy fails safe (treated as drift).
  • Cmdlet constraint fix: MajorWithMinorVersions is only passed when the request targets existing libraries. A live test showed Set-PnPSiteVersionPolicy rejects it for a new-libraries-only request when auto expiration is off.
  • App-only limitation: Get-/Set-PnPSiteVersionPolicy need a delegated user context that is site collection admin. Emits a warning in Azure Automation (Managed Identity) and documents preferring tenant-level Set-PnPTenant or interactive runs there.

Testing

Field shapes and constraints were captured from a live tenant (device-code login):

  • No policy -> fields blank, Description = 'No Site Level Policy Set...'
  • ExpireAfter 180/100 -> DefaultTrimMode=ExpireAfter, DefaultExpireAfterDays=180, MajorVersionLimit=100

Invoke-Pester -> 92 passed, 0 failed, including a functional drift context using those exact shapes.

Notes

Builds on #12. No breaking change; default Legacy mode unaffected.

Add Test-SiteVersionPolicyDrift, which reads the current policy with
Get-PnPSiteVersionPolicy and compares it against the desired settings
using the real returned fields (DefaultTrimMode, DefaultExpireAfterDays,
MajorVersionLimit). Set-PnPSiteVersionPolicy is now called only when a
drift is detected; compliant sites are skipped. Reading fails safe:
unreadable policy is treated as drift.

Also fix a constraint validated live against a tenant: MajorWithMinorVersions
must not be passed for a new-libraries-only request when auto expiration is
off, so it is only added when the request targets existing libraries.

Refs #13
Add static assertions for Test-SiteVersionPolicyDrift and the drift-gated
apply, plus a functional context exercising the comparison against the
exact Get-PnPSiteVersionPolicy field shapes captured from a live tenant
(no policy, matching ExpireAfter 180/100, differing values, fail-safe on
read error). 91 tests pass.

Refs #13
Note in CHANGELOG/RELEASE-NOTES that Set-PnPSiteVersionPolicy is applied
only on drift (via Get-PnPSiteVersionPolicy), that reading fails safe, and
the MajorWithMinorVersions constraint. Add a drift-based apply note to the
Configuration wiki.

Refs #13
Get-/Set-PnPSiteVersionPolicy require a delegated user context that is site
collection administrator, which is not available in Azure Automation with a
Managed Identity (app-only). Emit a warning in that context so the likely
'unauthorized' failure is explained, and document the limitation in the
Configuration wiki (prefer tenant-level Set-PnPTenant or interactive runs).

Refs #13
@luigilink
luigilink merged commit e51177f into main Jul 15, 2026
1 check passed
@luigilink
luigilink deleted the feature/site-policy-drift branch July 15, 2026 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Only apply site version policy when there is a drift (Get-PnPSiteVersionPolicy)

1 participant