@@ -77,32 +77,11 @@ function New-CIPPGroup {
7777 $null
7878 }
7979
80- # Determine if we should generate a random mailNickname:
81- # For Security/Generic groups WITHOUT a username filled in
82- $ShouldGenerateRandomMailNickname = ($NormalizedGroupType -in @ (' Generic' , ' Security' )) -and [string ]::IsNullOrWhiteSpace($GroupObject.username )
83-
84- # Extract local part of username if exists and remove special characters for mailNickname
85- if ($ShouldGenerateRandomMailNickname ) {
86- # Generate a random alphanumeric mailNickname for security groups without a username
87- # Format: 8 hex characters + hyphen + 1 hex character (e.g., "450662e4-3")
88- $RandomPart1 = -join ((0 .. 7 ) | ForEach-Object { (0 .. 15 | ForEach-Object { ' 0123456789abcdef' [$_ ] } | Get-Random ) })
89- $RandomPart2 = (0 .. 15 | ForEach-Object { ' 0123456789abcdef' [$_ ] } | Get-Random )
90- $MailNickname = " $RandomPart1 -$RandomPart2 "
80+ # Determine if we should generate a mailNickname with a GUID, or use the username field
81+ if (-not $GroupObject.Username ) {
82+ $MailNickname = (New-Guid ).guid.substring(0 , 10 )
9183 } else {
92- if ($GroupObject.username ) {
93- $MailNickname = ($GroupObject.username -split ' @' )[0 ]
94- } else {
95- $MailNickname = $GroupObject.username
96- }
97-
98- # Remove forbidden characters per Microsoft 365 mailNickname requirements:
99- # ASCII 0-127 only, excluding: @ () / [] ' ; : <> , SPACE and any non-ASCII
100- $MailNickname = $MailNickname -replace " [@()\[\]/'`;:<>,\s]|[^\x00-\x7F]" , ' '
101-
102- # Ensure max length of 64 characters
103- if ($MailNickname.Length -gt 64 ) {
104- $MailNickname = $MailNickname.Substring (0 , 64 )
105- }
84+ $MailNickname = $GroupObject.Username
10685 }
10786
10887 Write-LogMessage - API $APIName - tenant $TenantFilter - message " Creating group $ ( $GroupObject.displayName ) of type $NormalizedGroupType $ ( if ($NeedsEmail ) { " with email $Email " }) " - Sev Info
0 commit comments