运行 在 TFS 构建结束时删除项目错误

Remove-Item errors when run at end of TFS build

我们经常使用大量源进行大量构建。因此,我们希望在每次构建后清除构建源和暂存目录。我在本地 TFS 2015 Update 1 中使用 vNext 构建。我创建了一个 PowerShell 脚本作为执行删除的最终任务:

[CmdletBinding()]
param()

begin {
    function Delete-Directory {
        param([string]$directory)

        Write-Output "Attempting to delete '$($directory)'"

        if (Test-Path $directory -pathType container) {
            Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Recurse -Force
            Write-Output "Successfully deleted the directory: '$($directory)'"
        } else {
            Write-Output "Failed to delete '$($directory)' as it does not exist"
        }
    }
}

process {
    Delete-Directory $env:BUILD_SOURCESDIRECTORY
    Delete-Directory $env:BUILD_STAGINGDIRECTORY
}
end{}

最初,我没有使用 Get-ChildItem .... | Remove-Item,而是使用 Remove-Item *path* -Recurse -Force,但显然 recurse parameter of Remove-Item 有问题。最初它有时会工作。现在它永远不会工作。

我尝试了许多不同的变体,这里是一些结果:

-Recurse-Force

Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Recurse -Force

等于:

Get-ChildItem : Access to the path 'E:\GeneralAgent1\_work\s\TfsBuild' is
denied.
At E:\GeneralAgent1\_work\s\TfsBuild\Scripts\DeleteSources.ps1:11 char:5
+ Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Recurse -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (E:\GeneralAgent1\_work\s\TfsBuild:String) [Get-ChildItem], Unauthor
izedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

没有-Recurse

Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Force

等于:

Remove-Item : Windows PowerShell is in NonInteractive mode. Read and Prompt
functionality is not available.
At E:\GeneralAgent1\_work\s\TfsBuild\Scripts\DeleteSources.ps1:11 char:54
+ Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Force
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand

没有-Force

Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Recurse

Returns 很多错误,1 个权限被拒绝,其他错误由于之前权限被拒绝而无法删除:

Remove-Item : Cannot remove item
E:\GeneralAgent1\_work\s\Proxies\Development\Isd.Proxies.BO_16.01.1\Avalara\StyleCop.Cache:
You do not have sufficient access rights to perform this operation.
At E:\GeneralAgent1\_work\s\TfsBuild\Scripts\DeleteSources.ps1:11 char:54
+ Get-ChildItem -Path $directory -Force -Recurse | Remove-Item -Recurse
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (StyleCop.Cache:FileInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
......
Lots

-Recurse-Force

Get-ChildItem -Path $directory -Force -Recurse | Remove-Item

等于:

Remove-Item : Windows PowerShell is in NonInteractive mode. Read and Prompt
functionality is not available.
At E:\GeneralAgent1\_work\s\TfsBuild\Scripts\DeleteSources.ps1:11 char:54
+ Get-ChildItem -Path $directory -Force -Recurse | Remove-Item
+ ~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand

我也尝试过其他组合并使用 Get-ChildItem 的参数进行操作并得到类似的结果。

构建代理帐户对根目录具有完全权限。

有什么帮助吗?

不确定这是你的解决方法,但我做的有点不同

Get-ChildItem $FolderPath | ForEach-Object ($_){
    Remove-Item $_.FullName -Recurse -Force
}

如果您从下往上进行,您应该能够删除所有内容。在删除项目之前,按全名降序对 Get-ChildItem 的结果进行排序:

Get-ChildItem -Path $directory -Force -Recurse |
  Sort-Object -Property FullName -Descending |
  Remove-Item -Recurse -Force