0

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
Rod
  • 14,529
  • 31
  • 118
  • 230
  • 1
    Well the "update" link provided is telling you your request method should be `PUT` so `-Method Put` should be used. – Santiago Squarzon Mar 08 '23 at 22:46
  • updated op with results – Rod Mar 08 '23 at 22:52
  • 1
    Sounds like you need to add `-ContentType application/json` to your [`Invoke-RestMethod`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/invoke-restmethod) call too. – mklement0 Mar 08 '23 at 23:00
  • As far as the body is concerned, is that how I’d address setting a single variable in a group of variables? And if I wanted to update 2 or more, just comma separate name/value pairs? The Variablegroup-Update docs didn’t have an example on how to do this. – Rod Mar 08 '23 at 23:18

1 Answers1

1

In your Invoke-RestMethod call:

  • Use -Method PUT

  • Use -ContentType application/json to specify the media type.

  • Pass a JSON string to -Body; use ConvertTo-Json to convert your hashtable to such a string; this would equally work with a [pscustomobject] instance.

$body = @{ aVariableInGroup = 'hello world'} | ConvertTo-Json
Invoke-RestMethod -Method PUT -Uri $uri -Credential $credential -Body $body 

Note:

  • It isn't a problem with your sample input hashtable, but note that you may need ConvertTo-Json's -Depth parameter to prevent unintended truncation of more deeply nested input object graphs - see this post.
mklement0
  • 382,024
  • 64
  • 607
  • 775