PowerShell Functions and Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] languages like Powershell are a tool for Automation and the concept behind automation is doing something that you do over and over again and making it so that it's automated uh functions are a key part to that if you start doing something over and over again you write the same code it might make sense to put it into a function and use that we're going to discuss using functions in this video and my tools will be Visual Studio code inside of a Linux environment yes this is repetitive I say this all the time I'm doing this in Linux to show that while Powershell is renowned for a system administration tool on Windows it is open source in Linux and mac and you can use it just as much you can use it natively inside your most Linux and Mac flavors I'm going to show it so hopefully it increases your knowledge of how to use Powershell everywhere let's discuss using functions basic principle behind a function if you're going to execute code more than once use a function it's reusable code that's really the principle is reusable code um yeah so here is a function in a nutshell basic principle you declare the word function you give it a name best practice is to follow the commandlet concept verb noun it's highly recommended you don't have to follow that then you just put in parentheses your variables that you want to pass in you don't have to have variables they're not required but if you're going to put if you need if you want to be able to pass in contact variable content content to your function that's where you'll put it then you put your squiggly brackets you'll do your code and then you end your squiggly brackets so let's go put this in Visual Studio code and walk through the elements so the first thing we did I'm going to put I'm going to take the variables out and see that this still is fine the reason we get a nice little squiggly here it's going to tell you that it's using an unapproved verb display is not one of the 27 verbs that Powershell uses it's okay you don't have to um if you start if you start making a lot of code and lots of function you start passing around it might help to make your commandlets followed basically you're going to find that this function becomes just like a commandlet and so it helps to follow the same naming schemes that Powershell uses but it's not it's not essential and then everything that you're going to do goes between these two squiggly brackets and then the syntax to call it you put your functions up at the top usually as a general rule your functions go at the very top of your Powershell it's you can put them at the bottom but it's really usually general practice is that I'll go up at the top and if you have multiple functions you'll just write them I could I'm just going to copy this function but um and I would have say display text one and I could just keep on passing on functions down the line and then ultimately what I want Powershell to do I write down below it and the way you call a function you just call its name so I've got this thing's called display text I just call display text I could if I want to call the next one I use text one and that'll call this function all right so we need to make sure that we put back in here we call our variables we know that we want to pass in two little things when passing two strings so I'm going to type in on a parentheses variable one variable two horrible horrible parameter names you should probably give a lot better definite description to them but for the principle of what we're doing here we're going to leave that alone and we'll we'll copy this again we're actually let's just get rid of this function no need to have it and so now what we're going to do variable one and variable two can be used in the function and we're going to so what we're going to do we'll call display text and we'll pass in hello and readers but remember this is much like a a commandlet so if I have this written right I can actually go tack and notice it notice knows there's a variable one and I'll put a tack over here and it knows there's a variable and variable two so it's I can pass it in that way um it's not required that I put that in there but for this it makes it makes a little more clear that the relationship between it and commandlets and if I run this I now get back hello and readers this hello goes here this readers goes to variable two that is a function in its most basic format um you could you don't have to have it right host you could have it do some math in there change some things around etc etc so now let's talk about these parameters you can choose to make the parameters mandatory or not mandatory and the principle there is you've got your we're going to pass in underneath the display text we're going to put in our parameters so this is a slightly different way of putting your parameters in and I'm going to we can we'll make this go away for now just show this instead of putting this little parentheses this little uh uh parentheses appearancing variable one variable this is not a very this is definitely not best practice you want to have a little more control over your parameters so what you'll want to do is actually inside your squiggly brackets we're going to declare this little thing called param and then we call out parameter in square brackets and then we put in parentheses mandatory equals true or mandatory equals false I'm going to create a brand new variable so you can see this in practice if you have more than one they need to be separated by a comma the very last one does not have a comma so I'm going to put a comma here and I'm going to put make my own parameter so we put brackets parameter and then I put a parenthesis and I'm going to say mandatory equals false this is not mandatory and we're going to call it my variable and that's as simple as it is and we're going to write put something here write host third variable okay and we'll get rid of this display text here we're going to get rid of this function we don't need that anymore we have a brand new function where we're actually putting stuff in let's take this out so the first time I'm going to go put in all of my my values so I went display text and I put in the computer name with attack I could put attack here and say message well that one my variable unfortunately tab message there we go and then tack my variable and we could pass them that way let's go watch it work and third so first variable my computer name my message hello world I'm now gonna I said this is not required so I'm going to erase it I said mandatory equals false clear this up run the run the command third it's it's still going to try to run this line but now my variable is a blank and so it just writes third variable and nothing in there but it does not air so it will handle this just fine now if we take out my message it's also not required it's not mandatory clear this out run the command and we notice now we're missing another variable but it's still fine this computer name I'm going to just take it on out I'm just going to write the command display text hit clear if I run this it's going to come back and tell me uh nope you need to supply a computer name field and if I press enter it's still gonna it's it's going to take that and press push it in as a blank field because I pressed enter if I run it again and I leave it blank I'm going to put some computer in there and it's going to pass that in and so when you make them mandatory it will then prompt you if you don't if the person doesn't Supply the field and so that's a really nice feature there I want to show one more we're going to do a new function new new text file I'm going to do a Powershell select a language I'm going to Powershell and we're going to do a function I'm going to call it add to numbers and we're going to put brackets now we're going to put a parenthesis param and so we follow that syntax we got param there cram it's not my bad param and then we put blocks like that if we follow the syntax then we're going to write our parameter name right there parameter I'm going to call out um it's mandatory equals true and we've now done that we've finished our block so now we call we give the variable I'm going to put first number I'm going to do a comma because I'm going to do it again parameter [Music] Tory equals false and we're going to do second number and if we look what do we miss forgot a bracket bracket my bad C it's a general rule once I do it once I just copy and paste saves me some time so parameter goes in Brackets and the mandatory field goes in parentheses and I actually want these both to be true and now I'm going to write down here right host parameter first number Plus second number and so what we're going to do is we're going to do a simple little math program it's going to take two values and they're going to add them together so let's try that add to number first number is going to be 1. second number is going to be 2. and let's write that um well just this is actually going to have an unintentional bug and I just want to show this to you this is not going to do what we expected to do let's see what actually happens if I hit clear gonna run run it and what ended up writing one plus two but but I was supposed to do math on that all it did is write those things in there and what happened is Powershell was going to try to guess it variables are not by default written in there it tries to best determination what type of variable it is and it decided these were strings now unfortunately when you add strings together the is what you get so what we want to do is we can add one more thing and type cast I'm going to cast it as an integer and say you are I'm going to force this and say by putting this bracket in bracket in these are integers they are not strings and now if I clear and I run the same I don't have to run I can just run the command here we will get actually let's try this okay so that actually my bad we're going to do this here um we're going to do one first number Plus number we don't want to write host right host is going to definitely put that as a string no matter what I do and so if we run this we now get three if I remove the int and now these will it'll decide try to do its best and it's going to interpret it as a string instead of a variable we're going to find sometimes you get um we got a three here as well just need to be careful though depends on how they write them in what if I go and so basic principle you don't want to leave when you're passing things in up to the computer decide what to do my recommendation is to always type gas especially when you got numbers so that you make sure they come in correctly you don't want it to accidentally think it was a string and do something weird with it um yeah so that's that's the general principle behind a function you use the param you can then type in your parameters with brackets and parentheses mandatory is true or false nice thing is here if I make this false I do faults again we'll just re-emphasize this if I run this command clear this out and let's go put the type casting in there and tight casting and run this and now I'm going to remove that just run it it's going to tell me that I don't need those didn't do me very good it wasn't very good now if I make it true that it's required see that's going to mess up your math but if you make it true if you run this command let's clear this out what's your first number you want to supply we're going to give it a one I'm going to give it a two I got math if I run this again and I do one and I press a cannot recognize a as a system 132 due to format error I'm now telling it nope you're going to give me a number and so I'm going to now put 2 in and we're good to go another reason to type cast anyway I hope this was helpful um my last little thing I want to cover with functions you can pass them in use them all sorts of different ways it's you can use them later on it's so the concept behind a function do you want to reuse the codes you'll often want to pass that information back out so I'm going to just show this in an example I've got a function create a warning message I'm going to make a variable called my error and I'm going to write this as a warning message and then I'm going to write that to this and so this by doing it this route I can call it later and you so I've got create warning message it's going to go ahead it's going to make a warning message and that my error is going to get passed down this pipe and write warning is a command that allows you to change it'll change the format this will come across yellow it'll have some different formatting um it's a it's a command that right warning that you can use um but it will be you can pass this variable into the pipe and down the line create message here we can use the return command and send back the string so create message it'll go in it'll run this it'll say hey return my return message so that'll write to string right to the screen function create message two I can also just use write output and write stuff to the screen so we'll watch all three of those in action if I run this command first thing we get warning this is my warning message the reason it came across a warning is we pass it into this little commandlet that says right warning and it's it's a nice little function of in of itself that puts a warning on there turns it yellow um Etc then we get the create message which writes my return message and the last one my right output message and there it is the screen so there are three different ways that you can return values back from a function using variables here and pass it in use the return command or write output and again you can pass variables this was a string but you could pass you could do like return and write my error in there if you wanted to if that was part of the syntax anyway lots of different options uh functions they repeat the code if you're going to do something over and over again put in a function you can return the values back it's a powerful feature I recommend if you're going to start automating with Powershell functions are the way to go I hope this helps you as you start to use Powershell at home and in your at work if you like this video give me a big thumbs up I'd love it if you subscribe to the channel I continue to plan on putting out videos as they relate to log analysis and security automation things like that if you subscribe to the channel you'll get the latest videos as they come out and I hope to see you I hope you'll watch more of my videos
Info
Channel: Lame Creations
Views: 628
Rating: undefined out of 5
Keywords:
Id: bk0c3DBpwDM
Channel Id: undefined
Length: 19min 52sec (1192 seconds)
Published: Thu Dec 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.