Skip to content

Instantly share code, notes, and snippets.

@scrthq
Last active May 19, 2019 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrthq/f784228937183a1cf8105351872d2f8a to your computer and use it in GitHub Desktop.
Save scrthq/f784228937183a1cf8105351872d2f8a to your computer and use it in GitHub Desktop.
Azure DevOps / VSTS - API PowerShell functions
function Update-Release {
[CmdletBinding(DefaultParameterSetName = "UseDefaultCredentials")]
Param (
[parameter(Mandatory = $true,ParameterSetName = "PAT")]
[String]
$User,
[parameter(Mandatory = $true,ParameterSetName = "PAT")]
[String]
$PersonalAccessToken,
[parameter(Mandatory = $false,ParameterSetName = "UseDefaultCredentials")]
[Switch]
$UseDefaultCredentials,
[parameter(Mandatory = $false)]
[String]
$Name, # Pass a string here to update the release name
[parameter(Mandatory = $false)]
[Hashtable]
$Variables = @{}, # Pass the variable key and values here. Variables MUST exist as variables on the release pipeline itself (not via variable group) and they MUST be settable at queue time.
[parameter(Mandatory = $false)]
[Int]
$ReleaseId = $env:RELEASE_RELEASEID,
[parameter(Mandatory = $false)]
[String]
$ProjectName = $env:SYSTEM_TEAMPROJECT,
[parameter(Mandatory = $false)]
[String]
$ServerUri = $env:SYSTEM_TEAMFOUNDATIONSERVERURI # Should be something like "https://dev.azure.com/OrgName/"
)
Begin {
if ($ServerUri -like "https://dev.azure.com/*") {
$ServerUri = $ServerUri.Replace("https://dev.azure.com","https://vsrm.dev.azure.com")
}
if ($ServerUri -notmatch "\/$") {
$ServerUri += "/"
}
Write-Host "Getting Release details"
$uri = "$($ServerUri)$($ProjectName)/_apis/Release/releases/$($ReleaseId)?api-version=4.1-preview.6"
$apiParams = @{
Uri = $Uri
}
if ($PSCmdlet.ParameterSetName -eq 'UseDefaultCredentials') {
$apiParams["UseDefaultCredentials"] = $true
}
else {
$headers = @{
Authorization = "Basic {0}" -f $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$PersonalAccessToken))))
}
$apiParams["Headers"] = $headers
}
$body = Invoke-RestMethod -Method Get @apiParams
}
Process {
try {
foreach ($key in $Variables.Keys | Where-Object {$body.variables.PSObject.Properties.Name -contains $_}) {
Write-Host "Setting variable [$key]: $($Variables[$key])"
$body.variables.$key.value = $Variables[$key]
}
if ($PSBoundParameters.Keys -contains 'Name') {
Write-Host "Setting name: $($PSBoundParameters['Name'])"
$body.name = $PSBoundParameters['Name']
}
$jsonBody = $body | ConvertTo-Json -Depth 20
Write-Host "Updating Release via API"
Invoke-RestMethod -Uri $uri -Method Put -Headers $headers -Body $jsonBody -ContentType 'application/json'
}
catch {
throw $_
}
}
}
function Test-GetGist {
'Testing'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment