PowerShell 5.1
Azure DevOps Server 2020
I'm trying to use PowerShell 5.1 Invoke-RestMethod
to get/set a single variable in my Azure DevOps Library Group Variables. There's 9 other variables in the group.
The GET part works, now I just need some help with setting a variable in my library group variable. I want to set a single variable in my group that contains 9 other variables.
When I try to set it, I don't get an error but it doesn't update the variable either. It just returns data as if I did a GET.
Resources:
Invoke-RestMethod
Variablegroups - Update
Variablegroups - Get
function Main{
SetVariable
}
function GetVariables {
$uri = 'my azure devops uri'
$credential = SetCredentials -userId 'user1' -password 'P@$$w0rd123'
$results = Invoke-RestMethod -Uri $uri -Credential $credential
$results.value.variables
}
function SetVariable {
$uri = 'my azure devops uri'
$credential = SetCredentials -userId 'user1' -password 'P@$$w0rd123'
$body = @{ aVariableInGroup = 'hello world'}
$results = Invoke-RestMethod -Uri $uri -Credential $credential -Body $body -Method Put
$results
}
function SetCredentials ($userId, $password) {
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
return New-Object -TypeName PSCredential -ArgumentList $userId, $securePassword
}
Main
UPDATED:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The request indicated a Content-Type of \"\" for method type \"PUT\"
which is not supported. Valid content types for this method are: application/json,
application/json-patch+json.","typeName":"Microsoft.VisualStudio.Services.WebApi.VssRequestContentTypeNotSupportedException,
Microsoft.VisualStudio.Services.WebApi","typeKey":"VssRequestContentTypeNotSupportedException","errorCode":0,"eventId":3000}
At C:\Users\user1\Documents\WIP Update Password Library Group Variables.ps1:19 char:16
+ ... $results = Invoke-RestMethod -Uri $uri -Credential $credential -Body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand