Search code examples
powershelltfstfs-power-tools

Querying for multiple work items using TFPT PowerShell


I'm trying to query TFS to get the change sets that are linked to a set of work items using PowerShell. So far I have been able to do that for a single work item using the following command:

tfpt workitem 928850 /collection:<collection url>

However, I would like to be able to pass in multiple work item IDs at the same time. Does anyone know if this is possible for this command?

I tried pulling multiple work items using a query and the TFPT query command, however it doesn't appear that I can pull linked change sets when using this command.


Solution

  • If you've got an on prem instance, and can use PowerShell against it, then you should be all set. You're going to need to load the TeamFoundation snapin, and load up a pair of DLLs that should be found in your Visual Studio installation in the path 'Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'. So, if you have those items you can do this:

    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
    [System.Reflection.Assembly]::LoadFrom((Resolve-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll').Path)
    [System.Reflection.Assembly]::LoadFrom((Resolve-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.WorkItemTracking.Client.dll').Path)
    $TFS = Get-TfsServer <your TFS URL, something like 'http://MyTFSServer:8080/tfs/MyTeam'>
    $WIS = $TFS.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
    

    Once you have a connection to the WorkItemStore you can post queries against it. That is basically the database that stores all your WorkItems for TFS.

    $QueryText = "SELECT * FROM WorkItems WHERE [ID] = '928850' OR [ID] = '928851'"
    $WorkItems = $WIS.Query($QueryText)
    

    I hope that helps.