Get work item store from Tfs using powershell

2019-05-10 09:53发布

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?

2条回答
成全新的幸福
2楼-- · 2019-05-10 10:31

I was also having issues getting WorkItemStore with Powershell attempts. Needed it to clone a PBI with tasks cloned also. Found this .Net soln. Worked like a charm! Actually had to add a try catch around a History null exception, but after that, bam!

https://github.com/DanielMeixner/WorkItemDeepCopy/

查看更多
何必那么认真
3楼-- · 2019-05-10 10:49

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
}
查看更多
登录 后发表回答