You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加字段后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.EmployeeID in your script will throw an error—you need to use $user."Employee ID" instead.
  • Double-check that your CSV has a column named SamAccountName (not SAMAccountName or UserID—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 Address or SamAccountName has 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:46:00