0

I'm trying to update a few (560) users on my domain. they are using incomplete and/or incorrect names according to my workers DB.

I created a CSV file containing this info:

samaccoutname,Name,givenname,surname
r001248,ADRIANA DAS COUVE ,ADRIANA ,DAS COUVE
r020230,ALEXANDRA DAS NEVE ,ALEXANDRA ,DAS NEVE

This is my code but it isn't working out:

#
# Script.ps1
#

Import-Module activedirectory

$userlist = Import-Csv C:\Users\r013462\Documents\Atualização_AD.csv -Delimiter ","

foreach ($user in $userlist)
{
    $GivenN = $user.givenName
    $FullN = $user.Name
    $SurN = $user.surName
    Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName $GivenN -Surname $SurN -DisplayName $FullN
}

suggestions?

  • Your subexpression `$()` is entirely unnecessary. – Maximilian Burszley Nov 09 '17 at 19:37
  • changed ; to , and these changes, still not working foreach ($user in $userlist) { $GivenN = $user.givenName $FullN = $user.Name $SurN = $user.surName Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName $GivenN -Surname $SurN -DisplayName $FullN } – Sidney Morais Nov 09 '17 at 19:41
  • added -delimiter "," not worked – Sidney Morais Nov 09 '17 at 19:42
  • getting this as return: Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again. [ERROR] At C:\Users\r013462\source\repos\rename-project\rename-project\Script.ps1:14 char:23 [ERROR] + Get-ADUser -Identity $user.samaccountname | Set-ADUser -GivenName ... – Sidney Morais Nov 09 '17 at 19:43
  • Missing character capitalized. `samaccoutname,Name,givenname,surname` -> `samaccouNtname,Name,givenname,surname` – BenH Nov 09 '17 at 19:51
  • @BenH compiled but haven't changed anything in Active Directory. continuing investigating. thanks for now – Sidney Morais Nov 09 '17 at 19:55
  • actually it changed display name but didn't changed name parameter on AD. Continuing. – Sidney Morais Nov 09 '17 at 20:02

1 Answers1

1

Ok, finally, came to a solution!

if you want to do it, you have to use same parameters of my csv orginally posted and use this script:

#
# Script.ps1
#

Import-Module activedirectory

$varCSV = ""
$userlist = Import-Csv -Path $varCSV -Delimiter ","

foreach ($user in $userlist)
{
    $samN = $user.samaccouNtname
    $GivenN = $user.GivenName
    $FullN = $user.Name
    $SurN = $user.Surname
    $dn = (Get-ADUser -Identity $samN).DistinguishedName
    Get-ADUser -Identity $user.SamAccountName | Set-ADUser -GivenName $GivenN -SurName $SurN -DisplayName $FullN  
    Try {
        Rename-ADObject $dn -NewName $FullN
    }

    catch {
        Write-Output "usuario repetido: " ($user.samaccountname) | Out-File C:\errors.txt -Append
    }

}