Help with Power Shell.

hello experts,

 I have a folder with users Pictures and I want to add the picture in active directory for all the users using PowerShell.

Please note I have already extended the schema to include the thumbnailPhoto. But need assistance to upload 100 pictures in total. The pictures are sitting in C:\photos\Firstname_Lastname.jpg of the domain controller.

Thank you.
LVL 4
Habib ZakariaNetwork Solutions ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
Any takes?
0
Chris DentPowerShell DeveloperCommented:
Are all the pictures appropriately sized?

How are the pictures named? That is, will the name let us find a user?

In principal you want:

$Photo = Get-Content afile.jpg -Encoding Byte
Set-ADUser someuser -Replace @{thumbnailPhoto=$Photo}

More can be done of course, but that remains at the heart.

Chris
0
Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
Yes all pictures are sized properly.

The pictures are named by firstname_lastnamte
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Chris DentPowerShell DeveloperCommented:
So perhaps something along these lines:
Get-ChildItem photosFolder | ForEach-Object {
  $FirstName, $LastName = $_.BaseBand.Split('_')
  $ADUser = Get-ADUser -Filter { given Name -eq $FirstName -and sn -eq $Last Name }

  if ($ADUser -and ([Array]$ADUser).Count -eq 1) {
    $Photo = Get-Content $_.FullName -Encoding Byte
    Set-ADUser -Identity $ADUser.DistinguishedName -Replace @{thumbnailPhoto=$Photo} -WhatIf
  } else {
    Write-Host "Too many or too few matches for $FirstName $LastName ($($_.Name))."
  }
}

Open in new window

Written on my mobile so hopefully auto correct didn't break anything.

WhatIf prevents it making the change, good for testing. Let me know how you get on.

Cheers,
Chris
0
Chris DentPowerShell DeveloperCommented:
Okay, I didn't escape auto correct. Let me post a fix before you try testing.

Chris
0
Chris DentPowerShell DeveloperCommented:
Minus mobile-based auto-correct. Let's see if I fixed all of them :)
Get-ChildItem photosFolder | ForEach-Object {
  $FirstName, $LastName = $_.BaseName.Split('_')
  $ADUser = Get-ADUser -Filter { givenName -eq $FirstName -and sn -eq $LastName }

  if ($ADUser -and ([Array]$ADUser).Count -eq 1) {
    [Byte[]]$Photo = Get-Content $_.FullName -Encoding Byte
    Set-ADUser -Identity $ADUser.DistinguishedName -Replace @{thumbnailPhoto=$Photo} -WhatIf
  } else {
    Write-Host "Too many or too few matches for $FirstName $LastName ($($_.Name))."
  }
}

Open in new window

Chris
0
Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
Another question .

Do I run the power shell in the DC or Exchange?
which server ?
0
Chris DentPowerShell DeveloperCommented:
Anywhere at all which has, or can have the MS AD tools (Remote Server Admin Tools) installed. Win Vista or 2008 or greater if I remember correctly.

If there's some reason you can't use those let me know and I'll change the method. They're not mandatory for this really.

Beyond that it needs network access to a DC and direct or file share access to the photos.

Chris
0
Habib ZakariaNetwork Solutions ArchitectAuthor Commented:
Hi Chris,

I created a folder in C:\Photos\PhotoFolder.

Ran the script but I do not think the photo loaded. Something did not work well. I did not see any error message but the What if statement came up.

 What if: Performing the operation "Set" on target "CN=xxxxx xxxxxi,OU=Local Profile Users,DC=cnc,DC=local".

Any idea why?
0
Chris DentPowerShell DeveloperCommented:
Yep, but the note about it got covered with mobile coding problems.

Set-ADUser has a -WhatIf parameter set to allow basic testing. Remove that and it'll go ahead and make the change.

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.