我想删除网络共享上的配置文件文件夹时遇到问题。某些无法删除的文件夹似乎存在问题。
这是代码,它适用于大多数文件夹:
Import-Module -Name 'C:\Users\mkz\Documents\powershell\modules\AlphaFS.2.1.3.0\Lib\Net452\AlphaFS.dll'
$folder = "\\filsrvm\MDrev\folderName"
#Function to take ownership and grant permissions
function takeOwnership($path)
{
if(Test-Path $path)
{
$server = ($path).Split("\\",5)[2]+".adm.domain.dk"
takeown.exe /S $server /F $path /r /d Y
icacls $path /grant 'GroupAdm_GL:(CI)(OI)F' /t /c /q
icacls "$path\*" /reset /t /c /q
}
}
#Function to delete folders
function deleteFolder($path)
{
if(Test-Path $path)
{
[Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $true, $true)
}
}
takeOwnership($folder)
deleteFolder($folder)
我收到以下错误:
<red>Exception calling "Delete" with "3" argument(s): "(145) The directory is not empty: [\\?\UNC\filsrvm\MDrev\folderName\Ctx]"
At C:\Users\mkz\Documents\powershell\singleDelete.ps1:24 char:9
+ [Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $true ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DirectoryNotEmptyException</red>
我曾尝试使用Remove-Item $path -force -Recurse
而不是“Alphaleonis”,但它会导致同样的错误。