I
I
ifossa2019-04-12 11:59:07
PowerShell
ifossa, 2019-04-12 11:59:07

How to add photo to user's jpegPhoto and thumbnailPhoto attributes in AD using PowerShell GUI?

I'm trying to make a simple form (using the PowerShell GUI) to edit user attributes.
The form has three controls, two Buttons ("Select Photo" and "Apply Changes") and a PictureBox.
The idea is this:
When I click the "Select Photo" button, I select an arbitrary image, for example userPhoto.jpg. Using the Resize-Image function, I reduce the image to the desired size and insert it into the PictureBox.

$imgFile = (get-item $openfiledialog.FileName)
$picturebox.Image = Resize-Image -InputFile $imgFile

Up to this point everything is working as intended.
I want to ensure that the cropped image from the PictureBox, without intermediate saving to a file, can be transferred to the jpegPhoto and thumbnailPhoto user attributes using the Set-ADUser cmdlet.
Set-ADUser -Identity testUser -Replace @{ thumbnailPhoto = $picturebox.Image }
Set-ADUser -Identity testUser -Replace @{ jpegPhoto = $picturebox.Image }

This code produces errors:
Invalid type 'System.Management.Automation.PSCustomObject'.
Parameter name: thumbnailPhoto
    + CategoryInfo          : InvalidArgument: (testUser:ADUser) [Set-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADUser
    + PSComputerName        : DC1.local.example.net
 
Invalid type 'System.Management.Automation.PSCustomObject'.
Parameter name: jpegPhoto
    + CategoryInfo          : InvalidArgument: (testUser:ADUser) [Set-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADUser
    + PSComputerName        : DC1.local.example.net

I understand that the problem is in this place (thumbnailPhoto = $picturebox.Image), but I can’t solve it myself.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ifossa, 2019-04-16
@ifossa

Seems to have figured it out.
It turned out something like this:

$stream = New-Object System.IO.MemoryStream
$picturebox.Image.Save($stream, [System.Drawing.Imaging.ImageFormat]::Jpeg) | Out-Null
[byte[]]$pictureData = $stream.ToArray()
$stream.Dispose()
Set-ADUser -Identity testUser -Replace @{ thumbnailPhoto = $pictureData; jpegPhoto = $pictureData }

K
Kirill, 2019-04-12
@GoooodBoy

You need to convert the photo before uploading it to AD
$photo = [byte[]](Get-Content "C:\temp\crusoe.jpg" -Encoding byte)
Set-ADUser -Identity testUser -Replace @{thumbnailPhoto=$photo }

D
D_mi_try, 2019-04-22
@D_mi_try

To upload photos to AD, I recommend using CodeTwo Active Directory Photos

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question