Wednesday, May 05, 2010

Use powershell to quickly backup all TFS Work Item Types

Hey all,
Just a quickie to easily backup all your current work items from all projects in Team Foundation Server 2008 SP1.  I wanted to do this ahead of a big migration and I am adverse to manual labor.

It starts w/ a function i found a couple years ago.  I don’t recall where or I would give credit here.  It is very useful for any tfs powershell manipulation.

Edit below to enter your $TFSHost and update your path if necessary.  I recommend running from a blank directory.  It will create a directory for each project and export each WIT for that project to the respective directories.

Update: I added another function from my library test-win32 and had that manage the default paths for x64 and x86 archs.


Good luck
$tfshost = "thshost"


 
function Test-Win32() {
    return [IntPtr]::size -eq 4
}

if (test-win32) {
      $tfstoolspath = "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE"
} else {
      $tfstoolspath = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE"
}

function get-tfs {
      #10-08 - cornasdf - i stole this from the web but this is a great way to connect to TFS
      # in your code you use: $tfs =  get-tfs $TFSHost
      #some quick examples from resolvedticketsmail.ps1:
      #get select from Stored query
      #$query = $tfs.WIT.Projects[$ProjectHoldingQuery].StoredQueries | where{$_.Name -eq $QueryViewName}

      #get results of specified query
      #$oldTickets = $tfs.WIT.Query($query.QueryText)



      param(
            [string] $serverName = $(throw 'serverName is required')
      )

      begin
      {
            # load the required dll
            [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

            $propertiesToAdd = (
                  ('VCS', 'Microsoft.TeamFoundation.VersionControl.Client', 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'),
                  ('WIT', 'Microsoft.TeamFoundation.WorkItemTracking.Client', 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore'),
                  ('CSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'),
                  ('GSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService')
            )
      }

      process
      {
            # fetch the TFS instance, but add some useful properties to make life easier
            # Make sure to "promote" it to a psobject now to make later modification easier
            [psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
            foreach ($entry in $propertiesToAdd) {
                  $scriptBlock = '
                        [System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null
                        $this.GetService([{1}])
                  ' -f $entry[1],$entry[2]
                  $tfs | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock)
            }
            return $tfs
      }
}    

$tfs = get-tfs $tfshost

$projects = $tfs.wit.Projects


foreach ($proj in $projects) {
      if ( -not (Test-Path -path .\$($proj.Name))) {
            New-Item .\$($proj.Name) -type directory
      }
     
      foreach ($wit in $proj.WorkItemTypes) {
            "Exporting $($proj.Name) : $($wit.Name) -> .\$($proj.Name)\$($wit.Name).xml"
            & "$tfstoolspath\witexport.exe"  /p "$($proj.Name)" /n "$($wit.Name)" /f ".\$($proj.Name)\$($wit.Name).xml" /t $tfshost
           
      }
}

4 comments:

  1. I get the following error:

    Exception calling "GetServer" with "1" argument(s): "TF249051: No URL can be found that corresponds to the following server name: xyz. Verify
    that the server name is correct."

    server xyz exists and I can ping it

    ReplyDelete
  2. if you don't have an alias already in the registry for that team server (you would only have this if you have team explorer or similar already on the machine) you will need to qualify your server w/ http://server:8080 or what ever protocol and port your tfs server is listening on.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

analytics