Go to content Go to navigation Go to search

Brokenwire.NET::Programming

Using the Team Foundation Object Model with PowerShell
· Feb 26, 12:45 PM by Thijs Kroesbergen for Brokenwire.NET

After my first steps in PowerShell I wanted to do something more. I wanted to manipulate the TFS build store from within PowerShell (because I installed TFSDeployer which responds to "Build Quality Changed events" from TFS). So after some Google-ing I found the "get-tfs.ps1" script written by James Manning. His script wraps the Team Foundation Server SDK (which is .NET object model to access TFS) into one nice PowerShell script.

The only thing his script could not do, was manipulate the build store. But with the addition of one line I made this possible. His version also was a "script-as-function" while I'd rather have just a separate function that I could put in my script library. The script now look like this:

function get-tfs ( [string] $serverName = $(Throw 'serverName is required') ) { # 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'), ('BS', 'Microsoft.TeamFoundation.Build.Common', 'Microsoft.TeamFoundation.Build.Proxy.BuildStore'), ('CSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'), ('GSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService') ) # 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 }

The way this works is that the "propertiesToAdd" array is looped through, and the $tfs powershell object is extended with a new object with the names of the first array element. The actual objects are retrieved from the third array element and they are retrieved from the assemblies in the second array element.
This function is used to create a $tfs object, like this:

$tfs = get-tfs http://YourTfsServer:8080

Next, you can use one of the properties of the $tfs object to do cool stuff.

VCS gives you access to the version control server. For example, you can download a single file, without an active workspace mapping:

$tfs.VCS.DownloadFile("$/Test Project/ClassLibrary1/ClassLibrary1.sln", "C:\ClassLibrary1.sln")

Or you can map a temporary workspace to retrieve all files in a specific folder:

$workspace = $tfs.VCS.CreateWorkspace([System.Guid]::NewGuid().ToString(), $tfs.AuthenticatedUserName) $workspace.Map("$/Test Project/Dev/Branch/SomeFolder", "C:\SomeFolder") $workspace.Get() $workspace.Delete()

WIT lets you use the work item store. For example, you can retrieve the uri, title and state of a workitem:

$tfs.WIT.GetWorkItem(1) | Format-List uri, title, state

BS is the wrapper for the build store. Let's do something more complex: retrieve the last successfully completed build and once this build is found, update the Build Quality.

$build = $tfs.BS.GetListOfBuilds("Test Project", "TestBuild") | where {$_.Buildstatus -eq "Successfully Completed"} | sort -property FinishTime -desc | select -first 1 $tfs.BS.UpdateBuildQuality($build.BuildUri, "Ready for Initial Test")

CSS contains the ICommonStructureService interface. This interface gives you access to the more common properties of you TFS server. For example, retrieve all projects on your server (which is just a bit boring...):

$tfs.CSS.ListAllProjects()

The last one, GSS, gives access to the group security service interface. The next two lines of script will give you a list of all the groups (both windows and application groups) a specific account is a member of:

$groups = $tfs.gss.ReadIdentity('accountName', 'YourNTAccountName', 'direct').memberof $groups | foreach-object -process { $tfs.GSS.ReadIdentity('sid', $_, 'none') } | fl type, displayname

I know I like working with TFS & PowerShell a lot, and I hope you do too. Enjoy!

- Permalink -

Name
E-mail
http://
Message
  Textile Help