Created
December 6, 2022 20:47
-
-
Save aflyen/f6d5b3900afeeb8d795b099e6b68ab51 to your computer and use it in GitHub Desktop.
Get a unique Microsoft 365 Group alias for use when creating new groups in automated solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-CorpMicrosoft365GroupAlias | |
{ | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$Alias | |
) | |
$AliasFound = $false | |
$Retries = 0 | |
$UniqueAlias = $Alias | |
while ($AliasFound -eq $false) | |
{ | |
$Group = Get-PnPMicrosoft365Group -Identity $UniqueAlias | |
# No group found so the alias is available | |
if ($Group -eq $null) | |
{ | |
$AliasFound = $true | |
return $UniqueAlias | |
} | |
# If no unique alias has been found in 20 retries cancel | |
if ($Retries -eq 20) | |
{ | |
throw "Unable to find unique alias" | |
} | |
# Create attempt of unique alias by adding random 4 numbers at the end | |
$UniqueAlias = "$($Alias)$(Get-Random -Minimum 1000 -Maximum 9999)" | |
$Retries++ | |
} | |
return $UniqueAlias | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment