pabrams pabrams - 3 months ago 38
PowerShell Question

Get work item store from Tfs using powershell

How do I get a WorkItemStore from TFS using powershell?

I've tried the following:

function get-tfs {

param(
[string] $ServerName = "http://MyServer:8080/tfs"
)

begin{}

process
{
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
return $tfs
}
end{}
}

[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.WorkItemTracking.Client.dll")


The above code executes fine and I have a value for $tfs.

Then I do this:

$wis = $tfs.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])


But $wis is null. Same thing if I do this instead:

$wis = $tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])


Also, if I do this, powershell says it cannot find assembly 'Microsoft.TeamFoundation.WorkItemTracking.Client' even though it just found it and loaded it a second ago:
Add-Type -AssemblyName Microsoft.TeamFoundation.WorkItemTracking.Client

I don't understand why it found the assembly then suddenly can't find it anymore.

What am I doing wrong?

Answer

Something like this works for me:

function get-tfs
{
    param([string] $ServerName = "http://myserver:8080/tfs")

    $binpath   = "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0"
    Add-Type -path "$binpath\Microsoft.TeamFoundation.Client.dll"
    Add-Type -Path "$binpath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"

    $creds = New-Object Microsoft.TeamFoundation.Client.UICredentialsProvider
    $teamProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $ServerName,$creds

    $ws = $teamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
    return $ws
}