mrsilent
(Mr_Silent)
1
I’ve been given this absolute dumpster fire of a script to look at and try decipher why it’s not working. I’m only really semi-proficient so unfortunately, I can’t get to the bottom of this. This is supposed to pull data from a SharePoint Online list and create an Active Directory user based on the information it gets. I’ve done user creation scripts before, but nothing at all like this
.
Get-ADUser : The search filter cannot be recognized At line:62 char:9 + if (Get-ADUser -Filter {SAMAccountName -eq $BaseSam} ) #-ErrorAct … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (
[Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
.
- Edited. Decided to go another way with this thing. Was just too much
of a mess to deal with :(
8 Spice ups
Evan7191
(Evan7191)
2
You should sanitize the script, because it looks like it contains a real domain name and possibly a real password.
3 Spice ups
mike9019
(Mike in IT)
3
“I am more than happy to pay for like… an hour or so of someone’s time to look over this” - I’d be willing if you’d agree to a charitable donation somewhere 
Can you tell us what exactly is not working? Are you getting errors? More detail would be nice before diving into code.
3 Spice ups
Neally
(Neally)
4
If you post code, please use the ‘Insert Code’ button. Please and thank you!

mike9019
(Mike in IT)
5
See attached for general comments within the script. Some things I’d do differently and some things I don’t understand the goal/need but I don’t know your operation either. Look for “#ORIGINAL”, “#CHANGE”, or “#COMMENT” in the code to find my documentation.
Other comments I didn’t know where to put them within the script I’ll leave in the post…
- Write-Verbose will not display output until the $VerbosePreference Preference Variable is set accordingly
- Since you have M/O365 and are using username/password in the document I wonder if you have modern authentication turned off or AAD security defaults turned off? App passwords and basic auth won’t work with this enabled. Providing a default level of security in Microsoft Entra ID - Microsoft Entra | Microsoft Learn
- You commented out the random password generator, wonder why you wouldn’t keep that?
- The ASCII conversion is slick, but I’ve always done that via XSLT. Sounds odd but it’s easier to maintain and does character translation better than any .NET I’ve ever come across.
- You continue to use Convert-ToLatinCharacters for $Firstname and $Lastname in differing functions. Set it once and forget it.
As for the error you’re getting;
Get-ADUser : The search filter cannot be recognized At line:62 char:9 + if (Get-ADUser -Filter {SAMAccountName -eq $BaseSam} ) #-ErrorAct ...
My best guess is that $BaseSam is returning a null value, thus not able to filter. Add more logging output in that section to see what you’re getting.
Also, has this script ever worked? If it has, what’s changed since the last time it ran?
Edit: one other kind of major thing I forgot. Should really look at securely storing your credentials, outside of the script.
f693c676-ad1f-45ba-95cb-9fadcc3a5fe1-2341672.txt (12.5 KB)
1 Spice up
mrsilent
(Mr_Silent)
6
Holy crap Mike!!!
Dear god!!
You’re an absolute legend mate :O. I appreciate your effort more than I can express… 
I’ll take these changes you’ve suggested, put them into play and see how I go.
Thank you again!!!
mike9019
(Mike in IT)
7
To be clear, there’s no real changes I’ve made - just a lot of comments to lead you down the right path. I can see some things that I would change but I don’t know the reasoning for some of the things you’re doing so I can’t really make suggestions without talking with you about them.
1 Spice up