Forum Discussion
Occasionally an error occurs in Disconnect-ExchangeOnline
- Mar 23, 2023
TL;DR - Add Start-Sleep 60 to your script right before your Disconnect-ExchangeOnline command. Adjust the Sleep length down if needed/preferred. 15 seconds works fine for me.
A couple things. First, the ExchangeOnlineManagement module Microsoft page says that it is built to function on all PowerShell versions 5.1 and above.
Source: https://learn.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#windows
Second, I am getting this as well, and am getting it pretty consistently right after upgrading the ExchangeOnlineManagement module on a Scheduled Task server to v3.1.0. I assume it's a bug and will get ironed out eventually. However, I keep getting alerts because one of my scripts is throwing this error.
Based on Varun's suggestion above, I manually deleted the temp folder that was identified in the error as "unable to be deleted". I was able to manually do this in the file system every time without fail. So I'm thinking it's the PowerShell process itself that needs a little more time hanging on to the files in the temp folder before they can be deleted.
Not proud of it, but adding a Start-Sleep 15 just before the Disconnect-ExchangeOnline -Confirm:$False line is now preventing this error from occurring on all subsequent runs. Until MS figures it out, this seems like a solid temporary fix. Of note, of the many Exchange scripts running on this server, this script is the shortest, lasting a minute or less. It's possible a certain amount of time needs to pass between connect and disconnect or the files in the temp folder aren't completely downloaded (or something...)
TL;DR - Add Start-Sleep 60 to your script right before your Disconnect-ExchangeOnline command. Adjust the Sleep length down if needed/preferred. 15 seconds works fine for me.
A couple things. First, the ExchangeOnlineManagement module Microsoft page says that it is built to function on all PowerShell versions 5.1 and above.
Source: https://learn.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#windows
Second, I am getting this as well, and am getting it pretty consistently right after upgrading the ExchangeOnlineManagement module on a Scheduled Task server to v3.1.0. I assume it's a bug and will get ironed out eventually. However, I keep getting alerts because one of my scripts is throwing this error.
Based on Varun's suggestion above, I manually deleted the temp folder that was identified in the error as "unable to be deleted". I was able to manually do this in the file system every time without fail. So I'm thinking it's the PowerShell process itself that needs a little more time hanging on to the files in the temp folder before they can be deleted.
Not proud of it, but adding a Start-Sleep 15 just before the Disconnect-ExchangeOnline -Confirm:$False line is now preventing this error from occurring on all subsequent runs. Until MS figures it out, this seems like a solid temporary fix. Of note, of the many Exchange scripts running on this server, this script is the shortest, lasting a minute or less. It's possible a certain amount of time needs to pass between connect and disconnect or the files in the temp folder aren't completely downloaded (or something...)
Thanks both of you for your response.
As TimGill suggested, we will add a 15 second sleep and see what happens.
At any rate, I am relieved that it does not seem to be an event that occurs only in my environment.