【发布时间】:2018-11-28 18:33:00
【问题描述】:
我需要将选定的文件夹从 TFS 源代码控制下载到本地文件夹。我可以使用以下脚本执行此操作:
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
$securePass = ConvertTo-SecureString -AsPlainText -Force -String "mypassword"
$uri = "serverURL"
$cred = New-Object System.Management.Automation.PSCredential("myusername",$securePass)
$tfsServer = Get-TfsServer -Name $uri -Credential $cred
$structureService = $tfsServer.GetService("Microsoft.TeamFoundation.Server.ICommonStructureService")
$versionControlService = $tfsServer.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$pathsToDownload=[System.Collections.ArrayList]@()
$pathsToDownload.add("$/myfirstPath")
$pathsToDownload.add("$/mysecondPath")
$pathsToDownload.add("$/mythirdPath")
$localTFSRoot = "c:\tfs"
$serverRoot = "$/"
foreach ($serverPath in $pathsToDownload) {
write-host "Working with $serverPath"
$items = Get-TfsChildItem -Server $tfsServer -Item $serverPath -Recurse
foreach ($item in $items) {
$destinationPath=$($Item.ServerItem.Replace($serverRoot,$localTFSRoot)).replace("\","/")
write-host "Downloading $destinationPath"
if ($item.ItemType -eq "Folder") {
#create directory if it doesn't already exist
if (-Not (Test-Path $destinationPath -PathType Container -IsValid)) {
New-Item -ItemType Directory -Path $destinationPath -Force
}
} else {
$versionControlService.DownloadFile($item.ServerItem,$destinationPath)
}
}
}
但是,此脚本每次都会下载所有文件。仅当文件发生更改时,我才想下载文件。有没有办法通过powershell操作来执行这样的操作?我不确定 getItem 与 downloadfile 的作用。
感谢您的帮助。
更新*** 我可以使用工作区路由进行下载,但如果它仅适用于更新,我还没有尝试过。 但是,workspace.get() 正在下载而没有任何详细输出。有没有办法让它列出它正在使用的文件,这样用户就不会认为它在下载时被挂起?
if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}
$securePass = ConvertTo-SecureString -AsPlainText -Force -String "mypassword"
$uri = "https://myserver"
$cred = New-Object System.Management.Automation.PSCredential("myusername",$securePass)
$tfsServer = Get-TfsServer -Name $uri -Credential $cred
$structureService = $tfsServer.GetService("Microsoft.TeamFoundation.Server.ICommonStructureService")
$versionControlService = $tfsServer.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
$workSpaceName = $env:USERNAME + "-IMAG"
$localTFSWorkspace="c:\tfswspace"
$serverRoot = "$/myproject"
$pathsToDownload=[System.Collections.ArrayList]@()
$pathsToDownload.add("$/myproject/path1")
$pathsToDownload.add("$/myproject/path2")
$testPath = $($pathsToDownload[0].Replace($serverRoot,$localTFSWorkspace)).replace("/","\")
if (!(Test-Path $localTFSWorkspace -PathType Container)) {
New-Item $localTFSWorkspace -ItemType Directory -Force
}
# Check if workspace already exists
$workspace = $versionControlService.TryGetWorkspace($testPath)
if ($workspace -eq $null) {
#Workspace doesn't exist, we need to create one
$workspace = $versionControlService.CreateWorkspace($workSpaceName,$cred.UserName, 'Workspace for Repository Sync')
}
#create mappings if these don't exist
foreach ($serverPath in $pathsToDownload) {
$localPath = $($serverPath.Replace($serverRoot,$localTFSWorkspace)).replace("/","\")
if (!(Test-Path $localPath -PathType Container)) {
New-Item $localPath -ItemType Directory -Force
}
$workingFolder = $workspace.TryGetWorkingFolderForServerItem($serverPath)
if ($workingFolder -eq $null) {
#create mapping here
$workspace.Map($serverPath,$localPath)
}
}
# Now mappings are done -- get items now
$workspace.get()
【问题讨论】:
-
您使用的是什么版本的 tfs?我觉得你应该在某处包含一个设置为最新的 VersionSpec。
-
是TFS 2013。我的理解是downloadFile总是会得到最新版本。但是,它并不“知道”已经存在本地副本。
-
我已经有一段时间没有使用 TFS API 的东西了,但是你有没有试过下载一个文件,等几分钟,再做一次,然后比较第一个到第二个的时间戳下载?通常,如果 TFS 认为文件已被下载,它会避免将文件下载到工作区。
-
是的,但在我的脚本中,我没有创建或定义工作区。它只是一个本地目录。
-
这可能是您的问题的根源。它不跟踪文件。
标签: powershell tfs