How to use the Azure DevOps REST API with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome in this video I'll show you how to use the azure DevOps REST API with PowerShell why PowerShell welp our shell is very powerful as you all know and we can use it to do quick iterative tries of our functionality it is highly recommended that you use vs code to do your partial work because it has intellisense debugging and it's very easy use note that everything that I'm doing now you can do in dotnet your most important resource when you try to use PowerShell and the REST API is the Microsoft REST API site which you can get to at this URL this site has all the information you need to access the REST API for all the different sections of the azure DevOps functionality we have everything from work items to releases to get to the different profiles a service endpoint a symbol server to test the test plans so there's information about pretty much everything you can get to in Azure DevOps you can get through the REST API and from this site all the information is available so when we do the demo a little bit later we'll I'll show you a couple of those different URLs so we can see what what's available in here note that this is available both on Prem and in the cloud there's a different versions of API which we'll discuss soon so a typical request that contains a verb and an instance URL which we'll talk about in a minute and the team project you want to access with areas and resource and a particular version so the instance URL is essentially the URL that you need in order to access resources and depending if you're on the cloud or in TFS that's going to change your team project obviously is the name of your team project your areas is are we accessing release work items all of those major top areas we saw on the website and the resource is a particular release in there is it build information from one particular build and then finally the version API that for the cloud currently there at version in five maybe five point one and four TFS you can navigate to this particular URL and you'll see depending on which version of TFS you have there's different version going from three two to five as well so in order to access the REST API you need to create an personal access token so how do we do that under the profile section of when you're logged into your azure devops account you can go to the security part and then create a new path with all the access that you actually want for that particular Pat and we'll do that before we actually start doing demos so we'll use that pad to create a header and then we'll send that Heather to all of our calls when we use the invoke REST API and finally if you're on premise you can also use your default credential the slash default credential at the end of your invoke REST API that should work as well so for the cloud we need to access resource areas URL and essentially when you use this particular functionality it will you give it the name of your account and you give it the resource that you're interested in so resource management extension management etc and then the grid for that particular service that you're looking for and then it'll return a URL in the form that we have here so for example release management it's going to be https vs RM dev - comm slash your organization in for extension management for example there's an ext MGMT dot dev comm and then for most functionality it'll be dev comm slash organization and on-premise for TFS it'll just be your TFS URL so you your organization colon 8080 slash DFS last default collection for example would be the URL that you would use and those actually are very important because they change and you don't know what they're going to be so in order to do that properly we essentially do this call so HTTP Devdas recom / API / resource area we give it an area ID so for example for build it would be the second line in the table below and then the rest of the information in this page and then once we do that it'll come back to us with the proper URL that we can use in our calls so for the rest of the video I will do some demos so we'll call for example the REST API to get a list of projects in your account will call the REST API to get the list of reliefs attached to your particular project will get the list of releases attached to this particular definition then we'll get a list of approvers for each of their releases and then finally we will modify a release variable and we'll modify our work item just to show you how to update so without further ado let's go to our demos alright so first of all we'll need to create a path in order to access the arrest API for our project so I'm going to go into my profile and under the security menu item I will come here and then I will basically create a new token we'll call it video we'll leave it for 30 days we'll give it full access and we'll create that so this creates a particular path here that if we move off of this page it'll be gone forever so we'll keep that in our clipboard all right so let's jump into vs code to do our demos so we have the URL that I'm using for my as your devops project then we use the personal access token we created a moment ago then we create our Heather by passing a token into a base64 function and then basically using authorization equals basic and then the token that's been base64 then we can actually start our first demo which actually uses the list of projects to do that we can actually go to the URL here and that'll give us a list of resource areas that we can see there's quite a whew and each one of them has a particular grid that we can use as a resource ID so back here I'm using the project grid in order to get the base area I need in order to call my my things so this calls a function called get the URL and this function up here is basically a call to the resource area function and basically doing a invoke rest method and when we get the result we have the URL in question that we need and we move on so we'll try and do our first demo we'll run it in vs code so I'll put a breakpoint over here and we can just basically f5 after ACOTA started we can f10 into our function and basically call the function here to get the URL and when we see it comes back with for getting dallisa project it's basically just dev dodger comm slash our tenant so we can now create a URL with this base URL to get the list of projects and in order to see that we can actually just go to this particular URL here and we'll see that to call and get the list of project we need to do this particular slash API slash projects and then version that we want so if we go back down we see that we have slash API slash projects and then the API that we want and then we'll basically just call the rest method again and this will give us essentially a list of projects I have three project in this tenant and then basically we'll just list them so parallel bill TFS to get and end demo so that basically is a very simple call to get the information about a project so we'll reuse this list of project we just created for our second demo which essentially is the use of those projects but we're get we're going to get release definitions for those projects so first of all we'll get into our loop here with all of our different or three projects we want had the release management area ID the base URL for the release management and as we can see this one is a little different it uses VSR m dot dev command slash our account so now that I have the base URL properly I can say use that base URL use a project the first one because we're in a loop right now and then the rest is the call to get the definition lists so if we go to this particular URL we see all the different information we can do to get a list of releases so I'll continue with an f10 and here this will call all the different release for the parallel build there are none in this project so if we keep going we're gonna get our second project there is also none in that project and then the third one which is our demo project actually has a list release definition so we can actually just basically go through it and get the name of it so if I go to my project and I go back here and we get the demos project which actually has the release and we go to our pipeline we'll see that we have our demo - ASB net core - CI - CD and that's the name of the release that it got here so that's our second demo the third demo is gonna go in and go a little deeper so I'll first it'll get the list of release definitions so same call we did just now once we have our release definitions we actually can dig in and get a list of all the definitions that are attached to this particular release so here we can see that we're still in that release definition and I'm gonna call the API that's gonna say get me a list of releases for a particular release definition right so now I have the release definition that I have and I can just dig in here and I'll see that I have of for five release that were done for this particular release and this function just says it's going to be released three four five six and seven so if we go back to our project again we'll see that we have released two three four five six and seven which are shown over here and the reason why we only see five because you saw in the site there there seems to be a few more is because when we made the call we said return only the last five here that was a in addition we can add to our URL to basically not get all the release but get a particular list of release so essentially that's our third demo our fourth demo is a little bit different because now we have to dig in each one of those release definition and actually get two releases for that definition and then if one of those release was actually approved by an approver I like to list the name of that approver so we're gonna go again and we're gonna do essentially the same thing we've been doing so the first two projects don't have any release definition so we're gonna skip those two and then the third one actually has the demo project in here and it says that it found one release definition it's going to list that release definition over here and then we'll dig in and we'll get all the releases for that definition so essentially what we've been doing for the last two demos but now we're gonna actually dig in to each one of those releases and then in each one of those environments for those release and then we're gonna list the for each one of those environments we're gonna look at the pre-approval section of that release and look to see if it was not automated so which releases you can basically say that you want to have an automatic approver so basically it's automated it just goes off and it does its deployment without actually having a stop for an approver and it so it needs to be automated and approve status of approved and if both are true then go ahead and write who actually did that so we'll basically just run through this and we found that release four actually was approved by me on such-and-such a date all right so this is really interesting because rest api gives you that information and much more as you can see but there's no report in the sts to actually give you that kind of information so actually using powershell to get that information and maybe save that as a CSV file or another JSON payload in order to do another report for somebody is very important and very interesting that you can actually do that so we'll keep moving we'll go to our fifth demo and our fifth demo essentially is we're gonna say we're gonna update a release variable into our release so if we go to our release and we edit it we'll see here that this one has some variable and the first variable is called demo and then it has a value called old value so we'll modify this value to be another value so we're gonna do pretty much everything we did the same thing we've done for the last two or three demos and we're actually gonna dig in all the way to here so I'll just put a breakpoint over here and just run through because we've seen that before and now that I've actually am here one of the call that we want to do is we want to get environments we want to expand the environments information because by expanding that we can actually go and get environment variables that are part of this environment so for each one of those environment I only have one here which is dev so as I'm going into I can actually dig in and look at all the environments and look at all the variables attached to this particular environment so if we go here and we look at variables we'll see that there's one called demo and it has a value of old value so first of all here I'm checking is there a variable called demo and if there is just write me the value of what it was there before so it was old value before we'll modify that to be new value well save all this and then we'll call an update so that we modify the actual release and then we'll check in result if it's a new value now so now it says new value which is the value we've put here if we go to the website and we actually refresh this we'll see that now the variable is gonna be called new value so that changed it directly on the site so for the last demo we're gonna do we're going to call it work I know I'm going to modify the title of it so we need to get the work area ID grid and then get the base URL for it and we can see that the base URL essentially is def - Joe calm and our project and we're gonna basically fix it work item number one which if we go to our project we can actually see in our boards will have work item number one is demo feature here and what we want to do is we want to change the title of this and add a plus demo at the end so essentially we're going to call the work atom to get the actual work atom payload and then we're gonna modify the body for this we're gonna get the value of the title field and we're gonna add plus demo at the end and then basically we're gonna update that so I'm just gonna run through this again we're gonna get the work items so if we look at this work out and we see need to list the fields so all the fields that we have here including the title which says demo feature we're gonna create a new body for this so this says the before title is demo feature we're gonna change the title of this call and then just basically get the title again and we see it's called demo feature + demo and if we look here and we refresh our browser we can see that we have demo feature + demo so as you can see using PowerShell to call the REST API is quite simple and it's very powerful we can run true do some debugging and actually get all tailored information that we need in order to create other reports specific custom tasks anything that we need to talk to TFS and/or Azure DevOps can be you can't we can use PowerShell to do it so thank you for listening see you next time
Info
Channel: Etienne Tremblay
Views: 45,367
Rating: undefined out of 5
Keywords: PowerShell, Azure DevOps, TFS
Id: ylJmee-4KTo
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Wed Feb 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.