From 226d3627ee7c1d03491ac3d1559f0fe521adc885 Mon Sep 17 00:00:00 2001 From: Clayton Tyger Date: Tue, 28 Nov 2023 06:03:29 -0500 Subject: [PATCH] Added Remove-CT365AllDeletedM365Groups --- 365AutomatedLab.psd1 | 5 +- CHANGELOG.md | 6 +- .../Remove-CT365AllDeletedM365Groups.ps1 | 78 +++++++++++++++++++ README.md | 2 + ...Remove-CT365AllDeletedM365Groups.Tests.ps1 | 16 ++++ 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 Functions/Public/Remove-CT365AllDeletedM365Groups.ps1 create mode 100644 Tests/Remove-CT365AllDeletedM365Groups.Tests.ps1 diff --git a/365AutomatedLab.psd1 b/365AutomatedLab.psd1 index 676742d..658d7c4 100644 --- a/365AutomatedLab.psd1 +++ b/365AutomatedLab.psd1 @@ -12,7 +12,7 @@ RootModule = '365AutomatedLab.psm1' # Version number of this module. -ModuleVersion = '0.1.7' +ModuleVersion = '0.1.8' # Supported PSEditions CompatiblePSEditions = 'Core' @@ -93,7 +93,8 @@ FunctionsToExport = @( 'New-CT365Teams', 'Remove-CT365Teams', 'Remove-CT365SharePointSite', - 'Set-CT365SPDistinctNumber' + 'Set-CT365SPDistinctNumber', + 'Remove-CT365AllDeletedM365Groups' ) diff --git a/CHANGELOG.md b/CHANGELOG.md index d774d87..0e2c3b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # 365AutomatedLab Changelog +## 0.1.8 + +Added Remove-CT365AllDeletedM365Groups. This will permanently delete all deleted Modern Microsoft 365 Groups. + ## 0.1.7 -Added Set-CT365SPDistinctNumber. Currently I have it so Sharepoint Sites have a number after them for testing so I know which ones I'm working on and not having to create "real" names for each. This allows you to easily rename the site names in one quick line. I do this as SharePoint Team sites never can fully delete fast as I want while testing. +Added Set-CT365SPDistinctNumber. Currently I have it so Sharepoint Sites have a number after them for testing so I know which ones I'm working on and not having to create "real" names for each. This allows you to easily rename the site names in one quick line. I do this as SharePoint Team sites never can fully delete fast as I want while testing. ## 0.1.6 diff --git a/Functions/Public/Remove-CT365AllDeletedM365Groups.ps1 b/Functions/Public/Remove-CT365AllDeletedM365Groups.ps1 new file mode 100644 index 0000000..4831265 --- /dev/null +++ b/Functions/Public/Remove-CT365AllDeletedM365Groups.ps1 @@ -0,0 +1,78 @@ +<# +.SYNOPSIS + Removes all deleted Microsoft 365 groups from the recycle bin. + +.DESCRIPTION + The Remove-CT365AllDeletedM365Groups function connects to a Microsoft 365 tenant and removes all Microsoft 365 groups that have been deleted and are currently in the recycle bin. It requires the PnP.PowerShell module and uses the Connect-PnPOnline cmdlet to establish the connection. + +.PARAMETER AdminUrl + The URL of the Microsoft 365 admin center. This parameter is mandatory and specifies the tenant to connect to. + +.EXAMPLE + PS C:\> Remove-CT365AllDeletedM365Groups -AdminUrl "https://contoso-admin.sharepoint.com" + + This example connects to the Microsoft 365 tenant at contoso-admin.sharepoint.com and removes all deleted Microsoft 365 groups. + +.INPUTS + None. You cannot pipe objects to Remove-CT365AllDeletedM365Groups. + +.OUTPUTS + String. The function outputs messages indicating the status of deletion operations and any errors encountered. + +.NOTES + This function requires the PnP.PowerShell module and PSFramework + +#> +function Remove-CT365AllDeletedM365Groups { + [CmdletBinding()] + Param ( + [Parameter(Mandatory)] + [ValidateScript({ + if ($_ -match '^[a-zA-Z0-9]+\.sharepoint\.[a-zA-Z0-9]+$') { + $true + } + else { + throw "The URL $_ does not match the required format." + } + })] + [string]$AdminUrl + + ) + + Begin { + + foreach ($module in @('PSFramework', 'PnP.PowerShell')) { + if (-not (Get-Module -ListAvailable -Name $module)) { + Install-Module $module -Scope CurrentUser + } + } + + Connect-PnPOnline -Url $AdminUrl -Interactive + } + + Process { + try { + + $deletedGroups = Get-PnPDeletedMicrosoft365Group + + foreach ($group in $deletedGroups) { + Remove-PnPDeletedMicrosoft365Group -Identity $group.Id + Write-PSFMessage -Message "Deleted group removed: $($group.DisplayName)" -Level Host + } + + if ($deletedGroups.Count -eq 0) { + Write-PSFMessage -Message "No deleted groups found." -Level Host + } + else { + Write-PSFMessage -Message "All deleted groups have been removed." -Level Host + } + } + catch { + Write-PSFMessage -Message "An error occurred: $_" -Level Error + } + } + + End { + Disconnect-PnPOnline -ErrorAction SilentlyContinue + } +} diff --git a/README.md b/README.md index 60ba45a..49eae27 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ Install-Module -Name 365AutomatedLab -Scope CurrentUser - Export-CT365ProdUserToExcel - Replace distinct number for sharepoint site names - Set-CT365SPDistinctNumber +- Delete all deleted Modern Microsoft 365 Groups + - Remove-CT365AllDeletedM365Groups ### Data diff --git a/Tests/Remove-CT365AllDeletedM365Groups.Tests.ps1 b/Tests/Remove-CT365AllDeletedM365Groups.Tests.ps1 new file mode 100644 index 0000000..da60954 --- /dev/null +++ b/Tests/Remove-CT365AllDeletedM365Groups.Tests.ps1 @@ -0,0 +1,16 @@ +BeforeAll { + # Call Cmdlet + $commandScriptPath = Join-Path -Path $PSScriptRoot -ChildPath '..\functions\public\Remove-CT365AllDeletedM365Groups.ps1' + + . $commandScriptPath +} + +Describe 'Remove-CT365AllDeletedM365Groups Function' { + Context 'When provided invalid parameters' { + It 'Should throw an error for invalid url format' { + $AdminUrl = "invalid_url" + + { Remove-CT365AllDeletedM365Groups -AdminUrl $AdminUrl } | Should -Throw + } + } +} \ No newline at end of file