我们经常发生大量的构建,并且需要大量的资源.因此,我们希望在每次构建后清除构建源和暂存目录.我在本地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参数存在问题.最初它有时是有效的.现在它永远不会起作用.
我尝试了很多不同的变体,这里有一些结果:
随着-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
没有 -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
没有 -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
没有-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\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
并得到类似的结果.
构建代理帐户具有对根目录的完全权限.
有什么帮助吗?
如果你自下而上,你应该可以删除所有内容.Get-ChildItem
在删除项目之前,按降序对其全名的结果进行排序:
Get-ChildItem -Path $directory -Force -Recurse | Sort-Object -Property FullName -Descending | Remove-Item -Recurse -Force