Hello,
I am Prosenjit an expert in AWS, Azure, VMware and multiple Microsoft technologies.
I presume you are using samAccountName not userPrincipalName attribute. The 20 character limitation for samAccountName is a common restriction and this needs to be changed in Schema. So, the prerequisite is, you must have Schema Admin rights.
Regarding the second issue, I need to review the script and may need to test it in my own LAB before I can propose a solution.
Let's connect to discuss further.
Best regards,
Prosenjit SEN