添加字段后Active Directory用户导入PowerShell脚本语法错误求助
Hi Christian, let's troubleshoot that syntax error you're hitting after adding the Employee ID, Address, and SamAccountName fields to your AD user import script. Since it worked perfectly before those additions, the issue is almost definitely tied to how you've implemented these new fields—either a mismatch with your CSV, a typo in parameters, or a formatting glitch in the New-ADUser command.
Here are the most common fixes and a corrected script example to get you back on track:
1. First, Verify Your CSV Column Names
The #1 culprit here is mismatched column names between your CSV and script. For example:
- If your CSV has
Employee ID(with a space), referencing$user.EmployeeIDin your script will throw an error—you need to use$user."Employee ID"instead. - Double-check that your CSV has a column named
SamAccountName(notSAMAccountNameorUserID—PowerShell is case-insensitive, but exact spelling matters).
2. Use Splatting to Avoid Syntax Chaos
Directly listing dozens of New-ADUser parameters is easy to mess up (missing commas, misplaced quotes). Using parameter splatting (storing parameters in a hash table) makes your script cleaner and less error-prone. Here's a revised version of your script with the new fields properly implemented:
# Create password for users $Password = ConvertTo-SecureString "Bevchain123$" -AsPlainText -Force # Input CSV file path $CSVlocation = Read-Host -Prompt "Enter path to CSV file" # Import CSV data $users = Import-Csv $CSVlocation # Process each user in the CSV foreach ($user in $users) { # Build user parameters (match CSV column names to AD attributes) $userAttributes = @{ GivenName = $user.GivenName Surname = $user.Surname Name = "$($user.GivenName) $($user.Surname)" SamAccountName = $user.SamAccountName # Match CSV column name exactly EmployeeID = $user."Employee ID" # Use quotes if CSV column has a space StreetAddress = $user.Address # AD uses StreetAddress for physical street address AccountPassword = $Password Enabled = $true ChangePasswordAtLogon = $true Path = "OU=Users,DC=bevchain,DC=local" # Update to your target OU } # Create the AD user New-ADUser @userAttributes }
3. Troubleshooting Steps to Pinpoint the Exact Error
If you still get a syntax error, run through these quick checks:
- Run the script line-by-line: Open PowerShell ISE, paste your script, and execute it one block at a time. The error message will tell you exactly which line is broken (e.g., "Missing closing '}' in statement block").
- Test with a single user: Trim your CSV to just one user entry—this eliminates issues with bad data in later rows.
- Check for special characters: If your
AddressorSamAccountNamehas special characters (like$,&), make sure they're properly escaped in the CSV (wrap the value in double quotes if it contains commas or quotes).
Quick Note on AD Attributes
Remember that Address in your CSV maps to the StreetAddress attribute in Active Directory. If your CSV includes additional address details (city, postal code), you'll need to add those as separate parameters (e.g., City = $user.City, PostalCode = $user.PostalCode).
Give these fixes a try—they should resolve the syntax error you're seeing. Let me know if you hit a specific error message we can break down further!
内容的提问来源于stack exchange,提问作者christian

