Calling REST with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i wanted to look at actually calling a rest api using powershell rest is really a standardized apr that's used very very widely today in the industry it's great for very loose coupled services and when i'm leveraging powershell i want to be able to interact with that now i'm going to demonstrate this also talking to azure because there's many times we actually want to interact with azure through the restful api maybe there's not a powershell commonly or a cli available yet but this really does apply to anything as always this is useful please go ahead and like subscribe comment and share and hit that bell icon to get notified of new content so we think about we have apis and api is kind of that application programming interface we want to interact with something and it's a standardized way to communicate to that service now rest is a very popular interface that we want to work with now i think about hey i have some kind of endpoint now this could be something in the cloud it could be saying on premises it really doesn't matter but i have some service that wants to talk to it and what we do is essentially we make a request so we're going to create a request now our request goes to a certain endpoint on that kind of restful interface and then we're going to have a number of different things now one of them is we're going to have an operation a method in rest terms that we're using for example i might have a method of for example post it could be put it could be patch and then i'll have a header i might optionally have a body so when i have different parts of that i could have parameters as part of actually the url i'm leveraging as part of that but i have these various components we can say hey there could kind of be a header header is also very useful if i have to actually authenticate i might have a bearer token i'll pass that as part of the header i might have a body and then what we're going to get back is basically a response and that response will have a certain payload a certain content very commonly it could be json it could be xml that can vary but we're going to get that response back now many commands will actually used rest behind the scenes but they're abstracted away for example in azure when i run a powershell command it's actually calling rest but that command that i'm using essentially abstracts that from me but then it actually goes and perform a request and it understands the response that's commonly what an sdk a software development kit does it abstracts away the actual api and we can see that so if i actually jump over super quickly for a second so what i've got here is a kind of basic script it's in my kind of random stuff repo it's going to define my subscription let's run that if i do a dash debug you can see i'm kind of doing that here that will actually give me a lot of information about running this command against azure and really the thing i care about here is if we scroll all the way up past all my kind of resource groups notice you can see the payload so it's giving me this kind of json payload but then you can see the headers so i can see kind of useful information that's part of the headers of the body of that response that we actually got back so it's giving me some good information just as part of that kind of header and then i can also see if i keep kind of looking through this i'll actually see the request we made to the api so behind the scenes it's actually using all the same things we could directly call so here you can see hey i'm using a http method kind of get over here i can see the absolute uri i'm leveraging to actually make that query against so it's doing all of the same things that well we can actually do ourselves it gives me information for example on azure a number of reads left before i expire my kind of quota i get 12 000. so it's using that restful interface behind the scenes so we have that but what about if we want to actually do this directly now there's actually two different commands we can leverage we have this idea of invoke web request so we have invoke web request i pass it a uri that can include the end point i'm trying to get to and what i'm doing is i'm storing this in a variable just r so this one i don't have to authenticate i can just leverage it so i'm calling this restful interface just for the azure update feed now if we just look at what dollar r is we can see we have this raw content we can see there's headers as part of that and then we do actually have as part of this the content and it's passing back xml so we can see here we do have this content configured for us and i could go and look at that so i can absolutely dump out that content so there's some work involved there but i get a lot of information but it's not doing much with that response i have to go and look at it i have to actually go and do various things alternatively if i'm actually making that kind of restful call that's going to return a rich format i can instead use invoke rest method now the parameter is the same dash url i'm passing it the same thing but now it's this time i'm just passing that output to a format table i'm not saying hey i want to look at the content or do anything else i'm just calling it because what happens is invoke rest method will actually translate the response i if it's json it's going to go and create a custom object for me that i can then just interact with like anything else in powershell so that's really a nice way so generally if we're talking to a restful endpoint i'm going to use invoke rest method now typically we do need to authenticate now this is going to vary depending on what is the system you're talking to that's not kind of a standard but what is a standard is it's probably going to want a bearer token it uses that token that it's going to use it has an audience so this restful endpoint is part of some service so when i think about authentication this is the audience i.e when i create a token as part of that token i have to actually specify who is it for and you have to put in who is the audience so if this whatever that service let's say it's the azure resource manager in my token the audience has to be let's say azure resource manager and then i can pass this token which has got a lot of other stuff as well as part of the header so that's what we're going to do next now generating the token is going to vary greatly but in azure which is what i'm going to kind of show here it's actually super super easy so if we go and look yes i can use my existing context and i've kind of got this fairly complicated set of commands here that hey creates this azure rm profile provider that then goes through and dumps a bunch of things out and eventually finds a token and i create a hash table which i'm going to use as the header so a hash table remember i'm just kind of doing this and i can put in my various kind of key values content type application json authorization is a bearer and then my actual token this absolutely works this code will work i can leverage that but it's actually much easier if i've already authenticated i can literally just run the command get a z access token and by default that's going to use the resource manager endpoint as the audience so i can literally just say hey give me a token it's going to default to resource manager and then create that hash table using the token that is inside that object that gets returned so now i've created the header and what i can now do is i can just do that invoke web request so i'm going to use the web request initially and i'm doing the same thing i'm dumping out resource groups now a key part here is so i'm passing the uri which is kind of hey what i'm actually sending this to and i have a certain kind of endpoint subscriptions etc i have a certain method so the method i'm using here is get and i'm telling it hey i'm going to pass headers and i'm passing it that hash table i created that says hey the type is json and it has my bearer token in it so i can execute that command and then i've got my content but if i just looked at the content on its own it's the json but it's kind of ugly so i can convert it from json's kind of a custom object and then back to json and what that really does is put it up so now hey there's the content but i do have the full headers as well so i can go and look at things like well how many azure resource manager requests do i have left in my quota hey i'm back at my eleven nine nine nines it's constantly getting kind of refreshed the rest method is cleaner my parameters are exactly the same i'm just replacing web request with rest method i'm storing this in r2 but notice now i just do r2.value and what it's done remember is it's converted it to kind of this custom object if i actually looked for a second and did get method and we execute that oh it's not right oh member not method oops there we go we can see what it created for me is a custom object so it automatically took the json and created this custom object that i can now just do things with now word of warning if i'm using powershell and i'm having this uri string remember some characters are special so the dollar character for example normally means hey it's going to be a variable and i hit this problem when i was trying to dump out compute skews for a certain region because it wanted you to have kind of this hey dollar filter equals location equals the region name well it just kept getting ignored well because dollar means a variable is going to follow i have to escape it so i just escape that out and then it's going to work so just be aware that if you put this as kind of a regular string you might need to actually escape stuff so now i'm going to look at all these skews compute in just east us 2 and it works now note i can request tokens for other audiences so here for example i'm doing the get easy access token but this time i'm saying hey i actually want it for a different audience it's microsoft graph graph.microsoft.com so now i get a different token i still create the auth header the same and now i could just say hey i want to get all users equals guest again because i'm using this dollar filter i have to escape the dollar i'm kind of escaping that out there's other methods you can use to escape i'm just manually doing it in the string so i'm just going to dump out all my guest users and there's all my guest users so i can kind of have those things there as well now that was all about method get i can have a body as well i can actually do a port i can do a patch remember the difference here is a put is all about replacing the current object that's there kind of already a patch just updates certain attributes of that existing object so if i just want to change one thing i would use patch instead of put so if we go back over here what i'm going to do now is i'm not got the complete content here this is actually if you're interested in the complete thing this was about setting some meta data for an azure vm there's a user data attribute and i've got the full kind of script in my random stuff repository over there but what i would have had is i had this variable dollar user database 64. and i need to pass that as the body to my restful call so i use a here string so here string in powershell member is i kind of start it with just kind of the you know double quote and then whatever i put within there it doesn't check it doesn't do anything with so i don't have to escape a bunch of stuff it will still evaluate the dollar kind of variables but i don't have to worry about anything else i don't have to escape things et cetera can have this multi-line just use it as is so i'm creating a hair string for the body and here you can see however properties and the user data attribute i'm setting to this variable and then you just call the web request the same way again i can even have variables as part of that uri you can see here i'm using kind of a dollar bracket to make sure it's very clear where the variable begin and ends so next dollar resource id and this time though my method is patch and then i pass it that here string for the body and then i still pass it the authentication header so that's how i can kind of patch those things together to do more things actually with it so that was it nothing is super complicated but people have actually been struggling i think with especially calling kind of the azure apis through powershell what bits do i need how do i do the things how do i get the token the easiest way but just generally how do i use restful from powershell so that was the goal i hope that helped until next time take care you
Info
Channel: John Savill's Technical Training
Views: 23,285
Rating: undefined out of 5
Keywords: azure, azure cloud, microsoft azure, microsoft, cloud, powershell, rest, restful, token, api
Id: 3dWZNfiyo_g
Channel Id: undefined
Length: 16min 5sec (965 seconds)
Published: Thu Aug 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.