简体   繁体   中英

Get the latest checkin files from a tfs folder using powershell

Hi i am actually new to powershell. I am trying to do ETL database deployment, so i need all the files with in the tfs folder after a given time. I established the connection with the tfs but i am able to download the files but if a file has two check-ins i am getting the file with the previous checkin and not the latest

My code:

$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]);
            }
        }                                                                                                                
    }
}

This is caused by the object order in $chArray. The changeset in $chArray is ordered from new to old. When you download the file, it is downloading the new file first and then download the older file.

For example, there are two changesets for one file: 111 and 112, with the code Write-Output "ChangesetArray is: $chArray" in your script, you should see the output like: ChangesetArray is: 112 111 . When it downloads the file, the file with 112 version is downloaded first and then 111 version which overwrite the latest version.

You can sort the array in $chArray to fix this issue:

Write-Output "ChangesetArray is: $chArray"

$sortcsarray = $chArray | Sort-Object

Write-Output "ChangesetArray is: $sortcsarray"

foreach ($ch in $sortcsarray) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM