我们经常发生大量的构建,并且需要大量的资源。因此,我们希望在每次构建后清除构建源和暂存目录。我在本地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
否-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
的参数进行播放,并得到类似的结果。
构建代理帐户具有对根目录的完全权限。
请帮忙吗?
答案 0 :(得分:10)
如果你自下而上,你应该可以删除所有内容。在删除项目之前,按降序将Get-ChildItem
的结果按其全名排序:
Get-ChildItem -Path $directory -Force -Recurse |
Sort-Object -Property FullName -Descending |
Remove-Item -Recurse -Force
答案 1 :(得分:0)
不确定这是你的修复,但我的确有点不同
Get-ChildItem $FolderPath | ForEach-Object ($_){
Remove-Item $_.FullName -Recurse -Force
}