Hi everybody , I'm new on this forum so I hope that I post my message where I have to . I'm not english too so if you don't understand my problem , just tell me :)
I would like to change users propreties like his firstname , lastname ... with the right informations in a Csv file ( so the user is on the AD and on the list ) but I've got a strange error at the command "Set-ADUser" :
Set-ADUser : replace
Au caractère C:\Projet2\New user.ps1:33 : 9
+ Set-ADUser -Identity $SAM -GivenName $Firstname -Surname $Lastname -Disp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (backelas01d:ADUser) [Set-ADUser], ADInvalidOperationException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser
I searched but I didn't find the answer to my problem , and someone with the same code as mine succeed , I can't understand what's happening ...
Here is the code , it didn't want me to upload the file ^^
Import-Module ActiveDirectory
$OU = "OU=Test,DC=TEST,DC=LO" #L' OU à spécifier est celle où vous voulez que les Users soient créés
$Path = "C:\Projet2\" # Ce chemin est à changer selon vos préférences
$compteExistant =[string[]](Get-ADUser -Filter * -Properties * -SearchBase $OU).SamAccountName
$pass = ConvertTo-SecureString -String "Azerty01!" -AsPlainText –Force #Convertit le mot de passe en châine sécurisée afin qu'il puisse être utilisé lors de la création
$users = Import-Csv -Encoding UTF8 -Path $Path\DomainUsers3.csv
$users | Get-Member
foreach ($User in $users)
{
$Name = $User.name
$Firstname = $User.Firstname
$Lastname = $User.LastName
$Detailedname = $Firstname +" " + $Lastname
$logonName = $User.LogonName
$SAM = $User.SamAccountName
$upn = "$SAM" + "@TEST.LO" # Changer ce qui est entre guillemets par votre nom de domaine ex: "@CA01.fr"
$description = $User.Description
$company = $User.Company
<# try {
New-ADUser -Name $SAM -SamAccountName $SAM -GivenName $Firstname -Surname $Lastname -DisplayName $Detailedname -UserPrincipalName $logonName -Company $company -Description $description -AccountPassword $pass -Enabled $true -ChangePasswordAtLogon $true -Path $OU
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityAlreadyExistsException]
{
Write-Host "L'utilisateur"$SAM "était déjà présent , mais cela n'affecte pas la création des autres utilisateurs"
}#>
if ($compteExistant -eq $SAM) { Write-Host "L'utilisateur" $SAM "existe déjà , ces données seront modifiées par celles contenues dans le fichier Csv"
Set-ADUser -Identity $SAM -GivenName $Firstname -Surname $Lastname -DisplayName $Detailedname -UserPrincipalName $logonName -Company $company -Description $description
}
else {
New-ADUser -Name $SAM -SamAccountName $SAM -GivenName $Firstname -Surname $Lastname -DisplayName $Detailedname -UserPrincipalName $logonName -Company $company -Description $description -AccountPassword $pass -Enabled $true -ChangePasswordAtLogon $true -Path $OU
}
}
I hope that someone could help me . Thanks for reading