Sending an Email with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on everybody this is a quick brief tutorial on how to send out an email from powershell um powershell is really powerful it's used for automation and different uh you know skills and different things that you use and you can add this video to your arsenal and you know kind of get more familiar with how powershell works and different things of that nature so i thought this is really cool skill to have and you know something that you might find useful down the road if you know if you want to send out an email to you know one of your friends or your email itself um this is what this video is kind of for so i'll walk you through how the script works um what i'm doing and if you've never used powershell before or the powershell environment that i'll be using um this might be helpful just in the terms or in the sense of uh getting more familiar with that platform so let's kind of start off this is my desktop here let's start off with some of the prerequisites you're going to need so as you can see here i've already was able to deploy the script and to run the script from powershell and this is an actual email that i sent from the powershell environment so this is the title hello from powershell this is the email address that i sent it to which is my email address and then this is the body of the text which is this is a test so all this was sent from powershell itself so let me go and get a drink here and we'll go ahead and get started okay cool um so to kind of just give you an idea of the prereqs that we want to make sure that we go take care of before we um get started on this is you want to go to this url link here which is myaccount.google.com forward slash less secure apps and what this does it's basically telling google to allow third-party apps to send information or or to uh you know kind of do something in regards to bypassing that parameter that google sets up you know google uh your google account your gmail uh sets up to block certain things from entering in so in a way it's kind of like a firewall feature but we're only turning this on simply for the purpose of uh throwing or putting this email um into production so i would recommend turning this off but when you do come to this website or this link you will see that this is off by default um i don't believe that you would ever see it on for any reason unless you determined or or set it on but um once you come here you're going to want to turn this on so we're going to have it on and we are going to turn this off um when you're done so i highly recommend turning it off so again the end goal is to have an email sent from powershell so this is pretty much all we got to do is go to this emailmyaccount.google.com forward slash less secure apps and turn this on that's really all you have to do on that part so now let's start with powershell itself so if you haven't never used powershell we do have two different platforms you have the powershell command prompt i'm going to open that up just to give you an idea so if you came here and let's say you wanted to use the get content command or or whatever other command that you wanted to use you can use it in this prompt and by all means you could actually write this whole script within the prompt itself highly don't recommend that and that's why we're going to be using the other platform which is powershell ise so we're going to type in powershell ise and we're going to run this as an administrator iec stands for integrated scripting environment just in case you're wondering the acronym now when we're in this interface we're getting practically the same thing only with a lot more features we can save our script we can open up some tools options you can change your font size i don't know if you'd ever want this but you can make it like 22 or 24. i guess if you're really not able to see the font at all then you might want to make that smaller or bigger um so i'm going to make the font size 10 apply go back to nine i like nine whoops why not eight i think 10's okay actually let's keep it on whoop come on now not spend too much time on this apply apply okay cool okay so um for the purpose of this video i actually think it's really neat when you hit control and then use the minus and plus button so i'm actually hitting i'm holding down the control button on my keyboard and i'm clicking on the subtract and plus button plus would zoom in and then minus would zoom out so we'll make this a little bigger by zooming in zooming out so if you really don't want to mess with your font too much you don't really have to because you can make it bigger in those terms um now it kind of gets you more familiar with this interface if you were to click uh up here top right script the script panel script script pane i think they call it um this will bring you into the prompt itself so if you were to come here to powershell you would see uh this prompt in this prompt here now they're practically the same thing only that this is being directed to the system32 file directory instead of it being directed to your user profile which in my case is casey um really the only difference so actually i want to change that we're going to change directory with the matilda key and we're going to be brought back to the default directory of my actual profile itself which is nested in my c drive so that's why we see c and then backslash users and whatnot so this is all having to do with your c drive i'm sorry your file directory location so we're gonna again if you drop this down drop it up you can get the um scripting platform interface and then you'll have the prompt command prompt here itself powershell itself but by all means in you know intents and purposes you're gonna wanna make sure that you use the scripting portion of this but if you did want to maybe change the style you can have more of a vertical look as opposed to the horizontal i'm more of a bigger fan of the horizontal look but you can make it into a vertical look if you want it and of course you can just make it the script interface itself without the command prompt even being there so i'm actually going to drop this down just a little because we're not really going to be using that until towards the end so let's get started with the actual script itself so the first thing we're going to do is create a function we're going to call it send email my keyboard is kind of funny sometimes i'll be writing something and uh it just simply won't take the key so hopefully we won't have too many of the too many of those issues with this video um so what we're doing i'm just kind of writing it out and kind of going along um you're not familiar with the syntax this is a curly bracket this is a function we're actually creating the function here we're opening up the parameters as well we're actually calling this a function of the or this portion of the script parameter um let's add in a bracket this is just a normal bracket we're going to call it parameter we're going to open up another um bracket brace there mandatory then we're going to set it to a boolean of true and overall what i am doing is i'm creating the structure of the body the subject and the um the two uh parameters of like two as in like who you're sending it to you know like email you're sending it to this is all just kind of laying out the structure of the email itself okay so what we'll do here is we're gonna create a string um string i'm gonna make this yep string that's okay i was making sure that don't make it capital um and then we're going to set our variables so we're going to set oops variables are set with a dollar sign two capital now something that's really cool is that you can come here let me drop that down you're going to come here and you can at the end of this whole entire layout um format you can copy that drop down to the next line and then paste because we are going to make three more or two more um and then you could add more information without having to draw it all out which i find really great it's it's definitely convenient because you don't want to have to keep writing out the same information over and over if you really don't have to especially when it comes to scripting it can be very overwhelming but we're just going to neaten this up a little bit make it a little bit tighter clean it up i'm a big fan of cleaning up your script as you go now what we're going to do here is we're going to create a comment which is basically just letting whoever uh stumbles across this script um whoops spell that this is what this portion of the script is doing so they're gonna come here they're gonna see that there is a username this is a variable and i'm gonna actually um just write this uh portion of the script out and i'll explain what it's doing here in just a minute so just kind of follow along here and let me come over here and grab the location i need boom we're gonna close that make sure that looks good and then we're going to set my location of the data that it will be searching for oh whoops forgot to close that there we go um and again if you come here hit copy go down paste and then you can change this to password so these are credentials this is what we're doing we're actually creating um a variable that says hey go grab my credentials off a text file within my file directory so and i'll show you that it is definitely working or it should be working if we come here and we click f8 i should see casey ingram 2012 gmail.com which we do so in the command prompt you see casey ingram 2012 at gmail.com and that is basically saying that this indicator is finding information within my text file so if you were to come here and let's say you opened up a text file notepad and you typed in uh hello uh world uh test i love pizza and this would be your zero object location your one your two so of course for all intents and purposes i do not want to exploit or expose my um uh password so if i was to click f8 this would show my password to my email which we don't want to have happen so what you'd want to do is you'd want to come here you'd want to go to your file directory and you'd want to find a place to nest in a text file i'm here text file uh you can just i'll just call this gmail because i already have a gmail.creds come here open that up it's opening up on my second screen and then just type in example gmail.com pass word 123. and this could be your credentials that you would use to log into your uh email account so you come here and you would save it and then you would do exactly what i did i'm not going to save that i'm going to actually delete this delete and you would come back here you would find this path you would simply come here i already deleted it but you come here you find the path and then you would add a backslash and then add actually add uh the name of the path there the name of the text file so i'm gonna close that out we don't need that anymore but that's our two variables that we need for our credentials for this portion of the script so the next thing we're gonna do is we're gonna create the object structure of the actual uh script itself or of the email itself the body the um the subject the uh the two the header of where the email is sending to um so yeah so we're gonna we'll just say create object structure i assume object structure that's just the best way to explain it in my opinion um all right so here another variable um we're going to create an object here email equal to at early brackets drop that down from we're gonna set from equal to username username 2 equal to 2 subject equal to [Music] subject cool cool smtp server smtp stands for simple mail transfer protocol equal to smtp and then you want to make sure to declare what email address the smtp server parameters is going to follow which in this case is gmail body equal to at body and then credential make sure i spell this right it's very important i spell it right equal to this parts a new object that we're going to create um system management dot automation automation not omission hold on automation there we go uh ps credential argue argument list and then we want to set our username and our password as the parameters within that purpose now this right here ssl it stands for secure socket layer ssl was deprecated years ago and tls is definitely the more desirable pick when it comes to http security because when you're sending data in transit ssl tends to send out that information in clear text especially if someone is used or a threat actor was to use like a protocol analyzer they can see that information through your packets it's exactly why we're running the path parameters up here uh because we don't want the clear text to show out um within uh the parameters that you're setting up especially when deploying only creating a variable because you have also shoulder surfers who can basically just run a git history command on your powershell and they can see all the history and they'll see clear text so if you came in here and you actually deleted that and then just put in your actual username like for instance casey ingram 2012 2012 gmail.com if you did that it'll work there's no doubt it won't work um but for all intents and purposes we definitely want to do that um we don't want to send out anything in cleartext that's not a good idea and um me being in the security field i definitely highly recommend not doing that so ssl at true but for the purpose of this video we are going to use ssl in order to send out this um email from powershell but it will not be in any way uh a risk towards me because i put it in a file directory that a hacker would not have any access to anyway so we'll set that to true these are booleans we're setting those to true um let's screw it clean up that code a little bit there and of course the last part of this script we're going to send mail message at email and we should be able to hit play and then see let's see we get oh hold on where's this saying that's that oh okay oops so that shouldn't be there make sure it looks good okay all right so um now at this point so what i did was i ran the script you can come here you can come here and click the play button or you can um click f5 it really just depends on what you know what you want to do there but you know i kind of come up here and just click play now here in the um command prop itself we do need to put in some code but we're going to actually call the function here and then let's create two and then we're going to send this so this is this is what we're doing we're actually sending the email at this point so 2012 at this is who i'm sending it to which i want to go to me because i want to be able to validate i got the email dot com uh subjects whoops subject hello from powershell okay then body hello there all right one second here where is it saying that's that object at email at email make sure that looks good to not validate the argument on two is that because these are like this let's try this i know we wrote it out but there must be something i missed so let's play that and then try this again okay so um i already wrote up the script earlier so what i did was there must have been something in the syntax but it's pretty much practically the same thing but there must have been something in the syntax it wasn't liking and i i didn't want to take the time out of this video to try to search for it but um make sure that anytime you're writing out script um you know you kind of watch your periods watch your commas watch your curly brackets all that because you'll just sit there forever and try to figure out you know you're looking for a ghost pretty much but it powershell is good at showing you um hey here's where the error is and like friends let's just i'm gonna give you an example here although i don't like to intentionally have errors but you try to run the script itself and it's saying hey hold on a minute we're seeing here that it's missing so and so at line one or and it'll tell you line seven um it's actually the mistake is that line four i'm trying to find out where it says that line four um line four here we go so line four it's saying we're missing a um comma at line four okay cool we know that because we made the mistake or i intentionally made that mistake then i come back here and this did actually already send the email my phone already lit up because i got my phone linked to my gmail account so we'll come back over here and if you click on inbox on my inbox you will see here that here are the two emails i actually sent it quite a few um earlier but um yeah you'll see here hello there uh hello from powershell um i always like to kind of mess with a little bit come here and just like i sometimes hate when scripting goes wrong what's your thoughts i never said that what's your thoughts and you come here i sometimes hate when scripting goes wrong so this is pretty cool huh i mean you can send um a script right from powershell i'm sorry an email right from powershell using a script so like i said make sure that you come over here and turn this feature off and yeah that's how you would do that so i'm going to leave the description or the script itself in the description of the video and if you like what you see let me know and i'm going to be doing a lot more of these videos this is actually my first video on scripting for powershell but i've been learning a lot about powershell and this is really cool so yeah stay tuned for more videos
Info
Channel: Cyber Casey
Views: 3,631
Rating: undefined out of 5
Keywords:
Id: cJnNv_rDTe4
Channel Id: undefined
Length: 20min 16sec (1216 seconds)
Published: Thu Aug 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.