I'm trying to bulk create Entra ID users using a PowerShell script with the Microsoft Graph API. My CSV headers include employeeId, lastName, firstName, department, and officeLocation. However, I'm running into an error that says, "New-MgUser: Cannot convert the literal 'departmentNumberString' to the expected type 'Edm.String'." It seems like the issue is related to the $department and $employeeId fields not being treated as strings during the request. I've ensured they're formatted correctly in my script. The body of my request looks fine, but I suspect that PowerShell might be dropping the quotes around the numbers during the HTTP request. The $department is meant to represent students' graduation years and $employeeId represents their student IDs. How can I keep using this CSV file for bulk user creation without these errors? Any help would be appreciated!
4 Answers
It sounds like you've got a solid plan! Just to confirm, you shouldn't need the extra quotes around $department and $employeeId in your hashtable. If you're forcing the values to strings beforehand, you can include them directly. Also, check the 'AccountEnabled' assignment; it should probably just be 'AccountEnabled = $True'. I recently had success with a similar setup using version 2.27.0, and it worked without issues!
If you're still having trouble, it might be worth asking ChatGPT to help convert your script to use `Invoke-WebRequest`. Just see if it handles the API calls better with your current setup. It could save you some headaches!
Make sure to explicitly convert those number fields to strings in your script before sending them to the API. It's a common oversight when dealing with JSON in PowerShell. Also, consider quoting the values in your CSV, as it can sometimes help with this issue. If you're using a loop, double-check how you're assembling the request body and ensure those values are defined as string types. This should help avoid conversion errors!
Yeah, definitely check that too! I've found wrapping those fields in single quotes in the CSV can make a difference.
I noticed you're using version 2.26.X of the Microsoft Graph PowerShell module. There are known issues in that version, so I suggest upgrading to 2.27.0 or even downgrading to 2.25.0 if necessary. It might resolve that conversion error you're seeing.
Totally agree! Also, have you thought about trying the Invoke-MgGraphRequest command instead? It might help with the formatting!