我们有大量的构建,经常有大量的源。因此,我们希望在每次构建后清除构建源和登台目录。我在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\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处于非交互模式。阅读并提示
功能不可用。
在E:\GeneralAgent1\_work\3\s\TfsBuild\Scripts\DeleteSources.ps1:11 char:54
+ Get-ChildItem -Path $ directory -Force -Recurse |除去项目
+ ~~~~~~~~~~~~
+ CategoryInfo:InvalidOperation :( :) [Remove-Item],PSInvalidOperationException
+ FullyQualifiedErrorId:InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand

我还尝试了其他组合,并使用Get-ChildItem的参数来获得相似的结果。

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

有什么帮助吗?

最佳答案

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

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

关于powershell - 在TFS构建结束时运行时出现Remove-Item错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34632351/

10-13 07:52