msgraph-sdk-powershell
msgraph-sdk-powershell copied to clipboard
Set-MgUserPhotoContent is returning to the host before complete.
Describe the bug
Set-MgUserPhotoContent is returning to the host before it has completed the Graph API to upload the photo file.
I'm using Set-MgUserPhotoContent in a script in a try block where the very next line is a Remove-Item cmdlet to delete a temporary image file. The Remove-Item cmdlet is deleting the file before Set-MgUserPhotoContent has completed the Graph API to upload it which then causes and error for Set-MgUserPhotoContent.
Also tried while using the -PassThru parameter and assigning the output to a variable.
Expected behavior
Set-MgUserPhotoContent should not return until the underlying Graph API call is complete or errored. Or have a parameter that enables it to wait.
How to reproduce
$user = [email protected] $file = get-item .\tempphoto.jpg Set-MgUserPhotoContent -UserId $user –InFile $file.FullName Remove-Item $file.FullName -force
SDK Version
2.19.0
Latest version known to work for scenario above?
No response
Known Workarounds
No response
Debug output
DEBUG: Content type DEBUG: ============================ HTTP REQUEST ============================
HTTP Method: PUT
Absolute Uri: https://graph.microsoft.com/v1.0/users/72f917be-3a85-4ee4-9a10-01eeded8c31c/photo/$value
Headers: FeatureFlag : 00000043 Cache-Control : no-store, no-cache User-Agent : Mozilla/5.0,(Windows NT 10.0; Microsoft Windows 10.0.14393; en-US),PowerShell/5.1.14393.6343 Accept-Encoding : gzip SdkVersion : graph-powershell/2.19.0 client-request-id : fd01ac23-ecae-42c5-8ce4-0164b71f32de
Body:
����jExif II*
� � � � � (
1 2 ! i� 8 %� 0 D NIKON CORPORATION NIKON D7500 ��
' ��
' Adobe Photoshop 25.9 (Windows) 2024:06:12
08:27:49 - �� Z �� b "� '� d 0� 2� d � 0231� j � ~
� � � � � � � � � �
� � � �
� � � � � � �
� � �� , � �� 59 �� 59 �� 59 � 0100� � � � � �
� � � � � � � � H � �
� �
� �
� ?
2024:06:04 08:10:18 2024:06:04 08:10:18 -04:00 -04:00 -04:00 �F @B Q @B -
h
ASCII R98 0100
� ( � � H H ���� Adobe_CM �� Adobe d� ��
��
� �" ��
��?
3 !1AQa"q�2���B#$R�b34r��C%�S���cs5���&D�TdE£t6�U�e���u��F'��������������Vfv��������7GWg
w�������� 5 !1AQaq"2����B#�R��3$b�r��CScs4�%���&5��D�T�dEU6te����u��F��������������Vfv����
����'7GWgw������� ? �k�
�b��~G�J�2r�r^mɺ��:o��{��{��i,|��?�ϱ�֏�s�RP�*~��v<[� V�c�@q��>��
3�z���kckffC�����?K��.������� W�}��� Z���k��;�W��)���CNRk��5��=��
��2JQ�Q%K�~I ����2?"(btL�Ħ!����M�2y���☔ܤ�)�T���-�)Md���|W����W����8�{�Z?�O�ǫ�S�� �.��T���;-�&)m�е��p��N
��b�oT��"b;Jpf��|>�T\��$��D1c��������w �ӏ/$�r<��'2�uiD��n�� ��c� "��������et�ݸ����
�c�~�9;x�s���}]�/�8QK,e�,qv��{�� A$>��E�G!���ZA�5�d~>�"�� G� a�v��%"���� ���� �9
읭��j�
!p�m��S���c5.DV/�L�Ӿ�]c[e������Ԕ�� Y����S�q�}��ݴ��������-���4���\O��j?N,p�]V#u��h���F�F�@�E���8�#�:�1�1^ͮ� �� h"V�X����H��pF ��&8p���»ѽ�Iwz�^;\���^��q��ͬ.�k���Vpe2�&�1�F�_R�;�j~ݻ���G ��#D�O���N���u'��X�W$��W�����;����'�?���SkI)��@��HJ]��4�������ؒ����{.q� �]@�� ��1\75��W5�c�x�LI��M
�c��s�0����N�o�Lz��-e@n���
'?�R�-���*�Q"�m.��5�\�Y$Oܣ�@�G�,dOP|�w�!��
]W�Y.���n-s��V��s?�vĢ�O��HHn�3��̀9����v�������@�Y#�kL|���˨�VU;�{mc�������X�|S]�>�jG�$#BZuVAp�^����� ���wz�D
�c@�R�p��>��>��� ����b��r���v�hN֩ �R����S������
���ݭRO�j(Z�Hȍ��PVc
+�� ��o���=����1�ȧ�C�1���ձ���?�U�Ҳ���Ǚ��[�o��'�H�+����t�M��A�o��������V��1��8w��#?1��X��1�"ܻgn����>b��
�s�8λ�ƪ����9���kev}ѻ�d7���_N��$�t��q�9�ջ{N�W8���^�s���R��c�2}k��o��$�� ̓,��h ]��ns�-ic���~��~�Vs+�~5Y
U���9�c���Ӎ���ҿ�.h��9��1"=�����V��{�.}2u�����$l� F��O�׆�.k}'\K��iy������v�;h$���52N�˭���):�,�q��qqA���?v
�ӿ��� V����k�S^?G��a�4��'K
�a$ �J��4W�I�'�sr�Fr�6X%<"WWtV�"�i�F1Ol�� ��뚤��J3ZB�hF���B+/���잃]������LJP��R��E��m
o��6ʬie��5�p��=��5�M"�.��dg�q\�\O&u]oN��X?�*���������������s����a�k�w�*s�;�
�\1<|�l�#F� ��ػ]C.��+��x<����EVL��;��ǩ�z/Z�;������>{�{0�r��i
��5{L���^�o�Ozdbj�s$�u��6�K��O�����c��Z�X���Q�K~K"ڱ-��:�� m���������釫Y��e}����}��Ս�7�Hƺ��kb<õ��I>=�f8�
��nk����:���vߣ���bװ�L�}#��n+rniq��ư�X?GN�~w����!2@Y�$q�M�ѻ��}��� ���>㹿�Q�h��(�S�#�##�D��k$�,d���B� 6X r����(.?z ���\p��8 [>E�s2�Z��j�Q������<.Ȩ�[e �[���� ���y����n�>!z�^��:P����kY I%�s��r��ָv�P �dէ 2ӧc�,>�I ������ �K�m۠�,sZ����Ao���6F�$��g��+2��N�� 7�)���6��P�oO��K��X?2�� 8���e_��mlcZ�7kX��mc�\��Q_P�<��Üy%�3_��-Vp #c�K ��3��?�'kT������ 79%#y��'(L$������0<e�I��2�����)vĪ�k��t>�oQ�~ګѕ��m���z��� ���~�7T�����=<z���?B���?w��g�}d������}�,�u�,� �i�SY?D����S���'ҭĕ[��W�=W��|����u��X��� �iw�ʱ���L�w�����O�~���q�.ø��a� O����� ��ع��W�OP�!��w{2X�K��s�ݽz�8��X-¹���,���t~��~�\�n�3��FLe�ߥ�_;��C� �{��z� [�f����X?�W��@\�Ռ����D>��uo�{7��F� w���D<Ty�y��P��cP![�2|��X h���cW!���b�F�N��&�-~s�����Z�>�� �W~�� �Q ��ױ������'>��q� �K\ ��O��� "��u��[wSб�R4�fP-g�����g�������3//; ,�M?G�j��$1���RpO��y/@� ]I�X�t[��lo�bɨ��V��/k�� ��� ���ԯ�]G �Q�������7R�s�H��u}���ͤQ�!��f�5�����30r0n5Z��%����;�+��}T�R�R���j�=F;��%�S2h�-��a�LG�=�� ��e�'��M�9�4:��l��< � �� >��?�,;��~��ٖ�ƭ���=�ߥl����R���5-�䄇"��R���p���1Cd�Yf�K��HꙘ����2o�����[-��������ƍ���C�
g��?�~�j� 5[-^M��k21�}��qc��[��E歱��Z�6[�G�<��� ���㨐|�c'�-l�}���M�7!�
˲��+&��~��+��m-���F3v��f?�o����Fڱ
�Z�SafauV���̜w�;���w�Wcg�]��RsTk����$?���z�W�U���U�pe�X譧��q����v�!8��Ց��$�C�65�/0��s�nݼ�)<�1Ţ\��3 ���{�� ���LT�b��S� �X��G�Ǻo鏆ދf�5� ��ף��ֻ1��1�^)�/�~��ō�c��~͔LG�in� ?�o�� �p#Y5�6H���c��V8�-���ѭ���6?O��M,mt�H`�'��{���� H�<�z�G3鰗��4��x���u�_R�������������-��c��+���� ��Ƚ�륆 ������������< l]Vd7L�y��� �r�X�����������[Ύuq�uO��� Ʊ��?�:��W��e|}��i͘�qe���� �\0N�(��H%��,�.�������d��5I�c����Ϯ�z)�~�u��Ϭ�{����sO�{ 1�om���W� ůU-�����xJ�m����NPhotoshop 3.0 8BIM ,Z 720240604< 081018-04008BIM% ��O��?�5y��7n8BIM: print Output PstSbool Inteenum Inte Clrm printSixteenBitbool printerNameTEXT O K I C 9 3 1 ( P C L 6 ) printProofSetupObjc P r o o f S e t u p proofSetup Bltnenum builtinProof proofCMYK 8BIM; - printOutputOptions Cptnbool Clbrbool RgsMbool CrnCbool CntCb ool Lblsbool Ngtvbool EmlDbool Intrbool BckgObjc RGBC Rd doub@o� Grn doub@o� Bl doub@o� BrdTUntF#Rlt Bld UntF#Rlt RsltUntF#Pxl@R vectorDatabool PgPsenum PgPs PgPC LeftUntF#Rlt Top UntF#Rlt Scl cropRectRightlong WhenPrintingbool cropRectBottomlong cropRectLeftlong cropRectToplong 8BIM� H H 8BIM& ?� 8BIM� x8BIM 8BIM� 8BIM' 8BIM� H /ff lff /ff ��� 2 Z 5 - 8BIM� p ����������� ������������ ����������������������� ����������������������� ����������������������� 8BIM @ @ 8BIMD @ @ 8BIM 8BIM ] � � T o n g _ G a r y _ 6 - 2 4 _ 4 4 6 3 0 � � null boundsObjc Rct1 Top long Leftlong Btomlong � Rghtlong � slicesVlLs Objc slice sliceIDlong autoGenerated Typeenum m ESliceOrigin ESliceType Img boundsObjc Rct1 Top long Leftlong Btomlong � Rghtlong � urlTEXT nullTEXT MsgeTEXT altTag TEXT cellTextIsHTMLbool cellTextTEXT horzAlignenum ESliceHorzAlign default vertAlignenum ESliceVertAlign default bgColorTypeenum ESliceBGColorType None topOutsetlong leftOutsetlo����jExif II* � � � � � ( 1 2 ! i� 8 %� 0 D NIKON CORPORATION NIKON D7500 �� ' �� ' Adobe Photoshop 25.9 (Windows) 2024:06:12 08:27:49 - �� Z �� b "� '� d 0� 2� d � 0231� j � ~ � � � � � � � � � � � � � � � � � � � � � � � �� , � �� 59 �� 59 �� 59 � 0100� � � � � � � � � � � � � � H � � � � � � � ? 2024:06:04 08:10:18 2024:06:04 08:10:18 -04:00 -04:00 -04:00 �F @B Q @B - h ASCII R98 0100 � ( � � H H ���� Adobe_CM �� Adobe d� �� �� � �" �� ��? 3 !1AQa"q�2���B#$R�b34r��C%�S���cs5���&D�TdE£t6�U�e���u��F'��������������Vfv��������7GWg w�������� 5 !1AQaq"2����B#�R��3$b�r��CScs4�%���&5��D�T�dEU6te����u��F��������������Vfv���� ����'7GWgw������� ? �k� �b��~G�J�2r�r^mɺ��:o��{��{��_i,|��?�ϱ�֏�s�RP�*~��v<[� V�c�@q��>�� 3�z���kckffC�����?K��.������� W�}��� Z���k��;�W��)���CNRk��5��=�� ��2JQ�Q%K�~I ����2?"(btL�Ħ!����M�2y���☔ܤ�)�T���-�)Md���|W����W����8�{�Z?�O�ǫ�S�� �.��T���;-�&)m�е��p��N ��b�oT��"b;Jpf��|>�T\��$��D1c��������w �_ӏ/$�r<��'2�uiD��n�� ��c� "��������et�ݸ���� �c�~�9;x�s���}]�/�8QK,e�,qv��{�� A$>��E�G!���ZA�5�d~>�"�� G� a�v��%"В���� ���� �9 읭��j� !p�m��S���c5.DV/�L�Ӿ�]c[e������Ԕ�� DATA TRUNCATED DUE TO SIZE
DEBUG: [CmdletException]: Received exception with message 'HttpRequestException - An error occurred while sending the request. : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RedirectHandler.<SendAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Kiota.Http.HttpClientLibrary.Middleware.RetryHandler.<SendAsync>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Kiota.Http.HttpClientLibrary.Middleware.CompressionHandler.<SendAsync>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.PowerShell.Authentication.Handlers.AuthenticationHandler.<SendAsync>d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.PowerShell.Users.<UserSetPhotoContent_Call>d__441.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Graph.PowerShell.Users.<UserSetPhotoContent_Call>d__441.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.PowerShell.Users.<UserSetPhotoContent>d__439.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.PowerShell.Cmdlets.SetMgUserPhotoContent_Set.<ProcessRecordAsync>d__69.MoveNext()'
Configuration
-OS: Windows Server 2016 x64
Name Value
PSVersion 5.1.14393.6343 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.14393.6343 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
Other information
No response
Actually the try block in the script is not working as expected and is executing the next command before going to the catch block. When running the script dot source the try block works correctly. But still don't understand why the Set-MgUserPhotoContent is erroring.
Hi @rleist apologies for the super late response on this issue. PowerShell cmdlets execute sequentially by default and by wrapping each step in a try-catch block, we ensure the next step only runs if the previous one succeeds. In a summary here is the implementation of the above.
# Define variables
$user = "[email protected]"
$file = Get-Item .\tempphoto.jpg
# Update the user's photo
try {
Set-MgUserPhotoContent -UserId $user -InFile $file.FullName -ErrorAction Stop
Write-Host "Photo successfully uploaded for user $user."
} catch {
Write-Error "Failed to upload photo for user $user: $_"
exit 1
}
# Remove the photo file after ensuring the previous task is completed
try {
Remove-Item $file.FullName -Force -ErrorAction Stop
Write-Host "Temporary file successfully deleted: $file.FullName."
} catch {
Write-Error "Failed to delete temporary file: $file.FullName. Error: $_"
}
Kindly also ensure that you upgrade to the latest SDK version (2.25.0) and open a new issue targeting that version