Help with Power Shell.

SR Zak
SR Zak used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SR ZakNetwork Solutions

Author

Commented:
Any takes?
Chris DentPowerShell Developer
Top Expert 2010

Commented:
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
SR ZakNetwork Solutions

Author

Commented:
Yes all pictures are sized properly.

The pictures are named by firstname_lastnamte
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Chris DentPowerShell Developer
Top Expert 2010

Commented:
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 DentPowerShell Developer
Top Expert 2010

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

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:
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
SR ZakNetwork Solutions

Author

Commented:
Another question .

Do I run the power shell in the DC or Exchange?
which server ?
Chris DentPowerShell Developer
Top Expert 2010

Commented:
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
SR ZakNetwork Solutions

Author

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?
PowerShell Developer
Top Expert 2010
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial