Skip to content

Commit d01d74b

Browse files
committed
add mailnickname validation and string replacement
1 parent 04ade59 commit d01d74b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Modules/CIPPCore/Public/New-CIPPGroup.ps1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ function New-CIPPGroup {
7676
$null
7777
}
7878

79+
# Extract local part of username if exists and remove special characters for mailNickname
80+
if ($GroupObject.username -like '*@*') {
81+
$MailNickname = ($GroupObject.username -split '@')[0]
82+
} else {
83+
$MailNickname = $GroupObject.username
84+
}
85+
86+
# Remove forbidden characters per Microsoft 365 mailNickname requirements:
87+
# ASCII 0-127 only, excluding: @ () / [] ' ; : <> , SPACE and any non-ASCII
88+
$MailNickname = $MailNickname -replace "[@()\[\]/'`;:<>,\s]|[^\x00-\x7F]", ''
89+
90+
# Ensure max length of 64 characters
91+
if ($MailNickname.Length -gt 64) {
92+
$MailNickname = $MailNickname.Substring(0, 64)
93+
}
94+
7995
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Creating group $($GroupObject.displayName) of type $NormalizedGroupType$(if ($NeedsEmail) { " with email $Email" })" -Sev Info
8096

8197
# Handle Graph API groups (Security, Generic, AzureRole, Dynamic, M365)
@@ -84,7 +100,7 @@ function New-CIPPGroup {
84100
$BodyParams = [PSCustomObject]@{
85101
'displayName' = $GroupObject.displayName
86102
'description' = $GroupObject.description
87-
'mailNickname' = $GroupObject.username
103+
'mailNickname' = $MailNickname
88104
'mailEnabled' = ($NormalizedGroupType -in @('Security', 'M365'))
89105
'securityEnabled' = $true
90106
'isAssignableToRole' = ($NormalizedGroupType -eq 'AzureRole')

0 commit comments

Comments
 (0)