Avatar of Habi Z
Habi Z
 asked on

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.
Shell ScriptingPowershell

Avatar of undefined
Last Comment
Chris Dent

8/22/2022 - Mon
Habi Z

ASKER
Any takes?
Chris Dent

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
Habi Z

ASKER
Yes all pictures are sized properly.

The pictures are named by firstname_lastnamte
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Dent

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
Chris Dent

Okay, I didn't escape auto correct. Let me post a fix before you try testing.

Chris
Chris Dent

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Habi Z

ASKER
Another question .

Do I run the power shell in the DC or Exchange?
which server ?
Chris Dent

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
Habi Z

ASKER
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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.