Skip to content

Commit

Permalink
Added Remove-CT365AllDeletedM365Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
DevClate committed Nov 28, 2023
1 parent 8c8917d commit 226d362
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
5 changes: 3 additions & 2 deletions 365AutomatedLab.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = '365AutomatedLab.psm1'

# Version number of this module.
ModuleVersion = '0.1.7'
ModuleVersion = '0.1.8'

# Supported PSEditions
CompatiblePSEditions = 'Core'
Expand Down Expand Up @@ -93,7 +93,8 @@ FunctionsToExport = @(
'New-CT365Teams',
'Remove-CT365Teams',
'Remove-CT365SharePointSite',
'Set-CT365SPDistinctNumber'
'Set-CT365SPDistinctNumber',
'Remove-CT365AllDeletedM365Groups'

)

Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
78 changes: 78 additions & 0 deletions Functions/Public/Remove-CT365AllDeletedM365Groups.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions Tests/Remove-CT365AllDeletedM365Groups.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 226d362

Please sign in to comment.