首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用powershell从tfs文件夹获取最新的签入文件

使用powershell从tfs文件夹获取最新的签入文件
EN

Stack Overflow用户
提问于 2016-11-17 16:47:31
回答 1查看 1K关注 0票数 0

大家好,我实际上是powershell的新手。我正在尝试进行ETL数据库部署,因此在给定时间后,我需要tfs文件夹中的所有文件。我建立了与tfs的连接,但我可以下载文件,但如果一个文件有两次签入,我将获得具有先前签入的文件,而不是最新的文件

我的代码:

代码语言:javascript
复制
$TfsUrl = "http://tfs2013-xxx02.ad.xxx.com:8080/tfs/abcd-xxx243"

    # Load TFS assemblies for connecting to the TFS server 
    Add-Type -Path "E:\Microsoft Visual Studio 14.0\Common7\IDE\TestAgent\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "E:\Microsoft Visual Studio 14.0\Common7\IDE\TestAgent\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "E:\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\4qvjm2or.ipa\Microsoft.TeamFoundation.Lab.Client.dll"
    Add-Type -Path "E:\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\4qvjm2or.ipa\Microsoft.TeamFoundation.Lab.Common.dll"
    Add-Type -Path "E:\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\4qvjm2or.ipa\Microsoft.TeamFoundation.VersionControl.Client.dll"

    #Get TFS Instance   
    $Tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TfsUrl)

    # use the account credentials of the process running the script
    try 
    {
        $Tfs.EnsureAuthenticated()
        Write-Output "TFS Connection is successful"
    }
    catch 
    {
        Write-Output "Error trying to connect to tfs server.  Check your tfs permissions and path: $_ " 
        Exit(1)
    }

    #Write-Message $LogFileName "THIS IS INSIDE Connect-ToTFS"
    #Write-Message $LogFileName "TFS IS $Tfs"

$DeploymentFilePath= "$/xxxx/FutureReleases/Database/ReportingETLs"

$TFSInstance    = $Tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
$LatestVersion  = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest
$RecursionType  = [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::Full  


$DateFrom   = "D2016-10-08T01:59"

# Get the From and To date-time in version format to be passed to TFS API
$VersionFrom    = $null 
$VersionFrom    = [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::ParseSingleSpec($DateFrom, "")      

$FileHistory    = @($TFSInstance.QueryHistory($DeploymentFilePath,$LatestVersion,0,$RecursionType,$null,$null,$null,[int32]::MaxValue, $true ,$true, $true))

#Write-Output "Filehistory is: $FileHistory"

#$ChangeSetCount = $FileHistory.Count
#Write-Output "ChangeSetCount is: $ChangeSetCount"


$TFSGetFullPath = "E:\temp\"
$chArray = @()
$checkin =""

foreach ($history in $FileHistory)
{
    foreach ($change in $history.Changes)
    {
        foreach ($item in $change.item)
        {

            if($item.CheckinDate -gt $VersionFrom.Date)
            {
                $chArray += $history.ChangesetId 


            }
        }
    }
}

Write-Output "ChangesetArray is: $chArray"
foreach ($ch in $chArray) 
{
    foreach ($lastdeployedHistory in $FileHistory)       
    {      
        if($lastdeployedHistory.ChangesetId -eq  $ch)
        {
            foreach ($workitem in $lastdeployedHistory.Changes)
            { 
                $workitem.Item.DownloadFile([IO.Path]::GetFullPath($TFSGetFullPath) + $workitem.Item.ServerItem.Split('/')[$workitem.Item.ServerItem.Split('/').Length - 1]);
            }
        }                                                                                                                
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-11-22 10:36:39

这是由$chArray中的对象顺序引起的。$chArray中的变更集按从新到旧的顺序排列。下载文件时,它会先下载新文件,然后再下载旧文件。

例如,一个文件有两个变更集: 111和112,在脚本中使用代码Write-Output "ChangesetArray is: $chArray",您应该会看到类似于:ChangesetArray is: 112 111的输出。下载文件时,先下载112版本的文件,然后下载111版本的文件,并覆盖最新版本。

您可以在$chArray中对数组进行排序以解决此问题:

代码语言:javascript
复制
Write-Output "ChangesetArray is: $chArray"

$sortcsarray = $chArray | Sort-Object

Write-Output "ChangesetArray is: $sortcsarray"

foreach ($ch in $sortcsarray) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40650240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档