簡體   English   中英

如何使用Powershell從本地目錄中的TFS僅下載更新的文件

[英]How to download only updated files from TFS in local directory using Powershell

我需要將選定的文件夾從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與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()

您需要創建一個工作區來獲取所有文件。 當執行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();

我可以使用下面的代碼示例使用工作區選項獲取代碼。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM