Forum Discussion
tetsuya785
Mar 01, 2023Copper Contributor
Occasionally an error occurs in Disconnect-ExchangeOnline
Hi Community, When running Disconnect-ExchangeOnline, the following error may occur. If anyone knows how to work around this error, please let me know. Error Description: PS C:\> Disconne...
- 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...)
Varun_Ghildiyal
Mar 07, 2023Iron Contributor
Based on the error message, it appears that the Disconnect-ExchangeOnline command is unable to remove a directory because it is not empty. This could be caused by a file or folder being locked or in use by another process.
One workaround to try is to manually delete the directory in question before running the Disconnect-ExchangeOnline command. You can navigate to the directory using File Explorer or use the Remove-Item command in PowerShell to delete the directory.
Another option is to close any programs or processes that may be using files in the directory before running the Disconnect-ExchangeOnline command. You can use the Task Manager to identify any programs or processes that may be using files in the directory and end them.
If the issue persists, you may want to try running the Disconnect-ExchangeOnline command in Safe Mode or with a different user account to rule out any conflicts with other programs or processes running on the system.
It's also worth noting that the ExchangeOnlineManagement module is designed to work with PowerShell version 7 and above, so upgrading to a newer version of PowerShell may help resolve the issue.
- TimGillMar 23, 2023Copper Contributor
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...)- tetsuya785Mar 24, 2023Copper Contributor
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.