Link to home
Start Free TrialLog in
Avatar of smart Z
smart 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.
Avatar of smart Z
smart Z

ASKER

Any takes?
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
Avatar of smart Z

ASKER

Yes all pictures are sized properly.

The pictures are named by firstname_lastnamte
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
Okay, I didn't escape auto correct. Let me post a fix before you try testing.

Chris
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
Avatar of smart Z

ASKER

Another question .

Do I run the power shell in the DC or Exchange?
which server ?
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
Avatar of smart 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?
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial