简体   繁体   中英

PowerShell - Error Changing AD Attributes

Running a PowerShell script to update user attributes. 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. I matched up the data with the AD attributes as best as possible for simplicity. I'm missing something but cant see where it is.

Excel\\CSV Data:

在此处输入图片说明

PowerShell code:

$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.

$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.

$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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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