The Set-AzureADUserThumbnailPhoto PowerShell cmdlet ImageByteArray"

When you try and set an Azure Active Directory user thumbnail using the ImageByteArray parameter the command fails and you see the following error

System.Management.Automation.CmdletInvocationException:   ---> System.ArgumentException:  

   at Microsoft.Open.Azure.AD.CommonLibrary.Utilities.ImageUtils.GetImage(Stream fileStream, String filePath, Byte[] imageByteArray) in X:\bt\1205398\repo\src\dev\PowerShell.V2\CommonLibrary\Utilities\ImageUtils.cs:line 114


This appears to be a bug in the internal code of the PowerShell cmdlets. To work around this in C# you need to convert your byte[] array to a memory stream, and use the FileStream parameter. This should work as expected.


/// <summary>
/// Sets the thumbnail photo for the specified acccount.
/// </summary>
/// <param name="identifier">The user principal name or object identifier of the user for which the thumbnail photo value should be set.</param>
/// <param name="thumbnailPhoto">The automation property that contains the thumbnail photo to set.</param>
/// <remarks>
/// The method uses a memory stream as a workaround to a bug in the PowerShell module whereby the ImageByteArray parameter has not been implemented.
/// https://docs.microsoft.com/en-us/powershell/module/azuread/set-azureaduserthumbnailphoto?view=azureadps-2.0
/// </remarks>
protected void SetThumbnailPhoto(String identifier, ByteArrayAutomationProperty thumbnailPhoto)
{
    try
    {
        if (thumbnailPhoto.IsNotSet) { return; }
        String objectIdentifier = StringSupport.IsGuid(identifier) ? identifier : GetAccountObjectIdentifier(identifier);
        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(thumbnailPhoto.ByteArrayValue, 0, thumbnailPhoto.ByteArrayValue.Length);
            PowerShellCommandSupport.ExecuteCommand(runspace, "Set-AzureADUserThumbnailPhoto", "ObjectId", objectIdentifier, "FileStream", stream);
        }
    }
    catch (Exception ex) { throw new AutomationAgentExecutionException(String.Format(Resources.Office365AccountAgentBase.SetThumbnailPhotoException, ex.Message), ex); }
}


Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations