I'm trying to update mailNickname to cas_{ but nothing seems to work. This is my completed script of everything working but changing the mailNickname at the end of the code. Any ideas would be helpful. Thanks
Here is the basic code
# Update User mailNickname NOT WORKING
$cas = "cas_{"
$Context.SetModifiedPropertyValue("mailNickname", $cas)
#$Context.SetModifiedPropertyValue("mailNickname", "cas_{") Wont work also
Here is the completed script.
Import-Module Adaxes
$upnSuffix = "YYYYY.com" # TODO: modify me
function IsUserNameUnique($username)
{
$user = Get-AdmUser $username -erroraction silentlycontinue
return $user -eq $Null
}
# Get the username
$username = $Context.GetModifiedPropertyValue("samAccountName")
# Check if the username is unique
if (IsUserNameUnique($username))
{
return
}
# If the username is not unique, generate a unique one
$firstName = $Context.GetModifiedPropertyValue("givenName")
$lastName = $Context.GetModifiedPropertyValue("sn")
$uniqueUsername = $Null
if (($firstName -ne $NULL) -and ($lastName -ne $NULL))
{
$username = $lastName.Substring(0, 7)
$Context.LogMessage($username, "Information")
$Context.LogMessage($lastName, "Information")
foreach ($char in $firstName.ToCharArray())
{
$username = "$username$char"
if (IsUserNameUnique($username))
{
$uniqueUsername = $username
break
}
}
}
if ($uniqueUsername -eq $NULL)
{
for ($i = 1; $True; $i++)
{
$uniqueUsername = $username + $i;
if (IsUserNameUnique($uniqueUsername))
{
break
}
}
}
# Update User Logon Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)
# Update User Logon Name
$Context.SetModifiedPropertyValue("userPrincipalName", "$uniqueUsername@$upnSuffix")
$Context.LogMessage("The username has been changed to " + $uniqueUsername `
+ ".", "Information")
# Update User mailNickname NOT WORKING
$cas = "cas_{"
$Context.SetModifiedPropertyValue("mailNickname", $cas)
#$Context.SetModifiedPropertyValue("mailNickname", "cas_{") Wont work also