PowerShell Tutorials : Environment Checker (Beginner Project)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this powershell beginner project solution video so in the last video i went over eight beginner powershell projects and the first one on the list was the environment checker so i'm going to start off with this one today so i already have it built so i can show you guys what it does so i actually have it running in my task scheduler here and i have it set to run every five minutes and it verifies my test environment and then it tells me if anything is down or not so i'm going to go ahead and actually turn off one of my virtual machines and i'm going to run this task and just show you guys what happens when that happens so once the task is running it actually checks my environment and then sends me an email saying that this machine is offline and it tells me at what date it is offline and now it actually does do some other things so in five minutes when it runs again it will not resend me the same email but it will send me an email tomorrow saying that this machine is still offline if it's still offline and if it comes online it will actually send me an email saying that it's come online so it it does track all those different changes now since it's only a beginner project i've kept it to simply a csv file so we're not using a database or anything like that uh which that would be i would say more intermediate and advanced level depending on how into it you really get into it um but we're going to be starting with the csv file and then eventually once we get into the intermediate and advanced projects this is probably one that's going to keep following along and we're going to upgrade the project as we go so in the outline that i had given it was pretty much just check to see if computers uh which server website is reachable by ping and then log all the results and send email if any outages so we're going to be doing this and we're going to be doing a little bit extra it's really just going to be doing these two things but we're going to have intelligence in it so it doesn't just spam email people saying that servers are offline because that would be kind of annoying so what we're first going to want to do is i'm in this folder here and we are just going to create a script called environment checker dot ps1 and we're going to go ahead and create another file called nth checker list dot csv and this is going to be our csv file containing the server names so what i'm going to first do is populate this csv file with the server names that i have so what we're going to first do is create some columns so we're going to do server name and we are going to go ahead and put in the server names that i have which are going to be these two here so we have jp win 10 and see and then jp hype 2019 so we have these two servers now we're going to start our environment checker so what we're going to first do is we're going to go ahead and reference this environment checker list so we're going to do server list file path and we are going to set that to the file path which for me is actually going to be c backslash users administrator desktop then ps beginner projects and environment checker list csv we're just going to wrap that in double quotes and then we're going to bring that in and call it server list we're going to do an import csv and we're going to point our path to the server list file path and if we run that we are going to then see that we do get our server names so what we're going to want to do is we're going to first check to see if we could successfully ping the two servers so what we're going to do is we're going to do a 4 each and then we're going to do a for each server in server list now there is a connection a command lit for basically a ping in powershell now i have noticed that um issues using the commandlet was powershell of five so there might be some issues there it does work sometimes but i've noticed more than not i usually do have issues so i am using powershell 7.2 currently so let's do a test dash connection and we are going to pass in our target name which is going to be our server dot server name now we are going to go ahead and we are just going to put a count of one i only want to do one ping to the server and let's just see what that gives us all right so as we can see we ping the jp win10c which is offline currently and the jp hype 2019 which is online and it returns to me the ip address and the status of successful so that is really good but we don't really know what it was last time so this is where we're going to go back into our csv file and we are going to go ahead and add a last status and let's go back into our file here so what we're going to want to do is we're going to want to store this test connection in a variable called connection and what i like to do as well is at the top here before we grab the connection is let's create a variable called server name and we're going to store that using server.server name and we're also going to store the last status using the server dot last status okay perfect and then what we're going to do is we're going to do a if connection and we can see that it is a status we're going to do if connection dot status is equal to success we are going to do a write output server name is online and then we're going to have the else here and we are going to set that to server is offline all right perfect so now if we go ahead and run this here we can see that we have jp 10 when c is offline jp hype 2019 is online so we know that one's offline one is online now we don't really know if the status again has changed because we aren't using that last status anywhere nor are we updating the csv so what we're going to want to do is we are going to want to first update the last status at the end here so what we're going to want to do is do a server dot last status is equal connection dot status and then what we can do after the for each loop is or actually what we need to do is before the for each loop we're going to want to do a export equals and then we're going to create a array list here so we're going to create a system dot collections dot array list and we're going to create that as an empty arraylist then after we set the status we are going to go ahead and export dot add and we are going to add that server and we're just going to put our square bracket void here just to not have any outputs and then what we're going to have here is just export is going to be pipe to export csv path we're going to put in our server list file path our delimiter is going to be a comma and then we're going to do a node type information and then the import we are also just going to specify our delimiter here perfect so once we have this here so right now we only have our server name and our last status if i reopen that we can see that it's only the server names that are in there and now if we run our code it should actually input us our last status so here we have last status and that we have last status was destination host unreachable and last status for the hype 2019 is success so this is what we wanted to see because we want to be able to compare the last status to what it is right now so now what we're going to do here is if so inside the first check here so if the server is online what should we check for so what we're going to be checking for is going to be if the server if the last status of the server was not online so if the last status is not equal to success then we are going to put in the i'm going to erase that so inside this if statement we are going to go ahead and put that the server is now online otherwise it's always been online so we can just do a write output server name is still online take that out here and we can take this out actually as well perfect so now if we run this here we will see that the jp hype 2019 is still online and now if i go ahead and turn on my vm here to trigger this event of now being online it should be coming up any moment now and then i'll be running the code all right so it should be up now so if i run this once again still showing offline let me just run it once more so here we have jpwin10c is now online jp hype 2019 is still online if we go into our checker our csv we can now see that both are successful so that is good so now we know that our next thing that we want to do is what if a server goes offline we already know that we're going to get the notification that it's offline but what we want to do is we want to know if it's already been offline because we don't want to send an email or send an alert to the screen that it's offline every time that it's offline it's kind of a lot um to do so what we're going to make sure of that is we only want it to send us an alert saying that's offline if the last status was a success so what we're going to do is we're going to do a if last status is equal to success we are going to then do the right output of the server is now offline then else we are gonna write that the server is still offline because we know if it's not success it was obviously a failed so it is still offline nothing has changed so if we run the code again we're going to see that they are both still online now if i go ahead and turn off that virtual machine and we run the code we are now going to see that jpwin10c is now offline gp hype 2019 is still online and then if we run it again we will see that jp win10c is still offline and jp hype 2019 is still online so no status has changed now what we might want to do in this case um this is what i've thought of at this point was now it's nice that we get the alert that once it's offline what if i want to know if it's been offline for a long period of time so i don't necessarily want to just get an alert saying that it's still offline um and neither do i want an alert saying that it's still online so we already know that we're not really going to be doing anything if the server is still online uh so that we're going to get rid of that else statement because we don't really care about that but we do care for both cases of if it's offline if it's newly offline or if it is still offline so what we're going to want to do is check how long it's been offline for now what i like to do is i actually like to add two columns to my n checker list here so we have last status i'm going to put in last check time and then down since and at the same time we are going to put last alert time or last down alert time so these columns are going to be the last check time so that's going to keep track of the previous time that we've checked the down since is going to be the time that the server has been down since the first time that it's been noticed that it was down and then the last down alert will keep track of the last time that we sent an alert to the screen or by email of this server being offline so we're going to save that and then what we're going to want to do is we're going to want to of course create these variables again here so we're going to put a uh down synth here and we're gonna do a server dot down since and then we are also going to want to put a last down alert is going to be equal to the server last down alert okay perfect and then what we're going to want to do also is because now we're going to be dealing with the date and time we're going to want to get a date time and we're going to do a get date for that okay perfect so that should all be good so now one the server is online successfully we're not going to be doing anything we know that the server is offline online that should be good for now now once the server is offline for the first time so the last time we see that it was a successful uh connection so we know that it is now officially down so what we're going to do is we're going to do a server dot down since is now going to be equal to date time and then what we're also going to do is the server dot last down that's down alert equals date time all right and then if the server is still online so this is where we are going to want to pick how long we want these checks for so what i'm going to do here is i'm just going to pretend that the jp win 10c had a successful last time so let's go ahead and let's just check what this does for us okay so we did get an error here last down alert last down alert time okay last down last down alert time and last down alert time okay so i think that that should be good let me just do success here once again so let's just run this code here and let's see what it gives us we have the gp win10c is now offline and we see that we get a jp win10c destination host unreachable the last check time we don't have anything for because we haven't set that yet but we do know that it's been down since uh march 23rd at 8 57 pm and the last alert time was also march 23rd 8 57 um p.m so let's go ahead and let's put in at the bottom here where we do the last server we're gonna the last status we're gonna do the last uh check time equals date time and that will automatically add that date time to every entry so we're always updating that we're always updating the status and we're always updating the date time so once we have all of that so currently we are going through each of our servers and then we are checking to see if the connection is up if the connection is up and the last time it was not up we are now seeing that it is online otherwise if it's down and it was up last time we are saying that it is now offline and if it was down last time we are saying that it is still offline so that should be pretty easy to follow along a little bit there so now what we're going to want to do is if it was down the last time it's checked for we're going to want to check how long it's been down for and since when was the last alert so what we're going to do is we're going to do a down 4 and this is going to store the amount of time that it's been down for so what we're going to do is we're going to do a dollar sign parentheses we're going to be doing a get date and then we're going to do a parameter of date and for this we're going to put date time which is going to be the current time that the script is running and we're going to do that minus the parentheses here and we're going to do a get date date and we're going to put in the down sense and we're going gonna put that or total [Music] total days it's still offline or down for days and then we are also going to want to get since how much time has passed since we last alerted so since last down alert is going to be equal to the dollar sign parentheses and here we're going to be doing basically the same calculation here the only thing is it's not going to be the downtime it's going to be the last down alert and we're going to grab the total days here again for us all right and output and we're just going to do another output it has been since last down alert days since last all right so now let's go ahead and let's run this now we should see that okay so we see that it has been 0.002 days since the last alert and jp win10c has been offline for 0.0 days because it's just been put offline so what i'm actually going to do is just slightly manipulate this and for our down since i'm going to change the date from march 23rd to march 21st and i'm also going to change the last alert time to march 22nd so this will have been a couple days now so now if we run this code we see that has been one day since the last alert jpwin10c is still offline for 2.003 free days so we can see that it's been offline for a while now this is the time that we would want these alerts i prefer them once a day i don't want these alerts to be coming in every five minutes if i get an alert every day saying that the machine is still offline that's probably fine for me so i don't get spammed but you can definitely do this as often as you really want so what we're going to want to do is do an if statement so once we get these two dates here what we're gonna do is an if and then we're gonna do a parentheses here now we're gonna do the down four so we want the down four to at least be greater or equal to one day so we only want to get alerted of a second time at least if it's been one day that it's been down for and we also want since the last down alert we only want every alert every day so again great or equal to one and then we're going to have this if statement do that so this will tell us if it's been alert so this will be if it's greater than one day so if we run this we should get those same alerts which that is perfectly fine that's awesome and then what we're going to want to do is at the same time because this is alerting us is we're gonna do the server dot last down alert time equals date time so if we run this now again we will get the alert and now if we run it one last time we should not get any output here because it hasn't been if we go back into our file our last alert time is now today's date even though it's been down since the 21st we just alerted the user on the panel uh so we don't need to do anything it just skips it and now if we go ahead and do that should actually be good it's like the last alert time so yes that should be good so now the only thing is in theory at this point now what we need to do is if it's a success but it was not a success before we do have to reset these values so we need to do a server dot down since equals null and then the server dot last down alert time is going to be null as well okay and that's just to clear out those variables because we don't want them down if the server is actually up we don't want to have those variables stored in there so that should be good so now this is a full script that is definitely seems to be working so let me just test it one last time here we should not get any output now if i go ahead and turn on the vm let me just turn that on so it's just taking a little bit of time to turn on all right it is on so now if i run this so we can see that the last time it was unreachable now if we run this we have jp win10c is now online we don't have any of those variables anymore which is perfect and if we run it again they're both online still so that is perfectly fine and then if we turn off the vm and we run it again we can see that jpwin10c is now offline because it's now switched from online to offline and now we get nothing because it has the last alert time was a couple minutes ago and it's been down since just the 23rd so that is a okay everything is good so now what we want to do is we want to add a little bit more functionality because it's nice that we get these alerts on the screen but what we really want is we want these emails so what i'm going to be doing is i'm going to be using the mail module that we have built on this channel for this video today so what i'm going to be doing is i'm going to be doing an import module and we're going to be importing the module from c preps send email mail module just make sure that works yes all right so now we have this mail module now i'm going to be putting a link in the description down below to the video where we have built this mail module it is a very very useful mail module that replaces the send mail message commandlet since that commandlet is not recommended to use by microsoft they have not replaced it themselves yet um that being said it does still work so you can use it i just not really recommend it's not secure but any other mail api you can definitely use in lieu of this this is just simply going to be a full example that you can follow just using this channel so let's go ahead and let's move on with the mail module here so with the mail module as we've known from the previous video what we need is we need a mail account a port an smtp server a from address and a to address so what we're going to do is we're going to do a mail account and i'm going to do a import cli xml so you guys don't see the password for it and we're going to put the path of c scripts and email outlook.xml now i cover this import dashcly xml again in that mail module video i also have a link to just the cli xml video in there as well for six securely importing your password without putting them into plain text which is going to be very useful if you are putting scripts on servers and you don't want people to have access to those passwords so we're going to have the mail port here and our mail port here is going to be 587 for outlook and then our mail smtp server is going to be smtp.mail.com our mail from is actually going to be our mail account dot username and then our mail two i'm going to be sending that to tessjacked programmer outlook.com now i do have my contact info on my youtube channel do not send it to the testjackprogrammer outlook.com i do masterly emails on that account because it's the one i test with all the time all right so once we have all these variables in here what we're going to want to do is we're going to want to go ahead and create another variable and that variable is going to be just here and we're going to name it alert and we're going to set it by default to false in every situation and then what we're going to want to do is now to be server so once the server is online what we're going to want to do is we're going to do an alert equals true and then the server is now offline we are going to do alert equals true oops equals true and then we are also going to do that the alert equals true here this is just going to tell me where i need to put the alerts and this is the only time that the alerts gets triggered so what we're going to do is we're going to create a variable called subject every time that we have an alert and we're going to put that subject to be pretty obvious here so we're going to put server name is now online and exclamation mark just to make it slightly different from the one that i already have running and we're gonna have a body now i'm gonna make my body an html body you guys don't have to do this um i just like it because it does look a lot nicer at the end of the day uh in your email so for the body the first thing i'm going to do is i'm going to do a pair of h2 tags so we're going to do an open and close h2 tag and we're just going to put in here server name is now off uh is now online and then what we're going to do is we're going to do another body and we're going to do a plus equals we're going to do a pair of paragraph tags we're going to do a server name is now online at date time okay perfect and we are going to go ahead and copy that subject and body to the now offline but we're just going to change the wording for now offline now offline now offline and same thing for this here but this one we're gonna do is is still offline or down for days and we are going to say is still offline actually has been offline for down four days and we are going to go ahead and has been offline we're going to do a sense here we're going to do a down fence so that will tell us how long it's been down for now at the bottom here so before we update our server object what we're going to do is we're going to do a if alert so if the alert variable is true what we're going to do is we're going to do a send mail kit message from we are going to put a our mail from r2 we're gonna put our mail to smtp server we're gonna put our mail smtp our port we're gonna put our mail port our subject is going to be subject body is going to be body and we're going to make sure that we do the body as html and then the credential for that is we're going to pass in the mail account so that should work so now if i trigger this i don't believe that anything has changed so what we're going to do is we're going to turn on our virtual machine here what i'm going to do is i'm just going to go ahead and disable this task here and let us just delete all of these so the virtual machine is still booting up here so let's run this code so we have jp win10c is now online we get the output of an email being sent so here we have it jp win10c is now online at 9 21. um the server time unfortunately is slightly off i have not fixed that here on my end but you can see that it is now online and now if we go ahead and we just now it's online so now let's go ahead and let's turn it back offline and let's run this code again we're going to get another email saying that it's offline we should see that very very shortly so here it is is now offline and now if i run the code again we should not get any alerts because it is still been offline there's nothing that's changed and now if we go ahead and just change the down since so let's change the down since since the 21st and let's do the last alert time since the 21st as well and we go ahead and save that and we run our code once again you see that it's been triggered here and we get that jp win10c has been offline for two point a bunch of days what i would probably do in this case instead of using total days is use just days that would be probably a better solution and just a little bit cleaner so if we go ahead and just change that last alert time to one day previous and run that again has been offline for just two days and has been offline since the 21st so everything like that is seeming to be perfectly working so everything is working here now so what i would want to do at this point once i have this environment checker and it's running off of a file so i can always add more servers to this or remove servers as it sees fit the only thing that you would really have to do is just put in the server name you don't even need to put the quotes so let's just say as if we didn't even have the jp hype we go ahead we put that in there it's going to go ahead check the file and check it and now it's going to see oh we have a new server added in jp hype it's now online it's going to send you an email for that actually so which is pretty cool i find that's a pretty useful so any servers that you add will automatically send you an email just wait for that email to come in and if it has not come in we need to go and see why that is okay actually still running here so it might just be taking a little bit at times not sure if my powershell window is actually frozen or not here it seems that encountered some issues so let's just run that again here jp hype is now online and there you go it did send the email this time and we get that jp hype is now online so it just seemed to have been a little bit of a visual studio code uh visual studio um visual studio code bug there that had happened i've noticed that happens sometimes with powershell the only thing that you need to do is just force quit your script with ctrl c and then launch it again and it seems to work just fine so as you can see we added a server it's now online if we run it again or if we just look at the file we can actually see that it automatically added those values for us we can run it again and we see everything is good if i turn on my other virtual machine and we will just do one last little test here just waiting for that vm to start now what i would do um while we're just waiting for that vm to start here what i would like to do is i really like to set these as scheduled tasks so what we can do here is we can just delete this one and what we're going to do is we're going to go ahead and create the task because the vm just came online but what we're going to do is we're going to run the script as a task just so you guys can see what happens so make sure that if you are using that import cli xml make sure that you're running as the user that has access to that cli xml on the machine that has access to that by xml because it is hashed um as the user so what we're going to do is we're going to do an end checker here as the name and we're going to do run whether the user is logged in or not so we want it to run every five minutes i don't want it to check to make sure that i'm logged in i want it to run regardless and then the triggers here we're going to put daily and you can set that you can leave this really almost by default but we're going to repeat the task and we're going to repeat the task every five minutes now you can do this every two minutes um it doesn't really matter as long as you give it enough time to actually execute completely and then it can start back up and then so that should be good and then the actions here what i like to do is i just like to browse for the script so what we have here is ps beginner projects environment checker and then what you would put in front here is just pwsa space dash file space now this is going to run powershell 7. if you are running powershell just 5 you would just do powershell here but because we coded it in powershell 7 we've tested it with powershell 7. i'm going to run it with powershell 7 which is the pwsh dash file we're going to click ok it's going to give you a it appears as though arguments have been included in the program text box do you want to run the following program psp wsh was the following arguments click on yes click on ok it'll prompt you for the password to the to the account that you've selected and now if we go ahead and we click on run we should see a jp win 10c is now online so everything is working correctly so that would be an environment checker now the way that i've made it with the email might not be super beginner um but until the part of the email uh it should have been fairly easy um there was no real new commandlets uh some very basic logic checking so if you weren't able to get it just maybe go back look over the video and see maybe what parts you did not understand if you guys have any questions you guys want me to clarify anything let me down know down below in the comments and i will try to clarify things up for you guys but this should be the environment checker this could work in a production environment uh it's not super intrusive uh so you guys can definitely go ahead and use this this will be in my github right after this video release so we can actually change that to server name because we don't we actually assign it so that should be it in the next video we're going to be looking at the service checker um so that one i'm seeing that this video is actually running quite long i might start splitting these up a little bit because i don't like creating long videos i find myself i find that i kind of lose interest in long videos so i might split these up a little bit because these definitely do take almost an hour of time to explain um so let me know if you guys have any comments or questions or questions that you guys would like to see a video on down below in the comments hit that like button that subscribe and hit that notification bell to be notified when that next video comes out and i will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 4,292
Rating: undefined out of 5
Keywords: powershell basics, powershell, windows powershell, programming, coding, scripting, powershell scripting, powershell scripting tutorial, windows forms, powershell tutorial, email with powershell, send email with powershell, powershell email, send-mailmessage, beginner project, heartbeat powershell, automation, environment checker, powershell beginner project
Id: aBV4gmdcpjc
Channel Id: undefined
Length: 50min 36sec (3036 seconds)
Published: Mon Apr 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.