Forum Discussion
O365 Script - credentials prompt
- Apr 13, 2017
That's just a side effect of broken session to Exchange Online and PowerShell's attempt to recconect to it. The best thing you can do is to optimize your script to run as efficiently as possible, using server-side filtering and Invoke-Command in order to reduce the execution time. Apart from that, you can include a check for the session state before running each cmdlet/iteration. An example of such script can be found here: https://gallery.technet.microsoft.com/scriptcenter/Start-RobustCloudCommand-69fb349e
There are some other recommenadations you can find here: https://blogs.technet.microsoft.com/exchange/2015/11/02/running-powershell-cmdlets-for-large-numbers-of-users-in-office-365/
Hello,
I'm using this one and works great for me :
Set-ExecutionPolicy RemoteSigned $UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session
- Joe McNicholasMay 03, 2017Brass Contributor
Thanks Gorazd - that's about the same as what I use. My problem is that after a few hours the O365 session goes into a broken state and the script stops.
I've added some logic to remove the O365 session and re-connect to O365 every 15 mins to avoid this problem, and it looks promising. I have a script that has now been running for about 18 hours without stopping....
- bala_1992Jul 29, 2019Copper Contributor
Joe McNicholas Can I get the script that you modified and working perfectly fine ?