SOLVED

Occasionally an error occurs in Disconnect-ExchangeOnline

Copper Contributor

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:\> Disconnect-ExchangeOnline -Confirm:$false
Remove-Item : Cannot remove item C:\Users\administrator.xxxxx\AppData\Local\Temp\tmpEXO_q2lyvkhx.og5\en-us: The
directory is not empty.
At line:1 char:1
+ Remove-Item -Path "C:\Users\administrator.xxxxx\AppData\Local\T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (en-us:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\Users\administrator.xxxxx\AppData\Local\Temp\tmpEXO_q2lyvkhx.og5: The
directory is not empty.
At line:1 char:1
+ Remove-Item -Path "C:\Users\administrator.xxxxx\AppData\Local\T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\admini...XO_q2lyvkhx.og5:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand


PS C:\> Get-InstalledModule ExchangeOnlineManagement | Format-List Name,Version,InstalledLocation

Name              : ExchangeOnlineManagement
Version           : 3.1.0
InstalledLocation : C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.1.0

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.1884
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.1884
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


The same error occurred when using the recently released ExchangeOnlineManagement 3.2.0-Preview1.

Let me know in case any additional information is required.

Thanks.

3 Replies

@tetsuya785 

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.

best response confirmed by tetsuya785 (Copper Contributor)
Solution

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...

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...)

@TimGill @Varun_Ghildiyal 

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.



1 best response

Accepted Solutions
best response confirmed by tetsuya785 (Copper Contributor)
Solution

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...

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...)

View solution in original post