Issue with Set-UserPhoto command

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 “user001@domain.org” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user001.jpg”)) -Confirm:$falsecls
Set-UserPhoto -Identity “user002@domain.org” -PictureData ([System.IO.File]::ReadAllBytes(“C:\photos\user002.jpg”)) -Confirm:$falsecls
to…
Set-UserPhoto -Identity “user150@domain.org” -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

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.