Невозможно удалить пустую папку (доступ к облачному файлу запрещен)

#powershell

Вопрос:

В сценарии PS, рекурсивно удаляющем пустые папки с помощью Remove-Item, я получаю исключение «Доступ к облачному файлу запрещен» для некоторых пустых папок.

Ответ №1:

Даже если бы у меня не был установлен OneDrive, решение $dir.Удалить($true) из этой статьи https://evotec.xyz/remove-item-access-to-the-cloud-file-is-denied-while-deleting-files-from-onedrive/ работал

     $directoriesToDelete = Get-ChildItem -Path $pathToPictures -Recurse -Directory
foreach ($directoryToDelete in $directoriesToDelete) {
    $pathToDirectory = $directoryToDelete.FullName
    if ((Get-ChildItem $pathToDirectory | Measure-Object).Count -eq 0) {
        try {
            Remove-Item $pathToDirectory -Force
            Add-LogEntry -PathToLog $logFile -LogEntry "$pathToDirectory deleted"
        } catch {
            if ($_.Exception.Message -eq 'Access to the cloud file is denied') {
                $dir = Get-Item -LiteralPath $pathToDirectory
                try {
                    $dir.Delete($true)
                } catch {
                    Add-LogEntry -PathToLog $logFile -LogEntry "Error deleting '$pathToDirectory' (using dir.Delete): $_"
                }
            } else {
                Add-LogEntry -PathToLog $logFile -LogEntry "Error deleting '$pathToDirectory': $_"
            }
        }
    }
}