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

使用PowerShell的Set-ADUser填充Direct Reports字段报错求助

问题:PowerShell填充AD用户Direct Reports字段失败

我有一份CSV文件,需要用它填充已存在的AD空用户信息。目前givenName、description、postalCode等字段都能成功填充,但Direct Reports字段始终无法设置。我是PowerShell新手,附上代码和报错信息,求排查问题。

代码

Import-Csv -Delimiter "," -Path C:\script\PerthUsersa.csv | Foreach { 
     $ADUser = Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName)'"
    
    $DirectReport = Get-ADUser 

    if($ADUser) {
        Set-ADUser -Identity $ADUser -givenName $_.givenName -Surname $_.sn -DisplayName $_.displayName  -Description $_.description -Office $_.physicalDeliveryOfficeName -EmailAddress $_.mail -StreetAddress $_.streetAddress -City $_.l -State $_.st -PostalCode $_.postalCode -Title $_.title -Department $_.description  -MobilePhone $_.mobile -DirectReport $_.directReport
              
       } else {
         Write-Warning ("didnt get in " + $($_.userPrincipalName))
    }    
}

报错信息

Set-ADUser : A parameter cannot be found that matches parameter name 'DirectReports'.
At C:\script\perthUsers.ps1:5 char:343

  • ... hone $.mobile -DirectReports $.directReports
  • ~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
解决方案
  1. 参数名称错误:Set-ADUser没有-DirectReport或-DirectReports这类直接参数,要设置用户的直接下属,需通过-Add/-Replace/-Remove参数操作AD原生属性directReports。
  2. 缺失直接下属查询逻辑:原代码中$DirectReport = Get-ADUser未指定筛选条件,无法定位到对应的AD用户,必须根据CSV里的directReport值(如UPN、SamAccountName)查询目标用户对象。
  3. 修正后的完整代码:
Import-Csv -Delimiter "," -Path C:\script\PerthUsersa.csv | Foreach-Object { 
    $ADUser = Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName)'"
    
    # 假设CSV的directReport列存的是用户UPN,查询对应AD用户
    $DirectReportUser = Get-ADUser -Filter "userPrincipalName -eq '$($_.directReport)'" -ErrorAction SilentlyContinue

    if($ADUser) {
        # 先填充常规字段(修正原代码中Department绑定错误)
        Set-ADUser -Identity $ADUser `
            -givenName $_.givenName `
            -Surname $_.sn `
            -DisplayName $_.displayName `
            -Description $_.description `
            -Office $_.physicalDeliveryOfficeName `
            -EmailAddress $_.mail `
            -StreetAddress $_.streetAddress `
            -City $_.l `
            -State $_.st `
            -PostalCode $_.postalCode `
            -Title $_.title `
            -Department $_.department `
            -MobilePhone $_.mobile

        # 若查到直接下属用户,添加到directReports属性
        if($DirectReportUser) {
            Set-ADUser -Identity $ADUser -Add @{ directReports = $DirectReportUser.DistinguishedName }
        } else {
            Write-Warning "未找到直接下属用户: $($_.directReport),无法为用户$($_.userPrincipalName)设置Direct Reports"
        }
    } else {
        Write-Warning "未找到AD用户: $($_.userPrincipalName)"
    }    
}
  1. 额外注意事项:
    • 若要替换已有直接下属,将-Add换成-Replace;若要移除特定下属,使用-Remove @{ directReports = "目标用户DN" }。
    • 确保CSV中directReport列的值能唯一识别AD用户(推荐用DistinguishedName、UPN或SamAccountName)。

内容的提问来源于stack exchange,提问作者guilhermeherme

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:47:33