[英]Get the latest checkin files from a tfs folder using powershell
嗨,我實際上是Powershell新手。 我正在嘗試進行ETL數據庫部署,因此在給定的時間后,我需要tfs文件夾中的所有文件。 我建立了與tfs的連接,但是我可以下載文件,但是如果文件有兩個簽入,則我使用先前簽入而不是最新簽入來獲取文件
我的代碼:
$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]);
}
}
}
}
這是由於$ chArray中的對象順序引起的。 $ chArray中的變更集按從新到舊的順序排列。 當您下載文件時,它將先下載新文件,然后再下載舊文件。
例如,一個文件有兩個變更集:111和112,腳本中的代碼為Write-Output "ChangesetArray is: $chArray"
,您應該看到如下輸出: ChangesetArray is: 112 111
。 下載文件時,首先下載112版本的文件,然后下載111版本的文件,該版本將覆蓋最新版本。
您可以在$ chArray中對數組進行排序以解決此問題:
Write-Output "ChangesetArray is: $chArray"
$sortcsarray = $chArray | Sort-Object
Write-Output "ChangesetArray is: $sortcsarray"
foreach ($ch in $sortcsarray)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.