I’m back again with a new PowerShell script to create a TFS project without Power Tools (which isn’t supported on TFS 15+).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
Function Create-PSCredential { [cmdletbinding()] Param ( [Parameter(Mandatory=$true, HelpMessage="Please provide a valid username, example 'Domain\Username'.")]$Username, [Parameter(Mandatory=$true, HelpMessage="Please provide a valid password, example 'MyPassw0rd!'.")]$Password ) #Convert the password to a secure string. $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force; #Convert $Username and $SecurePassword to a credential object. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecurePassword; #Return the credential object. Return $Credential; } Function Create-TFSProject { Param ( [Parameter(Mandatory=$true, HelpMessage="Specify a TFS URL, example: 'https://source.something.com/tfs'")]$URL, [Parameter(Mandatory=$true, HelpMessage="Specify a valid version control, example: 'TFS' or 'GIT'")][ValidateSet("TFS", "GIT")]$VersionControl, [Parameter(Mandatory=$true, HelpMessage="Specify a TFS Collection, example: 'Netcompany'")][ValidateSet("MyCollection1", "MyCollection2", "MyCollection3", "MyCollection4")]$Collection, [Parameter(Mandatory=$true, HelpMessage="Specify a TFS Project, example: 'NC12345'")]$Project, [Parameter(Mandatory=$true, HelpMessage="Specify a TFS Project Decription, example: 'This project is used for coding stuff'")]$Description, [Parameter(Mandatory=$true, HelpMessage="Specify a TFS Process Template, example: 'Scrum'")][ValidateSet("Agile", "Scrum", "CMMI")]$Template, [Parameter(Mandatory=$true, HelpMessage="Specify a TFS Administrator Credential")]$Credential ) #Set the correct template ID. Switch($Template) { "Agile" {$TemplateID = "adcc42ab-9882-485e-a3ed-7678f01f66bc"} "Scrum" {$TemplateID = "6b724908-ef14-45cf-84f8-768b5384da45"} "CMMI" {$TemplateID = "27450541-8e31-4150-9947-dc59f998fc01"} } #Set the correct VCS ID. Switch($VersionControl) { "TFS" {$VersionControlID = "Tfvc"} "GIT" {$VersionControlID = "Git"} } #Construct the URI for the JSON. $URI = ($URL + "/" + $Collection + "/" + "_apis/projects?api-version=2.0-preview"); #Trust all certificates. add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy #Construct the JSON. $Body = @{ name = "$Project" Description = "$Description" capabilities = @{ versioncontrol = @{ sourceControlType = "$VersionControlID" } processTemplate = @{ templateTypeId = "$TemplateID" } } } | ConvertTo-Json #Execute the invoke rest API. Invoke-RestMethod -Method Post -Credential ($Credential) -uri ($URI) -Body ($Body) -ContentType "application/json"; } #Credential to create the project with. $Username = "Domain\Username"; $Password = "MySecretPassword"; #Call the function. Create-TFSProject -VersionControl GIT -Collection MyCollection1 -Project "MyTestProject" -Description "Testing project creation on TFS" -Template Scrum -Credential (Create-PSCredential -Username $Username -Password $Password); |
Thanks for this. I was having a problem with the TFS API giving me an error ‘You must provide all and only these properties/capabilities : ‘ when I was posting a project creation request with only the name and description fields supplied.
There’s no mention in the TFS API v1.0 documentation that the capabilities fields are mandatory and must contain the template and version control information.
Which version of TFS are you using?
I want to get all open defect list through powershell script. Also it will send daily to daily open defect reports . Can you help me please