如何避免 Remove-Item PowerShell 错误“进程无法访问文件”?

Mic*_*eim 7 powershell deployment

我们正在使用TfsDeployer和 PowerShell 脚本来删除Remove-Item 在部署新版本之前使用的文件夹。有时 PS 脚本会失败并显示错误:

删除项目:无法删除项目 Services\bin:该进程无法访问文件 Services\bin',因为它正被另一个进程使用 Get-ChildItem -Path $Destination -Recurse | Remove-Item <<<< -force -recurse + CategoryInfo : WriteError: (C:\Program File..\Services\bin:DirectoryInfo) [Remove-Item], IOExceptionfullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

我试图按照答案(来自:在 PowerShell 中强制删除文件和目录有时会失败,但并非总是如此)通过管道Get-ChildItem -Recurse进入Remove-Item.

Get-ChildItem * -Include *.csv -Recurse | Remove-Item
Run Code Online (Sandbox Code Playgroud)

但是错误仍然会定期发生。我们正在使用解锁器手动终止锁定应用程序,(通常是 w3wp),但我更喜欢找到自动化的解决方案。

另一个(不理想的)选项是抑制 powershell-errors

 Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议。

Dom*_*nin 0

既然它是一个锁定文件的 Web 应用程序,为什么iisreset /stop不在删除该项目之前执行一个操作,然后iisreset /start再执行操作呢?