Powershell Set ADuser:是否可以使用DisplayName更新AD中的用户属性?

Powershell Set ADuser:是否可以使用DisplayName更新AD中的用户属性?,powershell,active-directory,Powershell,Active Directory,我需要使用powershell更新AD中许多用户的employeeID属性。不幸的是,我没有他们的用户名或samaccountname,只有DisplayName。我可以使用DisplayName作为过滤器来获取用户,但使用set aduser时它不起作用。是否有任何方法可以使用get aduser获取samaccountname,然后使用它通过set aduser更新用户 另外,请注意,脚本不要覆盖任何现有值,这一点很重要 我的当前(非功能)脚本: CSV文件如下所示: employeeid,

我需要使用powershell更新AD中许多用户的employeeID属性。不幸的是,我没有他们的用户名或samaccountname,只有DisplayName。我可以使用DisplayName作为过滤器来获取用户,但使用set aduser时它不起作用。是否有任何方法可以使用get aduser获取samaccountname,然后使用它通过set aduser更新用户

另外,请注意,脚本不要覆盖任何现有值,这一点很重要

我的当前(非功能)脚本:

CSV文件如下所示:

employeeid,GivenName,Surname,displayname
489900,Angela,Davis,Angela Davis

任何意见或建议,谢谢

如评论所述,这实际上是的副本,但由于OP没有投票或接受任何给定的答案,因此我无法将其标记为副本

如前所述,您使用的过滤器是错误的。此外,在
Set ADUser
上没有
-Filter
参数,就像在其对应的
Get ADUser
上一样

这应该满足您的要求:

Import-Csv -Path 'c:\test\users.csv' | ForEach-Object {
    $ADUserObject = Get-ADUser -Filter "DisplayName -eq '$($_.displayname)'" -Properties DisplayName, employeeID -ErrorAction SilentlyContinue
    if ($ADUserObject) {
        # check if this user already has an EmployeeId filled in
        if ($ADUserObject.EmployeeID) {
            Write-Host "User $($ADUserObject.DisplayName) already has EmployeeId $($ADUserObject.EmployeeID)"
        }
        else {
            Write-Host "Setting EmployeeID $($ADUserObject.EmployeeID) for user $($ADUserObject.DisplayName)"
            $ADUserObject | Set-ADUser -EmployeeID $_.employeeid
        }
    }
    else {
        Write-Warning "User $($_.DisplayName) could not be found"
    }
}

“DisplayName-eq'$line.DisplayName'”
->“DisplayName-eq'$($line.DisplayName)”脚本仍然失败,因为-Filter不能用作Set-ADuser的参数。请查看@JoachimHollekim use
$ADuser对象| Set-ADuser…
Import-Csv -Path 'c:\test\users.csv' | ForEach-Object {
    $ADUserObject = Get-ADUser -Filter "DisplayName -eq '$($_.displayname)'" -Properties DisplayName, employeeID -ErrorAction SilentlyContinue
    if ($ADUserObject) {
        # check if this user already has an EmployeeId filled in
        if ($ADUserObject.EmployeeID) {
            Write-Host "User $($ADUserObject.DisplayName) already has EmployeeId $($ADUserObject.EmployeeID)"
        }
        else {
            Write-Host "Setting EmployeeID $($ADUserObject.EmployeeID) for user $($ADUserObject.DisplayName)"
            $ADUserObject | Set-ADUser -EmployeeID $_.employeeid
        }
    }
    else {
        Write-Warning "User $($_.DisplayName) could not be found"
    }
}