Skip to content

Commit fd4fed3

Browse files
committed
Add error handling for invalid domain in UserPrincipalName during user creation
1 parent 70a1652 commit fd4fed3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Entra/New-EntraUserFromCSV.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,34 @@ try {
454454
Write-Host "`nScript execution stopped.`n" -ForegroundColor Red
455455
exit 1
456456
}
457+
458+
# Check for invalid domain error
459+
if ($_.Exception.Message -match 'domain.*invalid|verified domain|InvalidValue.*userPrincipalName') {
460+
$errorDetails = if ($_.ErrorDetails.Message) {
461+
($_.ErrorDetails.Message | ConvertFrom-Json).error.message
462+
}
463+
else {
464+
$_.Exception.Message
465+
}
466+
Write-Host " FAILED: Invalid domain in UserPrincipalName" -ForegroundColor Red
467+
Write-Verbose "Domain validation error: $errorDetails"
468+
Write-Host " Error: $errorDetails" -ForegroundColor Yellow
469+
470+
# Continue to next user instead of stopping
471+
$failCount++
472+
$results.Add([PSCustomObject]@{
473+
UserPrincipalName = $userPrincipalName
474+
DisplayName = $displayName
475+
Status = 'Failed'
476+
Password = $null
477+
ObjectId = $null
478+
Error = $errorDetails
479+
})
480+
Write-Verbose " Error result stored in collection"
481+
Write-Host ""
482+
continue
483+
}
484+
457485
# Re-throw other errors to be caught by outer catch block
458486
throw
459487
}

0 commit comments

Comments
 (0)