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);