The Business rule just executes a PowerShell script when the mobile number is changed, and it does execute when i test. I have some $Context.LogMessage("", "Information") commands in there as validation when the script is run and the output of that is here:
Succeeded with warnings
Details
Business Rules: 1 rule encountered while processing your request
'Change DMZ Username after info update': Run PowerShell script 'Update username' for the user
Current username: ctesting1234
Fisrt Name: Chris
Last Name: Testing
Mobile: 123-123-4567
Can't find an object with identity 'ctesting1234'.
New username: ctesting4567
Can't find an object with identity 'ctesting4567'.
New UPN: <ctesting4567@domain.com
Here is my script:
Import-Module Adaxes
#import-module activedirectory
#Get First Name, last name and mobile
$currentUsername = $Context.TargetObject.Get("samAccountName")
$Context.LogMessage("Current username: $($currentUsername)", "Information")
$fn = $Context.TargetObject.Get("givenName")
$Context.LogMessage("Fisrt Name: $($fn)", "Information")
$ln = $Context.TargetObject.Get("sn")
$Context.LogMessage("Last Name: $($ln)", "Information")
$mobile = $Context.TargetObject.Get("mobile")
$Context.LogMessage("Mobile: $($mobile)", "Information")
$upnSuffix = "domain.com"
$newUsername = ""
$last4ofMobile = ""
$firstInitial = ""
#Remove spaces from Mobile
$mobile = $mobile -replace ' ',''
#Set new mobile number wihtout spaces
$Context.SetModifiedPropertyValue("mobile", $mobile)
#remove everything but digits from mobile
$mobile = $mobile -replace "[^0-9]"
#Get new Username
$firstInitial = $fn.substring(0,1)
if ($mobile.length -gt 4){
$last4ofMobile = $mobile.Substring($mobile.Length - 4)
}else{
$Context.LogMessage("Mobile number does not have enough digits, replacing with 1234", "Warning")
$last4ofMobile = "1234"
}
$newUsername = $firstInitial.ToLower() + $ln.ToLower() + $last4ofMobile
$userLogonName = $newUsername + "@" + $upnSuffix
# Update User Logon Name (pre-Windows 2000)
Set-AdmUser -Identity "$($currentUsername)" -samAccountName "$($newUsername)"
$Context.LogMessage("New username: $($newUsername)", "Information")
# Update User Logon Name
Set-AdmUser -Identity "$($newUsername)" -userPrincipalName "$($userLogonName)"
$Context.LogMessage("New UPN: $($userLogonName)", "Information")