Issue with Set-UserPhoto command

Welcome Forums General PowerShell Q&A Issue with Set-UserPhoto command

Viewing 1 reply thread
  • Author
    Posts
    • #240338
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      Hi,

      I have a PowerShell script that runs on a schedule to set all of our 150 users profile photos within Office365/Microsoft365.

      Here is the code I am using….

      $username = “<username>”
      $password = “<password>” | ConvertTo-SecureString -asPlainText -Force
      $Usercredential = New-Object System.Management.Automation.PSCredential($username,$password)
      $ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Usercredential -Authentication Basic -AllowRedirection
      Import-PSSession $ExSession -AllowClobber

      Set-UserPhoto -Identity “[email protected]” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user001.jpg”)) -Confirm:$falsecls
      Set-UserPhoto -Identity “[email protected]” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user002.jpg”)) -Confirm:$falsecls
      to…
      Set-UserPhoto -Identity “[email protected]” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user150.jpg”)) -Confirm:$falsecls

      The script used to work fine, however recently after about every 10 photos that are changed a prompt appears asking for the password to be re-entered:

      Creating a new session for implicit remoting of “Set-UserPhoto” command…

      I haven’t changed anything recently so I’m wondering why this is now prompting me?

      It’s really annoying as I cannot now run this as a scheduled task.

      Can anyone recommend a fix?

      Best wishes,

      Matt

    • #241211
      Participant
      Topics: 0
      Replies: 38
      Points: 379
      Helping Hand
      Rank: Contributor

      Hello Matt,

      Try to play with -SessionOption parameter. Create PSsession object using New-PSSessionOption cmdlet and tweak Idletimeout value.

      Reference: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssessionoption?view=powershell-7

      Hope that helps.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.