I've tried the following script to adapt the UPN to the country, the step will be processed in "before user creation" but the UPN stays default to com:
$propertyName = "countryCode"
$upnSuffixMap = @{
"companyName.com" = @("276")
"companyName.in" = @("356")
"companyName.cn" = @("156")
"companyName.com.tr" = @("792")
}
# Get property value
try
{
$value = $Context.TargetObject.Get($propertyName)
}
catch
{
return # Property is empty
}
# Get UPN Suffix
$upnSuffix = $NULL
foreach ($item in $upnSuffixMap.GetEnumerator())
{
if ($item.Value -notcontains $value)
{
continue
}
$upnSuffix = $item.Key
break
}
if ([System.String]::IsNullOrEmpty($upnSuffix))
{
$Context.LogMessage("UPN suffix was not specified for '$value'. companyName.com will be used.", "Warning")
return
}
# Get UPN
$userPrincipalName = "%userPrincipalName%"
if ([System.String]::IsNullOrEmpty($userPrincipalName))
{
$Context.LogMessage("Cannot assign a UPN suffix because the user logon name is empty", "Warning")
return
}
# Build new UPN
$userPrincipalName = $userPrincipalName.SubString(0, $userPrincipalName.IndexOf("@")) + "@$upnSuffix"
# Save changes
$Context.TargetObject.Put("userPrincipalName", $userPrincipalName)
$Context.TargetObject.SetInfo()