简体   繁体   中英

Importing csv data into custom attribute in Active Directory

I have a .csv file that I am using to modify custom attributes on users in Active Directory, but PowerShell does not like the script:

Import-Csv -path c:\users\user\desktop\doc.csv | ForEach-Object  { 
            Set-ADUser $_.mail -replace @{
                ExtensionAttribute1 = $_.ExtensionAttribute1
            }
        }

I get the following error:

Set-ADUser: replace

At line:2 char:4

  • Set-ADUser $_.mail -replace @{

  • CategoryInfo: InvalidOperation: (user123:ADUser) [Set-ADUser], ADInvalidOperationException

  • FullyQualifiedErrorId: ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

The CSV only has 2 columns:

extensionAttribute1,mail

Any help would be appreciated

The -Identity parameter for Set-ADUser does not take an email address (unless this also happens to be the UserPrincipalName).
Because that is unknown to us, better first try to find the user with Get-ADUser and if that succeeds set the attribute.

Import-Csv -Path 'c:\users\user\desktop\doc.csv' | ForEach-Object { 
    $user = Get-ADUser -Filter "EmailAddress -eq '$($_.mail)'" -ErrorAction SilentlyContinue
    if ($user) {
        $user | Set-ADUser -Replace @{ extensionAttribute1 = $_.extensionAttribute1 }
    }
    else {
        Write-Warning "No user with email address '$($_.mail)' found.."
    }
}

PS. I always use the exact LDAP name inside the Hash for the key name when using -Add , -Replace etc. Case sensitive.

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