PowerShell Live Training - APIs and Web Requests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay guys so um yeah i had my mic turned off there just for a second i was just giving some more people we got more people joining um so i want to give everyone time um so i was just waiting um but i think we're ready to get started i posted a link to theweatherapi.com if you guys want to join you can sign up there it's free and we're going to be learning how to work with apis in powershell so super important apis you know i use it all the time at work to power on power off instances create snapshots release snapshots in my cloud environment don't mind my husky back there you guys can see that he's back there eating a chew toy archer can you not make so much noise all right so uh yeah we use apis at work all the time to bring down instances bring them on schedule them and things like that so we're gonna learn how to do that and we're gonna work with a basic weather api and we're just gonna do something uh as simple as just getting the weather in the forecast but the same principles that you're gonna learn uh in this lesson can also apply to cloud computing and things like that okay and we're gonna kind of build on what we talked about uh in the last i think is the last webinar where we did uh you know variables for loops and things like that so what i'm going to do is you need to have gone to this website weatherapi.com and you should have registered so you guys will get a free account and you'll be brought to this dashboard page and this is my api key you should keep this a secret i'm going to refresh and delete this api key as soon as this live stream is done uh because this will be live on youtube but all right i hope if my dog is too distracting let me know and i will uh i'll turn my camera away from him i guess or i'll let him go in the other room really quick uh so when you register you're going to be brought to this page this is the api key that i was talking about and uh we're going to copy that api key and we're going to go to api explorer this is a website or this webpage allows you to browse your api without having to write any code so you can kind of get familiar with how the api works before you get into writing scripts with powershell um herms is asking about turn on the mic we will do that at the end um but that's a limit of 25 people so if i do that right now we'll kick people out so uh basically when p most people will leave and then i'll turn on the mics and uh usually it's less than 25 people at the end so i'll do at the end um so we're going to go ahead and put in our api key here at the top and i put in a zip code here under this uh parameter value and this is a zip code for fairfax and it says here you can enter in an ip address latitude launcher whatever click show response and then here at the bottom of the page uh you're going to get the response body and this is the actual information fairfax virginia uh it has a latitude of longitude you know the the time zone that it's in american new york it's just a bunch of information about the weather in this particular location so if we wanted to do something like get a forecast we can click the forecast tab and we could change this to like three days i think that's the limit on free accounts which is what we're using so if i click show response now it shows us the actual api uri that we're going to call to or the url and then it gives us the headers at the top and then the response body so this is stuff that we're going to be manipulating with powershell converting it to a powershell object and then doing things with that data so one thing i could do is i could just copy and paste this into my web browser press enter and i'm going to get the json you know returned directly to me now if you're working with an application that doesn't allow you to have an api browser like this which and that's common you're going to just get raw data just like this and you can use a website to format it in an easier way to read let me see i'm going to find that website it is jsonformatter.curiousconcept.com i'm going to post this in the uh in the chat so if i go to this page i can copy all this json and i can paste it into this formatter and i can hit process and it's going to format it in the same way that i was seeing it here on the website so if you're working with a service and they don't provide a nice tool like this for you to see what you're working with then you can go to a json formatter like that and it allows you to you know expand or collapse different data you can look at the different forecast days and things like that okay so uh we're gonna close that because we don't really need it we kind of we have the same information here um so what we're gonna do now is we're gonna do the same thing inside of powershell that we just did uh through the web browser so i'm gonna go ahead and launch the powershell ise so i'm gonna click the start button and i'm just gonna type in ise and i'll right click and say run as administrator and i'm getting the user access control prompt i'm gonna say yes and let me drag this over here so you guys can see this all right i'm going to zoom in so you guys can see what's going on here so the first thing um and by the way uh the script that i'm about to write i'm going to share with you guys i'll email it to you guys i think i usually send out the replays on monday so when i send out this webinar replay i'll email you guys this file so first thing we want to do is set up our variables and first variable i'm going to set up is api key we're going to set that equal to that long string that we copied earlier now i need to go back to the website in the dashboard and just copy and paste that uh and just keep in mind this is one way of doing this we could also take a shortcut and just copy this uh url as well but i want to show you guys how to structure a url using variables um because essentially this is the url that we should end up with api url and it's going to look like that okay so uh let's see the zip code that i did was 22.30 so we're going to go back to the ise and we're just going to say uh that was the parameter is query so we'll just call it query is equal to and then the zip code that we put in and um now we're going to set up how many days because that was another option that we had and i'm going to set that to 3. so just i'm going to drag this over to the left screen and drag this here so just to reiterate the data that we're looking at we have the api key which is configured here we're going to assign that to a variable of api key and we don't need to define the protocol or the format because that's all set by default then we have this query parameter and that's set to 0 30 and i'm just assigned that to that variable there and then down here we have a parameter of days and that's set to three now these variable names does not matter what you want to call them it's just uh that's what we're going to reference them by in the future so i'm just selecting these and i'm pressing f8 and that's going to execute that in the memory so if i wanted to call api key i could do that and press enter and get that information so let's go ahead and build the uri or the url however you want to call it that we're going to be calling out to so this is gonna be we're gonna be calling to uh the root of this address which is gonna be i'm just gonna copy it over here from this screen all right here we go so it's gonna be api http remember we're not doing uh https because http is the default uh api.weatherapi.com version1 and we're going to grab the forecast so this forecast tab here is forecast.json right so if i come over here i can type that forecast.json now um we also need to add our api key so we're going to do a question mark and we're just going to pass key is equal to and we'll just say api key so we're actually calling the variable up here api key so if i press f8 on that and then i output the value of uri it's going to give us a one string that contains the end point that we're reaching which is forecast.json and it contains our api key which is listed here all right but we need to keep going because we need we still need to pass the query and we need to pass the actual uh number of days so the query is passed by and q and that's going to be equal to dollar sign query sorry and q equals dollar sign query make sure i type this right uh so again we can just execute this and press f8 oops f8 type in uri and we're getting this string i'm going to zoom out just a little bit so this will come out a little cleaner there we go so we have the query um and then we have days so we're going to say and oops and days is equal to and we'll just say days and i'll try to max match the capitalization there okay so we could just select this code right here and press f8 it's kind of a shortcut it'll just output the value that we're trying to get so um if i run that whole thing it'll assign it to the variable name if i type out uri what we want to do is make sure that this this is a valid url and since we know that we can access it directly in the browser let's just copy this and let's just paste it into a browser and make sure we get some kind of results so we're not getting a anything like access denied like if i didn't provide the api key here if i set the key to equal zero we're gonna get an error message and it's gonna say api key was invalid so um by us running this command right here or running this uh or rather copying and pasting this into our browser we can tell that the api is working as expected so we're actually able to get return data now we're not returning data yet in powershell all we're doing is storing the url that we're gonna access so we need to do now uh let's just write a little bit of output so we can say let's say we're going to output what uri we're going to contact and we'll say right dash host and we talked about this um we talked about this in the last lesson if you guys remember in the last webinar api call uri is going to be equal to uri okay now if i execute this it's going to tell us this is the actual url that we're going to be accessing now let's go ahead and access that and actually grab data so what we're going to do is we're going to make a variable well let's go ahead and make a comment say let's say make the request we're going to say request if i can spell it right and i need to do a dollar sign request is equal to and we're going to say invoke dash web request and we say dash uri and that's going to be equal to uri so if i highlight this instead of a command here and i press f8 and now if i try to access the object request we get this output right here now you'll notice this looks quite a bit different than the json that we're getting down here but really it's just powershell's way of showing that information so what we have here is we have the response body that's shown within content you'll notice that uh inside of the response body the first item that you see here is location well if we look under content the first uh little element here is location right and then we have raw content this includes the headers as well and then we can just pull out the specific headers so um if we want what we can do this this is a powershell object so we can say request dot headers and we get the request headers that we see up here so we have transfer encoding is chunked uh connection keep alive and so forth so all that data is available here um now let's get down to actually accessing the content and one thing i want to point to point out you guys we don't want to uh when you're working with apis you want to make is the minimum amount of calls to the api as you possibly can so right now if i press f5 on this on my keyboard i'm going to make five calls to the api and it's totally unnecessary because we've already gotten that data and it's already stored in this variable so what we want to do is we want to tell the script that hey if you already have the data don't make another api request because if you remember we're limited to 20 000 api requests on our free account we're not going to reach that today but when you get into running larger automations you always want to try and write your code as efficient as you can so what we're going to do is just write a simple if statement that's going to decide if we should actually reach out to the api or if we already have the information and we don't need to so we're going to do that by just saying if and i'll just put this down on new line we'll bring it back up in a second we're going to say if and in parentheses we want to see if the request variable exists or if it doesn't exist rather so we're going to say an exclamation mark and then we're just going to say requests and what this does is it says if the request variable has not been set or if it's empty then we want to do the following and if request is empty what we're going to want to do is run the invoke request command and populate that variable right if it's not empty then we're going to say else and we'll say write dash host api call was already made not making another one all right so let me go ahead and just maximize this because we can we can pretty much focus on the powershell now i'm going to zoom in a little bit more if you guys have any questions along the way uh let me know and i will uh definitely answer that because uh that's a big part of the live streams is answering questions so i want to help you guys out if you guys need it all right so i'm going to call this api working price i'm saving it to my computer we are having thunderstorms so if i go offline i probably lost power hopefully i don't but all right so essentially this line of code should only make the request if the request variable has not already been populated so let's select this and press f8 and we can see that it says the apa api call was already made not making another one now if i was to close this ise and uh let's see close the ise and i'll restart it and i'll reopen that script i have another ise open hopefully this doesn't that doesn't interfere with what i'm doing but if i reopen this and i run this line again it should make the oh right i need to run the whole line so if i press f5 um it actually made the request and if i press f5 again you can see that here the api call which is right here and then we didn't output the api call was already made now the next time i run this we're going to see the api call was already made and every time i run that it's going to prevent me from making another api request so i'm sitting here matching that button matching the f5 button and we're not making additional requests all right so just a little hint there on how to be efficient with your code all right so now let's get into the content so we have this variable name request what we need to do is access the content and generally you want to stay away from raw content i've in my experience i've never had to use that if you guys have ever had to use raw content let me know but it just contains like some information at the top uh it does contain your status code and things like that but you can get that other ways so i generally don't have to mess with that so we're going to do is say request dot content and what this does is outputs that json so again if we wanted to we could copy this into that json formatter website we've already done that through the website so i won't do that again but there's a built-in powershell command that will convert this from json to a powershell object and that's going to be as simple as invert from dash json so now if i press enter it converts this to a powershell object now it's not that useful like it is right now because we have objects within objects but we'll get into how to access all of that um in a little bit uh another thing to keep in mind is that the data here is not fully shown because it kind of goes off screen so we can see it looks it appears as though we only have location and then something called current however that's not true if i pipe this to ogb or outgrid u we can see that we have location we have current which is the current weather and then if we go all the way to that we have another object forecast and then we have if there's an alert or not which there isn't um so we're not getting all the information when we try to put it out there so there's different ways that we can format this uh also as a side note if you're using powershell six you can run the as hash table command and i'll just show you guys what that does this is powershell 7 but this works on powershell 5 and above i'm going to copy you can see here this is virgin powershell version 7 rather if i paste this in here and run all this and if i go request convert from dash json and i say as hash table it's going to return an object that looks like this much easier to read than what we have right here which is just kind of putting it all like horizontally across the screen and then when you run out of room on the screen the rest is just lost over here and nothing in the nothingness so if you're using powershell 6 that's a nice little shortcut that you can use to convert it into the type of object you want now if you don't and you're using powershell version 5 which is pretty much everyone who's using the ise there's another way that we're going to grab this data and that's by piping this to oh oh before i get to that before i get to the switching output let's talk about formatting so there's a couple different formatting options we can use and i'm going to paste this into the chat right now so you guys can see this hopefully this comes out right um okay that did not come out like i wanted to but there is uh i'm just gonna copy and paste this into the script you guys can see it there is uh we can format custom so let me copy this up here and i'll show you guys how this works um this is just different ways to format the data so we can see what we're looking at let's see format yeah let me grab this real quick okay cool um format the data and we're going to say we'll just make a new variable we'll call it converted formatted json is equal to request dot content and we'll say convert from json and we'll say format and we could do format custom so if we run this line i'll highlight it and press f8 oh that came back with nothing hold on a second let me figure out whether why that happened uh let's see here let's run this make sure this comes out with data okay and then we convert it convert from json oops and then we can do format maybe format custom just doesn't work on that oh there we go uh i'm not sure that i spelled something wrong up here i don't see what i did wrong oh i'm yeah i'm silly because i didn't output the actual variable name so converted at the new newbie mistake okay so when i output that uh value or if i just highlight this right here i make it simpler for you guys if i just highlight this right here and i output it like this it's going to give me an object that looks a lot like json um it's an easy way for you guys to view all the data but it's still not like it's really not that user friendly and once we format that data and that's stored inside of converted formatted json i can no longer access sub objects so if i try to do converted formatted json i try to do location it's not going to work another one that's pretty nice is you can do format list and it's an easy way for you to see the actual data so if i go converted formatted json um this is much nicer than what we were seeing uh when we just converted it from json let me turn down my sound real quick so here we have uh the four objects that we're trying to access here we have location current forecast and we have alert right so a quick easy way to see your data is just format dash list but um you don't really want to save your data like that because once you format something it turns it converts it from a powershell object to just plain uh string text and that's not good for what we're going to be doing today so if we go here and we go to let's see where's our variable let's make a variable up here and call it converted json so convert convert from json and we'll call it converted json and we'll set that equal to request dot content convert from dash json all right and let's execute this line of code all right let's try this again press f8 so convert converted or converted json there we go we can access the uh sub objects that we see here so we can access the alert we can access the current and the forecast and we can access the location so if i go to location and press enter it's going to give me the location data and this is what we were seeing on the website now beyond that i can extrapolate and go further and i can say location.name or i could do location.region or i could go location.country these are all things that are not possible if you convert it to a formatted list or a formatted table or things like that so we want to keep it in the powershell object form because we can access that data more easily um all right so let's move on so let's go ahead and grab the forecast so we're gonna go down here and just make a comment let's say grab the forecast for the next three days or whatever and that's about that wrong but that's fine all right so we're going to make a variable and we're going to call it forecast so forecast is going to be equal to converted json dot forecast and uh let's go to forecast day so i'll kind of step actually before we do that let's just output this and see what it looks like i got to get rid of this wireless keyboard it doesn't want to work for me so if i output this new variable called forecast it contains this object called forecast in now if we expand that we go inside of forecast day press f8 now we have three days uh the 24th which is today the 25th tomorrow and then the day after which is the 26th and then we have actual forecast information for that day so if i click here and let's say we want to access the first day we could do open brace zero closing bracket is that bracket i think it's a bracket and if i press f8 and now when we output forecast it's just going to output the first one so i can now access forecast day i can access and this is all the information like the minimum average temperature um i have the condition and we have humidity average humidity and things like that um so what we really want to do is let's create a script that will go over all the days in this forecast it'll tell us what the high is going to be what the low is going to be if it's going to rain and things like that okay so we can do that if you guys remember with a for each because this is this is a power shell object and it has a list or an array of objects so we can iterate over each of these lines and we can create a script that is going to actually you know tell us what the information is that we want to know so let's make a comment say iterate over the forecast and output the data and we're gonna say four each um and a four each in my opinion is appropriate because uh we don't have to define an index and we don't have to define how many times we're gonna count up to we don't have to figure out how many elements are in there a four 4h will just for each particular object in there it will iterate over all of them so we have to tell it um you know count from zero to three or something or from one to three or zero to two um 4h is much more appropriate in this specific scenario than a for loop so we're going to say for day in it's going to be in the forecast okay and we can just do something like write dash host and we can just output day and just see what happens here so if i grab this and i press f8 it outputs each day so i can see that i'm accessing each day and if i wanted to do something like for each day dot and i could say date and if i just run this it's going to output the date for each day so we're on to something uh now let's let's kind of like take this further uh let's prep the variables and let's start by getting the date so debate is going to be equal to day. right and if we're looking here we have date so uh we're redefining each line here as a day we're writing that as a day okay and then we're able to access each line by calling day and then whatever the column name is in this case it's date hopefully that makes sense you guys if it doesn't make sense let me know in the chat and i will and i'll answer or hopefully help you guys clarify all right so we're going to say the high and this is going to be the high temperature so this is going to be day day good day again and then inside of day it's going to be dot and here i can kind of browse through and see what we have here so we have max temperature in fahrenheit so i could do celsius or fahrenheit we're going to go with fahrenheit okay and we'll make a variable called low and that'll be equal to day dot day and it'll be i think it's min yeah min temperature fahrenheit and then we can make a variable for condition that's going to be equal to day dot day oops there we go and condition dot text so i kind of know this because i've already done this but the way that i would figure this out if i didn't know is i would just because i've already run this for loop i have day loaded into memory so i could just output day and then i could say day dot day let's see what's inside of day and then inside of there i have condition so let's see what's inside of condition and inside of here i have something called text and that says partly cloudy and that looks like something that i'd want to use so i'm going to say text and we have partly cloudy so i'm assigning the condition to the value of that object all right and if we wanted to do we could do when to max wind is equal to day uh today uh let's see um max wind in miles an hour why not okay and then we could do minimum wind or min wind it's equal to day dot day that minimum now let's see what would that do they i don't know if they actually have a minimum wind let's see we have maximum wind yeah there's not going to be a minimum wind i guess that makes sense so we can do something like uv okay uv index all right okay so now we can just say output the weather for today okay and what we're going to do is we're going to out we're going to make a cut or a write host command and it's going to say that in quotation marks date date so it's saying today is going to have a high of high and that's the variable name high with a low temperature and we'll say low let's make a period and then say the max wind will be max wind okay and we'll just close out there and we'll put in uv index after the high it'll be a high with a uv index of uv index and we'll say the low temperature today will be low okay so if i just highlight this and i press f8 this is what it outputs it says 2020 07 26 which is today's date is going to have a high of 97.5 with a uv index of 10. the low temperature of today will be 76.3 degrees fahrenheit and the max wind will be 8.1 and we can say miles an hour mph and we could change these to be like fahrenheit uh or we could just add f after so it's a little bit more obvious that we're talking about fahrenheit not i mean i guess 97 celsius would be kind of ridiculous so now if i run this command if i press f5 what's going to happen is it's going to output for all three days in the forecast uh it's going to say today is going to have a high of 85. then there's tomorrow which is the 25th it tells us the weather for tomorrow with a uv index of eight and then it tells us what the uh the low will be oh and then i guess i shouldn't be writing today and i spelt that wrong uh the load temperature will be yeah i should get rid of today in that sentence let's rerun that because tomorrow is not today obviously and let's see the low temperature there we go run that again okay so now we have it outputting all the information that we want for the next three days and this api we could do you know more days if we paid for it but that's obviously we're just learning how to use an api so it's not necessary right now we're just getting information writing information is a whole another a whole nother ballpark same idea but it's a little bit more complicated to write information we're not going to get into that today um so one last thing we're running like super fast today i think we're going to be done pretty soon so one last thing i'm going to show you guys how to do is how to convert this date from a string in this format how to convert it to something like if we just wanted to output like friday is going to be like this saturday is going to be like that or if you wanted to change the date format so what we can do is here under date we can pipe this and remember when we when we're piping a command we're taking the output of something and we're passing it along and then we're able to manipulate that output so we're going to pipe this and we're going to say get dash date i'm going to say dash format and if we go to get dash date let me pull this up on my web browser so you guys can see how this would work get dash date powershell here we have microsoft documents here uh so we have um dash format was what we're looking for here we go so we'll see dash format there we go okay these are the formats that we can use for dash format so we can do k i think this outputs this is a time zone offset we can do uh 24 hour format with no seconds uh we can do a four-year digital format so we could just output the year which wouldn't make much sense or we could do day of the week full name so if we wanted to get today's date and just output the day of the week what we would do is say get best date format dd d d and now if we run this command let me uh maximize this again to the screen if i run this again now we have friday is going to have a high of 85 and you know it goes down the line says saturday and then it says sunday and we could change that um so if we wanted to add friday the and then the number of the day let's see where is that that would be day of month and two digit we could say uh db press f5 let me clear the screen and run that again so it looks cleaner so friday 24 is going to be and that's not the best way to do it you'd have to write logic for 24th 25th or 26th there might be a format that does that for you too um but that is how you can format your dates and make them look cleaner okay so that is interacting with apis and making api calls and getting that information and manipulating that information uh we're not going to get into writing data i have to find an api service that we can use for free and actually write data the apis that i use obviously hold like personal information like it has people's home addresses and um and we can't work on day like that so i have to find like an api that we can write to uh so that we can do another lesson and do it on on that but do you guys does anybody have any questions uh at this point okay so here we have i think most you guys know about this but we have the community and uh this is where you guys can make student support requests you can ask for help uh it looks like yeah santa's you obviously know about it so you're already making requests which is cool so uh i usually try to respond as fast as i can i didn't see this one because i was prepping for the webinar but um yeah guys make sure you're taking full advantage of this form here uh it's definitely you know very it can be very helpful and we're helping people with stuff outside of the scope of service like it doesn't have to be specific to the course but if you're working on windows server at work and you're having problems you can post here and um you know we'll help you guys get whatever your issues are fixed uh so cory's asking about the webinar time uh last time i did it at 3 p.m and that seemed to be too early like people were still at work so today i decided to do it at 5 00 pm eastern time uh if you guys have a better time then you know let me know i'm i'm flexible i'll do whatever time you guys want uh i sent out a survey and most people said fridays were the best day and sometime in the afternoon so i'm just trying to pick the best time for everybody
Info
Channel: Server Academy
Views: 2,281
Rating: 4.875 out of 5
Keywords: Instructor Paul, Server Academy, Windows Server
Id: GZ2nIErqAvY
Channel Id: undefined
Length: 34min 1sec (2041 seconds)
Published: Sat Aug 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.