Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, January 20, 2015 1:13 PM
hi !
Im new in PowerShell and my code is working perfectly fine :
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\importation1.csv
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
$userX = Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'" –Properties * -SearchBase "DC=base,DC=fr"
$Date = Get-Date -Format g
if ($userX -eq $Null)
{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
Write "$Date, l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
} else {
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
Write "$Date, l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
#Recherche dans un OU spécifique et met à jour les attributs
Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'" –Properties * -SearchBase "DC=base,DC=fr" |
Set-ADUser -Title $($user.Title) -givenName $($user.givenName) -employeeNumber $($user.employeeNumber) -displayName $($user.displayName) -initials $($user.initials) -manager $($user.manager) -company $($user.company) -department $($user.department) -userPrincipalName $($user.userPrincipalName)
Set-ADUser $user.SamAccountName -Add @{extensionattribute1=$user.extensionattribute1;telephoneNumber=$user.telephoneNumber;departmentNumber=$user.departmentNumber;sn=$user.sn}
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
But I do have a problem which is :
On my CSV file, some information are missing and i'd like to execute my script anyway but it doesnt allow me because of this missing informations. How do i do to make PowerShell ignore those file ?
Example CSV FILE
department,departmentNumber,displayName,employeeNumber,givenName,initials,sAMAccountName,sn,title
Informatique,M949,Toto TOT,,Toto,TTO,ttot,TOT,Informaticien
END CSV FILE
As you can see, this guy doesnt have an employeeNumber and i'd like that PowerShell let it blank but actually I've an error when I execute my script...
I've search without finding a solution so I'm coming here hoping someone to save me !
Thanks !!
Oh and sorry for my english, french guy talking as you can see in the code !!
Oh there might be a lot of useless stuff in my code so any suggestion to make it less "big" is welcome !!
Thank you !
All replies (29)
Wednesday, January 21, 2015 9:27 AM ✅Answered
You are probably goingto have to do it this way.
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\importation1.csv
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
manager=$user.manager
company=$user.company
department=$user.department
userPrincipalName=$user.userPrincipalName
OfficePhone=$user.telephoneNumber
surname=$user.sn
EmployeeNumber=$user.employeeNumber
Replace=@{extensionattribute1=$user.extensionattribute1;DepartmentNumber=$user.departmentNumber}
}
$userX|Set-ADUser @userprops
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
Note that "Manager" has to be a DistinguishedName string.
¯\(ツ)_/¯
Wednesday, January 21, 2015 9:43 AM ✅Answered
If you fields that are empty strings you can do this:
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
manager=$user.manager
company=$user.company
Department=if($user.department){$user.department}else{$null}
userPrincipalName=$user.userPrincipalName
OfficePhone=$user.telephoneNumber
surname=$user.sn
EmployeeNumber=$user.employeeNumber
Replace=@{extensionattribute1=$user.extensionattribute1;DepartmentNumber=$user.departmentNumber}
}
Notice how I handled "Department"
** Department=if($user.department){$user.department}else{$null}**
This says that if the field is a blank or empty string to rplace it with a $null. If you have items in the CSV like this:
value,value,,," ".value
Blank strings canbe handled by trimming. I reallity a CSV should never have a blank string. Empty fields should be set by commas alone. If the CSV is legal thenyou do not need to trim - just test for empty and set to $null if empty. We can assign nullor a string of one or mmore espaces. An empty string throws an error on some fields.
¯\(ツ)_/¯
Wednesday, January 21, 2015 11:58 AM ✅Answered
Glad it works. You will find alll of those methods can be useful at vaious times.
Try this too.
function Write-Log{
Param(
[string]$msg,
$color='green',
$logfile='C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt'
)
Write-host $msg -ForegroundColor Green
$out='[{0}] {1}' -f ([datetime]::Now.ToString('g'),$msg
$out | Out-File $logfile -Append
}
Now this: Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
Becomes this:
Write-Log "$UserN n'existe pas dans l'AD" -color red
¯\(ツ)_/¯
Tuesday, January 20, 2015 5:49 PM
There are many issue with your script. Start with this:
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
$users = Import-Csv -Path C:\Users\importation1.csv
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'" –Properties *){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
manager=$user.manager
company=$user.company
department=$user.department
userPrincipalName=$user.userPrincipalName
HomePhone=$user.telephoneNumber
department=$user.departmentNumber
surname=$user.sn
}
if($user.employeeNumber){
$userprops.employeeNumber=$user.employeeNumber
}
$userX|Set-ADUser @userprops
$userX|Set-ADUser -Add @{extensionattribute1=$user.extensionattribute1}
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
¯\(ツ)_/¯
Tuesday, January 20, 2015 5:59 PM | 1 vote
We can actually ddo it all inone call.
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
$users = Import-Csv -Path C:\Users\importation1.csv
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
manager=$user.manager
company=$user.company
department=$user.department
userPrincipalName=$user.userPrincipalName
OfficePhone=$user.telephoneNumber
Department=$user.departmentNumber
surname=$user.sn
EmployeeNumber=$user.employeeNumber
Add=@{extensionattribute1=$user.extensionattribute1}
}
$userX|Set-ADUser @userprops
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
Using this method we can also allow values to be null and they will just be ignored
¯\(ツ)_/¯
Wednesday, January 21, 2015 8:53 AM
Thanks a lot for your help !!
So, at first there was an error with department number, it didn't find it so I add it in the "Add=@" part.
Then I have this error and I dont know what to do :
PS C:\Users\administrateur.FOUGERE\Desktop> powershell .\PowerShellTest.ps1
Mise à jour des attributs de l'AD.......
Toto TOT existe dans l'AD
Set-ADUser : replace
Au niveau de C:\Users\administrateur.FOUGERE\Desktop\PowerShellTest.ps1 : 33 Caractère : 26
+ $userX|Set-ADUser <<<< @userprops
+ CategoryInfo : InvalidOperation: (CN=Toto TOT,OU=...C=fougere,DC=fr:ADUser) [Set-ADUser], ADInvalidOper
ationException
+ FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Fait!
And I dont know what to do ! :/
Sorry Im a bit (completely in fact) useless ! :/
So there is the code I've now :
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\importation1.csv
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'" –Properties * -SearchBase "DC=fougere,DC=fr"){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
manager=$user.manager
company=$user.company
department=$user.department
userPrincipalName=$user.userPrincipalName
OfficePhone=$user.telephoneNumber
surname=$user.sn
EmployeeNumber=$user.employeeNumber
Add=@{extensionattribute1=$user.extensionattribute1;DepartmentNumber=$user.departmentNumber}
}
$userX|Set-ADUser @userprops
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
Waiting for your answer ! Thanks again :)
Wednesday, January 21, 2015 9:02 AM
The following line i a waste of time:
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'" –Properties * -SearchBase "DC=fougere,DC=fr"){
It should be:
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
SamAccountName is unique accross a domain.
¯\(ツ)_/¯
Wednesday, January 21, 2015 9:13 AM
Yep ! I deleted it ! Thanks you were right :)
But I've still the same error ! :/
Set-ADUser : replace
Au niveau de C:\Users\administrateur.FOUGERE\Desktop\PowerShellTest.ps1 : 33 Caractère : 26
+ $userX|Set-ADUser <<<< @userprops
+ CategoryInfo : InvalidOperation: (CN=Toto TOT,OU=...C=fougere,DC=fr:ADUser) [Set-ADUser], ADInvalidOper
ationException
+ FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Wednesday, January 21, 2015 9:48 AM
Oh okay I just found out what the problem is and it's actually the same as before.
I deleted just the line
EmployeeNumber=$user.employeeNumber
and it worked fine, but it's because the information is not given in the CSV file.
And I do want this line even if the information is not given ! :/
But thanks the rest works perfectly fine !! :D
Wednesday, January 21, 2015 9:52 AM
If you have blank departmentnumber values you will need to assign them thisway:
Replace=@{
extensionattribute1=$user.extensionattribute1
DepartmentNumber=if($user.departmentNumber){$user.departmentNumber}else{' '}
}
We need to assign it to a string with one or more spaces.
These are some of the many reasonswhy I have not been very happy with the way MS designed these CmdLets. They lack consistency.
¯\(ツ)_/¯
Wednesday, January 21, 2015 9:56 AM
Okay then I think I get it, but do I have to do it for every information ?
Because sometimes it's going to be the departmentNumber, but sometimes it'll be employeeNumber etc ?
But I'll try this, I think you gave me everything to make it correct !
Thanks !! :)
Wednesday, January 21, 2015 9:59 AM
Oh okay I just found out what the problem is and it's actually the same as before.
I deleted just the lineEmployeeNumber=$user.employeeNumber
and it worked fine, but it's because the information is not given in the CSV file.
And I do want this line even if the information is not given ! :/But thanks the rest works perfectly fine !! :D
that is where you use the if construct to assign a null when the line is blank.
You really should fix teh CSV file. It should not have bad comma sequences.
This:
,,,,,
Is good.
This:
, , , ,
Is bad.
It sounds like the system generating your CSV is badly designed.
¯\(ツ)_/¯
Wednesday, January 21, 2015 10:06 AM
This is weird because it's how my CSV file looks ! No ?
cn,company,department,departmentNumber,displayName,employeeNumber,givenName,initials,sAMAccountName,sn,title,manager,telephoneNumber,extensionAttribute1,userPrincipalName
Toto TOT,Laboratoire Science et Nature,Informatique,M949,Toto TOT,,Toto,TTO,ttot,TOT,Informaticien,"CN=Guillaume CLASS,OU=Utilisateurs,OU=LSN,DC=fougere,DC=fr",307,INFORMATIQUE,[email protected]
But it works !
I did this and it works, the code can be executed and the field is blank in the AD so I guess it's perfect ! :)
Replace=@{
extensionattribute1=$user.extensionattribute1
employeeNumber=if($user.employeeNumber){$user.employeeNumber}else{' '}
DepartmentNumber=if($user.departmentNumber){$user.departmentNumber}else{' '}
}
Wednesday, January 21, 2015 10:07 AM
Okay then I think I get it, but do I have to do it for every information ?
Because sometimes it's going to be the departmentNumber, but sometimes it'll be employeeNumber etc ?
But I'll try this, I think you gave me everything to make it correct !Thanks !! :)
As I posted; ether fix the CSV or guard all fields that can contains spaces. True empty fields will evaluate to null and will work correctly.
¯\(ツ)_/¯
Wednesday, January 21, 2015 10:13 AM
As I posted; ether fix the CSV or guard all fields that can contains spaces. True empty fields will evaluate to null and will work correctly.
Sorry I might haven't seen it or understand it ! :/
My english isn't that good so Im doing my best to understand all your advices.
But thanks a lot for everything !
Wednesday, January 21, 2015 10:14 AM
I prefer to do batch updates with ADSI.
You can also use the User objejct and update it directly
$userx.EmployeeNumber=$user.EmployeeNumber
Set-AdUser $user.
The CmdLet will find and apply only changes. I would them do this:
if($user.EmployeeNumber){$userX.EmployeeNumber=$user.EmployeeNumber}
This will skip the assignment if the field is blank.
¯\(ツ)_/¯
Wednesday, January 21, 2015 10:19 AM
Like this:
foreach ($user in $users) {
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
$userX.Title=$user.Title
$userX.givenName=$user.givenName
$userX.displayName=$user.displayName
$userX.initials=$user.initials
$userX.manager=$user.manager
$userX.company=$user.company
$userX.department=$user.department
$userX.userPrincipalName=$user.userPrincipalName
$userX.OfficePhone=$user.telephoneNumber
$userX.surname=$user.sn
if($user.employeeNumber){$userX.EmployeeNumber=$user.employeeNumber}
$userX.extensionattribute1=$user.extensionattribute1
if($user.departmentNumber){$userX.DepartmentNumber=$user.departmentNumber}
Set-ADUser $UserX
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
}
}
¯\(ツ)_/¯
Wednesday, January 21, 2015 10:22 AM
As I posted; ether fix the CSV or guard all fields that can contains spaces. True empty fields will evaluate to null and will work correctly.
Sorry I might haven't seen it or understand it ! :/
My english isn't that good so Im doing my best to understand all your advices.
But thanks a lot for everything !
Actually on my WIndows the blank fields work. What version of AD/NT are you running. I am testing on WS2008R2.
It may be a version issue or even a difference in the MUI that you are running on. Your CSV does look OK.
¯\(ツ)_/¯
Wednesday, January 21, 2015 10:43 AM
The new code you gave me doesn't work ! :/
there is no error, so everything seems working but when I looked at the profil the informations where not updated! :)
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\importation1.csv
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
$userX.Title=$user.Title
$userX.givenName=$user.givenName
$userX.displayName=$user.displayName
$userX.initials=$user.initials
$userX.manager=$user.manager
$userX.company=$user.company
$userX.department=$user.department
$userX.userPrincipalName=$user.userPrincipalName
$userX.OfficePhone=$user.telephoneNumber
$userX.surname=$user.sn
if($user.employeeNumber){$userX.EmployeeNumber=$user.employeeNumber}
$userX.extensionattribute1=$user.extensionattribute1
if($user.departmentNumber){$userX.DepartmentNumber=$user.departmentNumber}
Set-ADUser $UserX
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
Im working on WS2008R2 too but we haven't done update for a long time so it might be the reason why it's not working ! :/
Wednesday, January 21, 2015 10:49 AM
Sorry - this line should look like this:
Set-ADUser-Instance $UserX
¯\(ツ)_/¯
Wednesday, January 21, 2015 11:09 AM
NICE !!! This is even better ! It works fine and when there is ,,, in the CSV file it doesn't write it as a blank string but it stay <not defined> !
Thanks a lot !!! I wouldn't have done it without your help !
So there is my final code :
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\importation1.csv
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-host "$UserN existe dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN existe dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
$userX.Title=$user.Title
$userX.givenName=$user.givenName
$userX.displayName=$user.displayName
$userX.initials=$user.initials
$userX.manager=$user.manager
$userX.company=$user.company
$userX.department=$user.department
$userX.userPrincipalName=$user.userPrincipalName
if($userX.OfficePhone){$user.telephoneNumber}
$userX.surname=$user.sn
if($user.employeeNumber){$userX.EmployeeNumber=$user.employeeNumber}
$userX.extensionattribute1=$user.extensionattribute1
if($user.departmentNumber){$userX.DepartmentNumber=$user.departmentNumber}
Set-ADUser -Instance $UserX
}else{
Write-host "$UserN n'existe pas dans l'AD" -ForegroundColor Green
"$([datetime]::Now.ToString('g')), l'utilisateur $UserN n'existe pas dans l'AD" | Out-File C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt -Append
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
Wednesday, January 21, 2015 3:40 PM
Thanks for this function ! It's working perfectly (talking about the function ! :P)
About the rest... It was to perfect to be right ...
Here is my code :
# Import AD Module
Import-Module ActiveDirectory
write-Host "Mise à jour des attributs de l'AD......." -ForegroundColor Yellow
# Import du CSV dans la variable $users
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\importation1.csv
# fonction pour la création d'un log
function Write-Log{
Param(
[string]$msg,
$color='green',
$logfile='C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt'
)
Write-host $msg -ForegroundColor Green
$out='[{0}] {1}' -f ([datetime]::Now.ToString('g'),$msg)
$out | Out-File $logfile -Append
}
# Boucle sur le CSV pour mettre à jour les utilisateurs si ceux ci existent
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-Log "$UserN existe dans l'AD" -color red
$userX.Title=$user.Title
$userX.givenName=$user.givenName
$userX.displayName=$user.displayName
$userX.initials=$user.initials
if($userX.manager){$user.manager}
$userX.company=$user.company
if($userX.department){$user.department}
$userX.userPrincipalName=$user.userPrincipalName
if($userX.OfficePhone){$user.telephoneNumber}
$userX.surname=$user.sn
$userX.EmployeeNumber=$user.employeeNumber
$userX.extensionattribute1=$user.extensionattribute1
if($userX.DepartmentNumber){$user.departmentNumber}
Set-ADUser -Instance $UserX
}else{
Write-Log "$UserN n'existe pas dans l'AD" -color red
}
}
Write-Host 'Fait!' -ForegroundColor Yellow #-NoNewline
I though all were fine, but here is the problem.
If I give, in the CSV file, the information about : telephoneNumber, manager, department or departmentNumber, the information aren't updated in the AD ...
Sorry, i though it was over but i think i'll be here for a bit more longer ! :/
Wednesday, January 21, 2015 6:10 PM
Something is odd about your AD setup. I would go back the previous version and leave it at that. The one with $userprops hash should work.
¯\(ツ)_/¯
Thursday, January 22, 2015 8:51 AM
Yep you might be right about the AD setup ! :/
I still have an error with this code
Import-Module ActiveDirectory
$users = Import-Csv -Path C:\Users\administrateur.FOUGERE\Desktop\TestImportation2.csv
function Write-Log{
Param(
[string]$msg,
$color='green',
$logfile='C:\Users\administrateur.FOUGERE\Desktop\Outputfile.txt'
)
Write-host $msg -ForegroundColor Green
$out='[{0}] {1}' -f ([datetime]::Now.ToString('g'),$msg)
$out | Out-File $logfile -Append
}
foreach ($user in $users) {
$UserN = $user.displayName
if($userX=Get-ADUser –Filter "SamAccountName -eq '$($user.samaccountname)'"){
Write-Log "$UserN existe dans l'AD" -color red
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
company=$user.company
userPrincipalName=$user.userPrincipalName
surname=$user.sn
EmployeeNumber=$user.employeeNumber
replace=@{
extensionattribute1=$user.extensionattribute1
telephoneNumber=if($user.telephoneNumber){$user.telephoneNumber}else{$null}
manager=if($user.manager){$user.manager}else{$null}
DepartmentNumber=if($user.departmentNumber){$user.departmentNumber}else{$null}
Department=if($user.department){$user.department}else{$null}
}
}
$userX|Set-ADUser @userprops
}else{
Write-Log "$UserN n'existe pas dans l'AD" -color red
}
}
Something isn't working but I can't tell what ...
Here is the error I get :
Set-ADUser : replace
Au niveau de C:\Users\administrateur.FOUGERE\Desktop\PowerShellFinal1.ps1 : 49 Caractère : 26
+ $userX|Set-ADUser <<<< @userprops
+ CategoryInfo : InvalidOperation: (CN=Toto TOT,OU=...C=fougere,DC=fr:ADUser) [Set-ADUser], ADInvalidOper
ationException
+ FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Thursday, January 22, 2015 9:06 AM
you cannot use repllace arbitrarily as you are and ther eis no attribut called ttelephonenumber.
manager cannot be set the way you are setting it.
$userprops=@{
Title=$user.Title
givenName=$user.givenName
displayName=$user.displayName
initials=$user.initials
company=$user.company
userPrincipalName=$user.userPrincipalName
surname=$user.sn
EmployeeNumber=$user.employeeNumber
Officephone=if($user.telephoneNumber){$user.telephoneNumber}else{$null}
manager=if($user.manager){$user.manager}else{$null}
Department=if($user.department){$user.department}else{$null}
replace=@{
extensionattribute1=$user.extensionattribute1
DepartmentNumber=if($user.departmentNumber){$user.departmentNumber}else{$null}
}
}
¯\(ツ)_/¯
Thursday, January 22, 2015 9:18 AM
I just ran that with every variation of blank null and set values with no issues.
¯\(ツ)_/¯
Thursday, January 22, 2015 12:30 PM
Sorry I don't get some things you are saying ! :/
Okay the 4 attributs I choose are the fourth that can be blank in the CSV that's why i wrote this but thanks now I get the point about what I was wrong.
But still, I've a really weird error :
All seems to work fine, there is no error when I apply the script but the user isn't updated in the AD ...
I don't know why ...
Thursday, January 22, 2015 1:21 PM
Are you sure the user isn't being updated?
Replication latency can make it appear that the update failed if you're checking from a different DC than the one you connected to when you made the update.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Thursday, January 22, 2015 1:51 PM
Sadly, I'm sure ...
The user won't update.
I dont know what to do ...
Edit: It's working it was completely my fault ! I made a mistake in my CSV file !
Thanks a lot ! :)
The only which is isn't working is for DepartmentNumber, it apparently can't be null but anyway i'll write a 0 in it instead so thanks a lot !!!