@@ -458,15 +458,38 @@ try {
458458 # Check for invalid domain error
459459 if ($_.Exception.Message -match ' domain.*invalid|verified domain|InvalidValue.*userPrincipalName' ) {
460460 $errorDetails = if ($_.ErrorDetails.Message ) {
461- ($_.ErrorDetails.Message | ConvertFrom-Json ).error.message
461+ try {
462+ $errorJson = $_.ErrorDetails.Message | ConvertFrom-Json
463+ $mainMessage = $errorJson.error.message
464+ $targetInfo = if ($errorJson.error.details ) {
465+ $errorJson.error.details | ForEach-Object { " Target: $ ( $_.target ) , Message: $ ( $_.message ) " }
466+ }
467+ else { $null }
468+
469+ if ($targetInfo ) {
470+ " $mainMessage `n Details: $ ( $targetInfo -join ' ; ' ) "
471+ }
472+ else {
473+ $mainMessage
474+ }
475+ }
476+ catch {
477+ $_.ErrorDetails.Message
478+ }
462479 }
463480 else {
464481 $_.Exception.Message
465482 }
483+
466484 Write-Host " FAILED: Invalid domain in UserPrincipalName" - ForegroundColor Red
467485 Write-Verbose " Domain validation error: $errorDetails "
468486 Write-Host " Error: $errorDetails " - ForegroundColor Yellow
469487
488+ # Extract domain from UPN
489+ $domain = $userPrincipalName -replace ' ^[^@]+@' , ' '
490+ Write-Host " Domain used: $domain (not verified in your tenant)" - ForegroundColor Yellow
491+ Write-Host " Suggestion: Update the CSV to use a verified domain for your tenant" - ForegroundColor Cyan
492+
470493 # Continue to next user instead of stopping
471494 $failCount ++
472495 $results.Add ([PSCustomObject ]@ {
@@ -475,7 +498,7 @@ try {
475498 Status = ' Failed'
476499 Password = $null
477500 ObjectId = $null
478- Error = $ errorDetails
501+ Error = " Invalid domain: $domain - $ errorDetails"
479502 })
480503 Write-Verbose " Error result stored in collection"
481504 Write-Host " "
@@ -492,18 +515,51 @@ try {
492515 }
493516 }
494517 catch {
495- Write-Host " FAILED: $ ( $_.Exception.Message ) " - ForegroundColor Red
496- Write-Verbose " Error details: $ ( $_.Exception | Format-List - Force | Out-String ) "
518+ # Parse error details from API response
519+ $errorMessage = $_.Exception.Message
520+ $detailedError = $errorMessage
521+
522+ if ($_.ErrorDetails.Message ) {
523+ try {
524+ $errorJson = $_.ErrorDetails.Message | ConvertFrom-Json
525+ if ($errorJson.error ) {
526+ $detailedError = $errorJson.error.message
527+
528+ # Add detailed information if available
529+ if ($errorJson.error.details ) {
530+ $additionalDetails = $errorJson.error.details | ForEach-Object {
531+ " [$ ( $_.target ) ]: $ ( $_.message ) "
532+ }
533+ $detailedError += " `n " + ($additionalDetails -join " `n " )
534+ }
535+
536+ # Add inner error information if available
537+ if ($errorJson.error.innerError ) {
538+ Write-Verbose " Inner Error - Request ID: $ ( $errorJson.error.innerError .' request-id' ) "
539+ Write-Verbose " Inner Error - Date: $ ( $errorJson.error.innerError.date ) "
540+ }
541+ }
542+ }
543+ catch {
544+ Write-Verbose " Could not parse error details JSON: $_ "
545+ }
546+ }
547+
548+ Write-Host " FAILED: $detailedError " - ForegroundColor Red
549+ Write-Verbose " Full error details: $ ( $_.Exception | Format-List - Force | Out-String ) "
550+ Write-Verbose " Error category: $ ( $_.CategoryInfo.Category ) "
551+ Write-Verbose " Error reason: $ ( $_.CategoryInfo.Reason ) "
552+
497553 $failCount ++
498554
499- # Store error result
555+ # Store error result with detailed information
500556 $results.Add ([PSCustomObject ]@ {
501557 UserPrincipalName = $userPrincipalName
502558 DisplayName = $displayName
503559 Status = ' Failed'
504560 Password = $null
505561 ObjectId = $null
506- Error = $_ .Exception.Message
562+ Error = $detailedError
507563 })
508564 Write-Verbose " Error result stored in collection"
509565 }
0 commit comments