简体   繁体   English

PowerShell - 更改 AD 属性时出错

[英]PowerShell - Error Changing AD Attributes

Running a PowerShell script to update user attributes.运行 PowerShell 脚本以更新用户属性。 I am using a test user to start with but everything seems in line.我正在使用测试用户开始,但一切似乎都符合要求。 I tried the list file as both an Excel and CSV and get the same error.我尝试将列表文件作为 Excel 和 CSV 并得到相同的错误。 I matched up the data with the AD attributes as best as possible for simplicity.为简单起见,我尽可能将数据与 AD 属性匹配。 I'm missing something but cant see where it is.我错过了一些东西,但看不到它在哪里。

Excel\\CSV Data: Excel\\CSV 数据:

在此处输入图片说明

PowerShell code: PowerShell 代码:

$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2) {get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" | set-aduser -displayname $($user.name) -surname $($user.last) -givenname $($user.first) -replace @{manager=$($user.manager)} -description $($user.description)}

Error Message:错误信息:

set-aduser : The name reference is invalid
At line:1 char:97
+ ... ntname)'" | set-aduser -displayname $($user.displayname) -surname $($user.la ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=John Doe,OU=...riese,DC=office:ADUser) [Set-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8373,Microsoft.ActiveDirectory.Management.Commands.SetADUser

First issue is your displayname column you try to reference with name in error.第一个问题是您尝试使用名称错误引用的显示名称列。 Second issue is you need to provide an AD user distinguished name for the manager.第二个问题是您需要为经理提供 AD 用户专有名称。

$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2)
{
    get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" |
        set-aduser -displayname $($user.displayname) -surname $($user.last) -givenname $($user.first) -replace @{manager = $(get-aduser $user.Manager).distinguishedname} -description $($user.description)
}

You should also consider splatting for readability.您还应该考虑使用 splatting 以提高可读性。

$users2 = import-excel "c:\orgstafflist2.xlsx"
foreach ($user in $users2)
{
    $UserParam = @{
        displayname = $user.displayname
        surname = $user.last
        givenname = $user.first
        replace = @{manager = (get-aduser $user.Manager).distinguishedname}
        description = $user.description
    }
    get-aduser -filter "SamAccountName -eq '$($user.samaccountname)'" | set-aduser @UserParam
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM