我需要将选定的文件夹从TFS Source控件下载到本地文件夹。我可以使用以下脚本执行该操作:
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对下载文件有什么作用。
谢谢您的帮助。
更新*** 我可以使用工作区路由进行下载,但是如果它仅适用于更新,则没有尝试。 但是,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()
答案 0 :(得分:0)
您需要创建一个工作区来获取所有文件。当执行Get
时,您会看到所有文件都已下载到工作区中。如果工作空间中的文件是最新的,则不会下载任何文件。一旦TFS中有新的更新文件,执行Get
之后,将仅替换更新的文件。这是有关如何创建workpacce的C#代码段:
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080/tfs/defaultCollection"));
var versioncontrols = tfs.GetService<VersionControlServer>();
var workspace = versioncontrols.CreateWorkspace("workspacename", "workspaceowner");
String ServerFolder = @"$/xxxx/xxxx";
String LocalFolder = @"E:\test";
WorkingFolder workfolder = new WorkingFolder(ServerFolder, LocalFolder);
workspace.CreateMapping(workfolder);
workspace.Get();
答案 1 :(得分:0)
我能够使用下面的代码示例使用工作区选项获取代码。
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()