Error while updating VSTS release definition from powershell - azure-devops

I use the APIs listed in the VSTS API documentation here. On modifying a variable and saving the definition the error I get from the server is VS402982: Retention policy is not set for the environment 'environmentName'.
The portion of the PS script that performs the update is -
$c = Invoke-WebRequest 'https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId' -Method Get -Headers #{Authorization = 'Bearer ' + $authtoken}
$jsonObj = $c | ConvertFrom-Json
$url3 = "https://accountname.vsrm.visualstudio.com/projectname/_apis/release/definitions/definitionId?api-version=4.1-preview.3";
$contentType3 = "application/json"
$headers3 = #{
Authorization = 'Bearer ' + $authtoken
};
$d = $jsonObj | ConvertTo-Json;
Invoke-RestMethod -Method PUT -Uri $url3 -ContentType $contentType3 -Headers $headers3 -Body $d;
What could be wrong here?

This problem has been reported a couple of times in different forms, and is mostly related to a small problem with the powershell code in the question.
If you see error like the one mentioned in the question or this - VS402903: The specified value is not convertible to type ReleaseDefinition. Make sure it is convertible to type ReleaseDefinition and try again it means that there is a problem in the JSON object that you are posting to the server. Easiest problem will be to capture the request payload and analyse it for issues.
However, in the code mentioned in the question, the problem lies with powershell's ConvertTo-JSON method. Do note, the release definition has multiple layers of nested objects, definition -> environment -> steps/approvals etc, and ConvertTo-JSON by default goes only 2 levels deep to form a JSON object, which means you are missing some vital properties while calling the VSTS APIs. The fix would be to specify a large value for the -Depth parameter so that you do not miss any properties while calling the service.
eg. ConvertTo-Json -Depth 100
More details on the problem and how it's fixed in the script can be seen here.

Additionally to divyanshm's solution make sure encoding is correct:
$d = [Text.Encoding]::UTF8.GetBytes($d)

Related

Graphi API - Creating Document Set with Invoke-RestMethod

I need some help to get my code to create a Document Set in SharePoint Online using Graph API directly from a PowerShell script using Invoke-RestMethod.
I tested the request under the Graph Explorer portal and it works fine and I get a nice HTTP 201 (OK) as seen on the picture below:
Trying the very same request from my PowerShell script fails and returns HTTP 400 (Bad Request), I can't get the folder created and that is the first step to get the document set created, according to my research and an example found here:
Is it possible to create a project documentset using graph API?
As the first step mentioned in the example above, I need to first create the folder and then proceed to the following steps to achieve the creation of the document set but I can't get this first step done.
My application has the necessary permissions as I tested in the Graph Explorer:
Files.ReadAndWrite.All
Sites.ReadAndWrite.All
Sites.FullControl.All (not required but I had to try this one to make sure!)
I'm on the second step (folder creation) and I can't get past this point, according to the link above, once I get this working I will need to get the new folder ID, and then send a new PATCH to alter its content type to match the desire document set, I hope I can get some help, all the examples are vague and pretty much describe only on what to do but no actual functional code to sample from.
Thanks in advance!
$uri = "https://graph.microsoft.com/v1.0/drives/b!yVnguUBzyUC1PxgTM0JP-_ERFp1PTZFCjycaWZK6yKulBi9Ce_J8RIfF-OkWKE4B/root/children"
$headers = #{
"Authorization" = "$($token.token_type) $($token.access_token)"
"Content-Type" = "application/json"
}
$body = #{
"name" = "Test"
"folder" = {}
"#microsoft.graph.conflictBehavior" = "rename"
}
$request = Invoke-RestMethod -Headers $headers -Body $body -Method Post -Uri $uri
It should work when you modify $body like this
$body = #{
"name" = "Test"
"folder" = #{}
"#microsoft.graph.conflictBehavior" = "rename"
} | ConvertTo-Json
$body is a JSON object and you need to convert it to JSON. For initialing an empty folder object you have to use #{} instead of {}.
Thanks #user2250152, you gave me a great idea by solving part of the problem!
I did add the conversion to JSON as you recommended and decided to add the content-type back to my original header and it did the trick!
Adding the content-type solved the issue but your collaboration was essential, so thank you very much!
Now wish me luck to get the other steps done and achieve the conclusion of this thing!
$headers = #{
"Authorization" = "$($token.token_type) $($token.access_token)"
"Content-Type" = "application/json"
}

Different results from Invoke-WebRequest and Invoke-RestMethod

I am trying to call Azure Rest API and get Schedules of DevTestLabs. I tried Invoke-RestMethod, but it doesn't give value on "dailyRecurrence" key. But Invoke-WebRequest does.
What would be the reason for that?
URL
$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?api-version=2018-10-15-preview"
URL with $expand
$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?$expand=properties(dailyRecurrence)&api-version=2018-10-15-preview"
Calling Invoke-RestMethod
$output = Invoke-RestMethod -Uri $url -Method "GET" -ContentType "application/json" -Headers $authHeaders
properties : #{status=Enabled; taskType=LabVmsShutdownTask; dailyRecurrence=; timeZoneId=AUS Eastern Standard Time;
notificationSettings=; createdDate=26/03/2019 4:38:18 PM; provisioningState=Succeeded;
uniqueIdentifier=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}
Calling Invoke-WebRequest
$output = Invoke-WebRequest -Uri $url -Method "GET" -Headers $authHeaders
Content : {"properties":{"status":"Enabled","taskType":"LabVmsShutdownTask","dailyRecurrence":{"time":"1900"}
,"timeZoneId":"AUS Eastern Standard Time","notificationSettings":{"status":"Disabled","timeInMinute
s":30},"createdDate":"2019-03-26T03:38:18.0726376+00:00","provisioningState":"Succeeded","uniqueIde
ntifier":"XXXXXXXXXXXXXXXXXXXXXXXXX"},"id":"/subscriptions/XXXXXXXXXXXXXXXXXXX/resourcegroups/XXXXXXXXXXXXX/providers/microsoft.devtestlab/labs/XXXXXXXX/schedules/labvmsshutdown","name":"LabVmsShutdown","type":"microsoft.devtestlab/labs/schedules","location":"australiasoutheast"}
The problem:
is merely a display problem,
albeit one that exposes a long-standing bug, still present as of PowerShell Core 7.2.0-rc.1, namely that [pscustomobject] instances mistakenly stringify to the empty string - see GitHub issue #6163.
In short:
The data is there in the Invoke-RestMethod output, it just appears to be missing - see the next section.
Use $output.properties.dailyRecurrence to see it or, to more helpfully visualize the output, use it $output | ConvertTo-Json to re-convert it to (prettified) JSON.
Note: Situationally, you may have to add a -Depth argument to fully represent object graphs with a depth greater than 2 - see this post.
Invoke-RestMethod - unlike Invoke-WebRequest - has deserialization built in: with a JSON response, it automatically parses the JSON text returned into a [pscustomobject] graph as if ConvertFrom-Json had been applied to it.
You're seeing the default formatting of the resulting object graph, which isn't really suited to visualizing nested [pscustomobject] instances, which are represented by their .ToString() values - and therefore - due to the bug - may appear to have no value, even when they do.
By contrast, since the output from Invoke-WebRequest reports the JSON text as-is in the .Content property of its output objects, the problem doesn't surface there.
A simple demonstration of the bug:
[pscustomobject] #{
nested =
[pscustomobject] #{
deep =
[pscustomobject] #{
deepest = 'down'
}
}
}
As of PowerShell Core 7.2.0-rc.1, this yields the following, mistakenly suggesting that .nested.deep has no value, even though it clearly does:
nested
------
#{deep=}

Powershell - Invoke-RestMethod with multiple headers

I am trying to use Invoke-Restmethod in Powershell to call an API (I'm fairly new to this). I can get it to POST and return the jwt access token. I can also use that token to return an id via GET, however I'm then having trouble with the next step of returning the next set of data. I can get it to work manually via curl. I believe the issue may be because multiple headers are required to return the tenant list and I'm unsure of the format to get this to work.
The curl script looks as follows, and works as expected:
curl -XGET -H "Authorization: Bearer <jwt access token>" -H "ID: <id>" https://theapiurl.com/.......
I've tried multiple ways to do this in powershell, most recently as below, but nothing I'm trying works. I've tried returning the individual $headers contents and building a string (i.e. $headers2 = $.headers.Item(Authorization) + ......) but that doesn't work either. To be honest, I've tried so many different things I've forgotten what I have and haven't tried
$headers = #{
'ID' = $id
'Authorization' = $auth_string
}
$response = Invoke-RestMethod -Method Get -Headers $headers -Uri $url
Please could you let me know the correct way to add multiple headers (which I think is the problem and what I'm getting wrong)?
In case it's useful to anyone else, another syntax for setting the parameters of this commandlet is as follows (real working example for uploading to the GitHub release repository). It's usful to set all the switches (without prepending a hyphen) in the parameters object like so:
$upload_params = #{
Uri = $upload_uri + "?name=$asset_name"
Method = 'POST'
Headers = #{
'Authorization' = "token $github_token"
'Accept' = 'application/vnd.github.everest-preview+json'
}
InFile = $asset
ContentType = 'application/octet-stream'
}
"Uploading $asset..."
$upload = Invoke-RestMethod #upload_params
"The server returned:"
echo $upload
The variable $upload contains the full object returned from the server (converted from json to a PowerShell object). So, for example, you can also get properties of this obect like so:
"Upload successfully posted as " + $upload.url
Thanks for all the responses - none of them were really the answer but they did give me the confidence I was doing it the right way.
I'd been using PS Write-Host to check the data returned - this was working for the tokens and ID's, but wasn't working for next step. I wasn't getting an error, just no data. (I did see the returned data when testing manually in a command prompt window).
As soon as I added an -OutFile to the PS and checked the file, I realised it was working all along and PS just wasn't showing me the results. 2 hours wasted, although I've learnt more as a result!

Powershell, Graph API and $select

I have an MS Graph API in PowerShell working for the most part.
I am using
$Uri = $null
$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"
$payload=$null
$payload = Invoke-RestMethod -uri $Uri -Headers $Header -Method Get -ContentType "application/json"
$payload.value
however, it is not changing the field selection. It keeps returning the default fields as demonstrated here
https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http
What could I possibly be doing wrong?
I am using application based authentication. The payload is being returned but it is not recognizing the $select statement.
No errors are being returned by the PowerShell
I run it in Graph Explorer it works fine.
The error is caused by the type of string declaration used for the Uri string. You are declaring the string like this:
$Uri = "https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode"
This tells Powershell, that you want to evaluate the string. $ is Powershell's variable identifier. Undeclared variables are set automatically to an empty string, when evaluated in a string. Therefore the request executed against the Graph Api is:
https://graph.microsoft.com/v1.0/users?=displayName,givenName,postalCode
Your can check this yourself by writing the variable to the host:
Write-Host $Uri
If you execute this query with the Graph Explorer. It will return all users without an applied filter, which is the behaviour you have observed. You need to change the declaration to:
$Uri = 'https://graph.microsoft.com/v1.0/users?$select=displayName,givenName,postalCode'
Then, Powershell will not interpret $select as a variable and your request should work properly.

Checking if a Rest Call succeeds in PowerShell

I'm currently writing a PowerShell script that will remove all users from a tool via Rest API. The current script works as intended but is lacking in error handling. The only thing I want to check for is if the Rest Call is successfully completed. After googling, I couldn't really find much or maybe I'm searching the wrong terms. Any thoughts or direction is much appreciated!
Here's an example of code I've used successfully (assuming you've defined Uri, Headers, and Body if required - also note the Method here is set to Post):
try
{
Write-Verbose "Calling $Uri"
Invoke-RestMethod -Uri $Uri -Method Post -Headers $Headers -Body $Json -ContentType 'application/json' -ErrorAction Stop
}
catch
{
throw $_
}