Click here to Skip to main content
15,892,005 members
Articles / Productivity Apps and Services / Sharepoint

How to Set Check-in Policies for all Projects in Team Foundation Server using PowerShell?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Nov 2014CPOL2 min read 8.4K   2   1
How to set Check-in Policies for all projects in Team Foundation Server using PowerShell

The Team Foundation Server 2013 provides the administrators with an opportunity to add check-in policies to the Source Control Settings. These check-in policies define that while checking in the code in the Team Foundation Server Version Control (either TFS Version Control or GIT), the user has to perform certain extra actions. These actions vary from adding comments to describe the check-in, linking the check-in to one or more work items defined in the product backlog or to make sure that your build successfully passes all the unit tests defined in the project.

The check-in policies are defined on the Team Project level. However, when you have a Team Project Collection with hundreds of various team projects, you want a way with which you can standardize the check-in policies for all the team projects.

Unfortunately, Team Foundation Server does not support this functionality out of the box. But luckily, you can use the Team Foundation Server SDK to implement this programmatically. The PowerShell script below shows how to set Check-in Policies for all Team Projects in Team Foundation Server using PowerShell.

The script makes use of the SetCheckinPolicies on the Team Project object. What it does is basically retrieve all the available projects in the Team Project Collection and loop through all available projects to set the check-in policy individually.

You would notice that the script makes use of two policies:

  • Work Item
  • Check for Comments

However, you can extend this or change to select other installed policies on your workstation. You would notice that script makes use of InstalledPoliciyTypes on the work station. It basically makes use of the registered policy assemblies from the registry under the path:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\TeamFoundation\SourceControl\Checkin Policies

TFS Policy

PowerShell
[void][System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.TeamFoundation.Common")
[void][System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.TeamFoundation.VersionControl.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.TeamFoundation.VersionControl.Controls")

function AddPolicyOnProject($project){
	$policies = @()
	
	$policies += AddToPolicyEnvelope($workItemPolicy)
	$policies += AddToPolicyEnvelope($checkForComments)
	
	$project.SetCheckinPolicies($policies)
	
	Write-Host "Adding Policies to" $project.Name
}

function AddToPolicyEnvelope($policy){
	$policyType = $installedPolicyTypes | where {$_.Name -eq $policy.Type}
	return New-Object -TypeName Microsoft.TeamFoundation.VersionControl.Client.PolicyEnvelope 
	-ArgumentList @($policy, $policyType)
}

$serverName = "http://manasbhardwaj.net/tfs/Projects"

$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
$versionControlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
$versionControlServer = $tfs.GetService($versionControlType)
$projects = $versionControlServer.GetAllTeamProjects($true)

$installedPolicyTypes = 
[Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.InstalledPolicyTypes

$workItemPolicy = New-Object -TypeName Microsoft.TeamFoundation.VersionControl.Controls.WorkItemPolicy
$checkForComments = New-Object -TypeName CheckForCommentsPolicy.CheckForComments

$projects | foreach { AddPolicyOnProject $_ }

This post How to Set Check-in Policies for all Projects in Team Foundation Server using PowerShell? written by Manas Bhardwaj appeared first on Manas Bhardwaj's Stream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
GeneralInformative article Manas Pin
HM India3-May-16 2:17
professionalHM India3-May-16 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.