Tfs Cmdlet拉太多项目

时间:2015-03-06 21:16:34

标签: powershell tfs

我正在开发一个PowerShell脚本来自动化一些后期构建活动。也就是说,部分版本需要生成一个SQL文件列表,以便DBA可以编译发布脚本。

我的脚本可以正常工作,但它会拉太多文件。当我指定变更集时,它会从其他几个变更集中撤回文件。在我正在测试的团队项目中,它会回退所有 SQL文件(所有这五个文件),但我只希望SQL文件实际上与提供的ChangesetId相关联。

这是我的脚本。任何人都可以告诉我我做错了什么,以及如何让它只从特定变更集中提取文件?

# Loads Windows PowerShell snap-in if not already loaded
if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

# Variables - CHECK EACH TIME
[string] $tfsCollectionPath = "http://validtfsurl:8080/tfs"
[string] $locationToSearch = "$/Sandbox/Database/"
[string] $outputFileDir = "C:\TFSTest\"
[string] $outputFileName = $outputFileDir + "ChangedSQLFiles.txt"
[string] $changeset = "C111"

 if (-not [IO.Directory]::Exists($outputFileDir))
 {
    [IO.Directory]::CreateDirectory($outputFileDir) | Out-Null
 }

[Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $tfs = get-tfsserver $tfsCollectionPath

# Add informational header to file manifest
[string] $outputHeader =
    "TFS Collection: " + $tfsCollectionPath + "`r`n" + 
    "Source Location: " + $locationToSearch + "`r`n" + 
    "Changeset: " + $changeset + "`r`n" +
    "Created: " + (Get-Date).ToString() + "`r`n" +
    "======================================================================" 
$outputHeader | Out-File $outputFileName -Append

$items = Get-TfsItemHistory $locationToSearch -Server $tfs -Version $changeset -Recurse -IncludeItems | 
Select-Object -Expand "Changes" | 
    Where-Object { $_.ChangeType -notlike '*Delete*'} | 
    Where-Object { $_.ChangeType -notlike '*Rename*'} | 
    Where-Object { ($_.ChangeType -like '*Edit*') `
        -or ($_.ChangeType -like '*Add*')} |  
Select-Object -Expand "Item" | 
    Where-Object { $_.ContentLength -gt 0} |  
    Where-Object { $_.ServerItem -like '*.sql' } |
Select-TfsItem |
Sort -Unique Path

foreach($item in $items)
{
    [string] $ItemPath_Local = $item.Path.Trim()
    [int] $ItemPath_Length = $ItemPath_Local.Length - 19

    $ItemPath_Local = $ItemPath_Local.Replace("/", "\")
    $ItemPath_Local = $outputFileDir + "SqlFiles\" + $ItemPath_Local.Substring(19, $ItemPath_Length)

    Get-TfsChildItem $item.Path -Server $tfs -Version $changeset |
    %{$_.DownloadFile(@(join-path $outputFileDir (split-path $_.ServerItem -leaf)))}

    $item.Path.Trim() | Out-File $outputFileName -Append
}   

[string] $outputFooter = "======================================================================" 
$outputFooter | Out-File $outputFileName -Append

1 个答案:

答案 0 :(得分:0)

在研究其他内容时,我发现this MSDN page描述了Merge命令的语法(尽管是旧的TFS版本),我注意到他们正在使用

C1~C1

获取单个变更集。我将脚本声明更改为

# Variables - CHECK EACH TIME
[string] $tfsCollectionPath = "http://validtfsurl:8080/tfs"
[string] $locationToSearch = "$/Sandbox/Database/"
[string] $outputFileDir = "C:\TFSTest\"
[string] $outputFileName = $outputFileDir + "ChangedSQLFiles.txt"
[string] $changeset = "C111"
[string] $changesetRange = "C111~C111"

然后我将Get-TfsItemHistory调用更改为

$items = Get-TfsItemHistory $locationToSearch -Server $tfs -Version $changesetRange -Recurse -IncludeItems | 
Select-Object -Expand "Changes" | 
    Where-Object { $_.ChangeType -notlike '*Delete*'} | 
    Where-Object { $_.ChangeType -notlike '*Rename*'} | 
    Where-Object { ($_.ChangeType -like '*Edit*') `
        -or ($_.ChangeType -like '*Add*')} |  
Select-Object -Expand "Item" | 
    Where-Object { $_.ContentLength -gt 0} |  
    Where-Object { $_.ServerItem -like '*.sql' } |
Select-TfsItem |
Sort -Unique Path

最后,只使用单个变更集来执行下载

Get-TfsChildItem $item.Path -Server $tfs -Version $changeset |
%{$_.DownloadFile(@(join-path $outputFileDir (split-path $_.ServerItem -leaf)))}

这很有效。在我的最终代码中,我将实际使用TFS环境变量来获取变更集,但执行是相同的。