Hi
Turns out I was close with my first script, the issue was the attribute names I was using.
To my mind, the following is cleaner and easier to manage than duplicating the same code for each property.
This also allows for the cleanup of whitespace from the beginning and end of the users full name.
#Remove leading and trailing whitespace from First, Last and Full name, SAMAccountName and UserPrincipleName
$Properties = @("givenName","sn","cn","samAccountName","userPrincipalName")
foreach ($propertyName in $Properties) {
#Get the property value
$value = $Context.GetModifiedPropertyValue("$propertyName")
#Trim the white space from the beginning and end
$trimmedValue = $value.trim()
$Context.LogMessage("Checking $propertyName for whitespace", "Information")
#If the original and trimmed values don't match, there must be whitespace
if ($value -ne $trimmedValue) {
$Context.LogMessage("Removing whitespace for '$propertyName'.", "Information")
#Write the trimmed value back to the property.
$Context.SetModifiedPropertyValue("$propertyName", $trimmedValue)
}
}
Hope this helps someone :)
Matt