首页 文章

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

提问于
浏览
5

我们经常发生大量的构建,并且需要大量的资源 . 因此,我们希望在每次构建后清除构建源和暂存目录 . 我在本地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存在问题 . 最初它有时是有效的 . 现在它永远不会起作用 .

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

With -Recurse and -Force

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

等于:

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

No -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\3\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

No -Force

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

返回大量错误,1表示权限被拒绝,以及其他由于之前拒绝权限而无法删除的错误:

Remove-Item : Cannot remove item
E:\GeneralAgent1\_work\3\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\3\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

No -Recurse or -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\3\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 的参数进行播放并得到类似的结果 .

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

有什么帮助吗?

2 回答

  • 8

    如果你自下而上,你应该可以删除所有内容 . 在删除项目之前,按降序将 Get-ChildItem 的结果按降序排序:

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

    不确定这是你的修复,但我做的有点不同

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

相关问题