You can use the REST API to achieve that (with the API version api-version=2.3-preview.1):
I tested on TFS 2015 Update 4 (Version 14.114.26403.0) with below PS scripts, it works as expected.
Below PowerShell scripts for your reference:
Create a new release: (See REST API here)
Param(
[string]$collectionurl = "http://server:8080/tfs/Collection",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "username",
[string]$token = "token",
[string]$releasedDefinitionId = "2"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{
"definitionId":$releasedDefinitionId,
"description":"",
"artifacts":[
{
"alias":"VSTEST",
"instanceReference":{
"name":"20171123.1",
"id":"1256",
"sourceBranch":"$/0418Scrum/web0418"}
}],
"isDraft":false,
"manualEnvironments":[]
}
"@
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/Release/releases?api-version=2.3-preview.1"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$ReleaseID = $result.id
Write-Host "ReleaseID:" $ReleaseID
Deploy the release to a specified environment: (Deploy the third environment in this sample, just change it based on your requirement.)
Param(
[string]$baseurl = "http://server:8080/tfs/CollectionLC",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "username",
[string]$token = "token",
[string]$ReleaseID = "3"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get releaseresponse
$Releaseurl= "$baseurl/$projectName/_apis/Release/releases/$ReleaseID"
$releaseresponse = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $Releaseurl
#Get all of the environment IDs from the release response:
$environmentIDs = $releaseresponse.environments.ForEach("id")
#Get the specific environment ID by grabbing the element in the environment IDs array:
$firstEnvironment = $environmentIDs[0]
$secondEnvironment = $environmentIDs[1]
$thirdEnvironment = $environmentIDs[2] # ...
#Create the JSON body for the deployment:
$deploymentbody = @"
{"status": "inprogress"}
"@
#Invoke the REST method to redeploy the release:
$DeployUrl = "$baseurl/$projectName/_apis/release/releases/$releaseid/environments/"+$thirdEnvironment+"?api-version=2.3-preview.1" # Change the envrionment ID accordingly based on your requirement.
$DeployRelease = Invoke-RestMethod -Method Patch -ContentType application/json -Uri $DeployUrl -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $deploymentbody
write-host "environmentIDs:" $environmentIDs
You can also get them together, Create a new release and deploy the specific environment in one script. See my answer in another thread.