Skip to content

Commit

Permalink
Merge pull request KelvinTegelaar#964 from kris6673/dev
Browse files Browse the repository at this point in the history
Improve DeletedUserRentention standard
  • Loading branch information
KelvinTegelaar authored Jul 9, 2024
2 parents eaab728 + 7a6e3d7 commit e75212a
Showing 1 changed file with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,57 @@ function Invoke-CIPPStandardDeletedUserRentention {
Run the Tools\Update-StandardsComments.ps1 script to update this comment block
#>

param($Tenant, $Settings)
$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/admin/sharepoint/settings' -tenantid $Tenant -AsApp $true

if ($Settings.report -eq $true) {
Add-CIPPBPAField -FieldName 'DeletedUserRentention' -FieldValue $CurrentInfo.deletedUserPersonalSiteRetentionPeriodInDays -StoreAs string -Tenant $tenant
}

# Input validation
if (($Settings.Days -eq 'Select a value') -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'DeletedUserRententio: Invalid Days parameter set' -sev Error
Return
}

param($Tenant, $Settings)
$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/admin/sharepoint/settings' -tenantid $Tenant -AsApp $true
$StateSetCorrectly = if ($CurrentInfo.deletedUserPersonalSiteRetentionPeriodInDays -eq 365) { $true } else { $false }
# Backwards compatibility for pre v5.10.0
if ($null -eq $Settings.Days) {
$WantedState = 365
} else {
$WantedState = [int]$Settings.Days
}

$StateSetCorrectly = if ($CurrentInfo.deletedUserPersonalSiteRetentionPeriodInDays -eq $WantedState) { $true } else { $false }
$RetentionInYears = $WantedState / 365

If ($Settings.remediate -eq $true) {
Write-Host 'Time to remediate'

if ($StateSetCorrectly -eq $false) {
try {
$body = '{"deletedUserPersonalSiteRetentionPeriodInDays": 365}'
$body = [PSCustomObject]@{
deletedUserPersonalSiteRetentionPeriodInDays = $Settings.Days
}
$body = ConvertTo-Json -InputObject $body -Depth 5 -Compress
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/admin/sharepoint/settings' -AsApp $true -Type PATCH -Body $body -ContentType 'application/json'

Write-LogMessage -API 'Standards' -tenant $tenant -message 'Set deleted user rentention of OneDrive to 1 year' -sev Info
Write-LogMessage -API 'Standards' -tenant $tenant -message "Set deleted user rentention of OneDrive to $RetentionInYears year(s)" -sev Info
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to set deleted user rentention of OneDrive to 1 year. Error: $ErrorMessage" -sev Error
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to set deleted user rentention of OneDrive to $RetentionInYears year(s). Error: $ErrorMessage" -sev Error
}
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Deleted user rentention of OneDrive is already set to 1 year' -sev Info

Write-LogMessage -API 'Standards' -tenant $tenant -message "Deleted user rentention of OneDrive is already set to $RetentionInYears year(s)" -sev Info
}
}

if ($Settings.alert -eq $true) {
if ($StateSetCorrectly) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Deleted user rentention of OneDrive is set to 1 year' -sev Info
if ($StateSetCorrectly -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Deleted user rentention of OneDrive is set to $RetentionInYears year(s)" -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Deleted user rentention of OneDrive is not set to 1 year' -sev Alert
Write-LogMessage -API 'Standards' -tenant $tenant -message "Deleted user rentention of OneDrive is not set to $RetentionInYears year(s). Value is: $($CurrentInfo.deletedUserPersonalSiteRetentionPeriodInDays) " -sev Alert
}
}

if ($Settings.report -eq $true) {

Add-CIPPBPAField -FieldName 'DeletedUserRentention' -FieldValue $StateSetCorrectly -StoreAs bool -Tenant $tenant
}
}


Expand Down

0 comments on commit e75212a

Please sign in to comment.