PowerShell 7 Tutorials for Intermediates #3 : Making your own module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the third video in our Powershell 7.2 for intermediate tutorial Series so in our first two videos we really took a look at creating our own methods in our scripts but there might be times where we actually want to use those methods in multiple scripts or we know that we might use them again in the future similarly to when we built the send email module in one of the previous videos that I released a couple months ago where we might use that in multiple scripts and we don't necessarily want to rewrite those methods all the time this is where creating our own modules will come in handy now we've already seen how to import modules and how to find modules in the beginner Series so in this video we're really going to be focusing on creating our module creating the module manifest and also being able to add the help information in our modules so let's actually go ahead and let's get started so the first thing we're going to want to do here here is we're going to want to create a folder in that folder we're going to name it the name of our module that we want to create so I'm going to create a module called configurations so here we have a folder called configurations and then inside that folder we're going to create a another file here and we're going to name that file again the same name that we want to name our module so we're going to make it configurations and we're going to do a DOT tsm1 all right so here we have our module file so psm1 is a Powershell module file so the first thing that we want to do when we create our modules of course is loaded up with different functions so we've already seen how to create functions or methods so let's go ahead and let's put in function here yet configuration then open and closing curly bracket we're going to go ahead and put the commandlet binding open and close parentheses Ram open and close parentheses and we're just going to write out a very simple commandlet here we're not going to make it a very uh extreme example here so we're just going to make it write the output of get configuration function and let's go ahead and let's just copy paste this function one more time here so we're going to have a get configuration and we're going to have a set configuration we're just going to change the output here so we have our two functions so we're just going to save so again we have our folder we have our module file so then what we actually want to do now now that we have our module we're just going to pretend that we have tons of parameters we have tons of different functions this module would be obviously fairly useless but we've seen in the last video how we can make the methods we can make functions we can put in tons of parameters make those parameters mandatory um and that would make these modules very very useful this module will be just it's really just a set as an example so let's go ahead and let's go back to another file here or you can even do it directly in the console but what we want to do next is actually not going to be inside the module so what we need to do now now that we have our module file and we have our module folder what we need now as a best practice in that Microsoft recommends is to create a module Manifest this will tell Powershell where to grab what files to grab for the module uh who created the module the version information and all sorts of different things you can get really really complex in this module we're just going to be creating a very basic module so what we're going to do here is we're going to do a new Dash module manifest and then here we're gonna have to give it a path so the path that we actually want to give is our configurations folder so let's go ahead and let's copy this path here and then what we actually want to add to it is actually a slash and we're going to again put in the name of our module so configurations Dot psd1 and that is a Powershell data file so it's it's really for the Manifest itself it'll tell Powershell what to do with this module and then the other options that we need to actually set up here is well we don't really need them but they are very useful so we actually have an offer parameter so let's put the offer here as jacked programmer and then we also have a description so let's put in a description here let's say this is the configuration module as you can see there are tons of different options here we could put in a module version which is also another string so let's just put one dot zero dot zero and then the last thing that we absolutely need is going to be the root module and then we are going to put in our module file here so this is going to be configurations dot esm1 be sure that there are no typos in here if you put a typo in here and you try to import your module afterwards you will get an error message saying that it cannot be imported this has happened to me in the past so once we have all of that done here we're just going to go ahead and we're going to run this line and what I'm actually going to do for you guys as well is I'm actually going to copy paste this line of code in the description of this video because I realized that there are it's a long line uh there's a lot of options you guys can definitely explore the other options to see what other variables you can set but let's actually go take a look at the configurations.psd file so here we have our offer name that we put in jacked programmer tells us the date that it was generated on we have our root module we have our module version it automatically creates a guide for us you can also give your own guide we have our offer once again here uh we have our company name which could be we could actually put this in we have our it's a default copyright but you can also Supply your own here we have our description you can actually set the minimum version as well so you can set up a bunch of different values that are going to be in here they definitely get quite long you can go on for hours about creating modules for the most part if we're just creating a very simple module just doing these exact steps that we're doing here I should mostly satisfy what you would need so once we have our PSD file we have our PSM file inside of a folder always all the same name here we are actually ready to import and use this module so as we've seen in the last video where we were looking at modules in the beginner series we saw that if we do an end PS module path we will actually see everywhere that Powershell looks for modules I also showed you guys how to actually just make this look a little bit nicer here so if we do a DOT split and we look at we split it on the semicolon we're going to get a list so we can actually put our module in any one of these spots and Powershell will automatically go look at it and automatically load it in for us if we need to the other way that we can load in a module is to give it the exact path so let's actually just take a look and let's see the couple different ways that we can actually do this so what we're going to do here is we're going to do an import Dash module and actually just what I want to do before we actually import it here let's do a get Dash configuration as you can see it's not getting Auto loaded and if we try to run this we say get configuration is not recognized as the name of a command lens so what we need to do is if we do an import module and we actually just copy the path of our folder that contains our module so here we have our configurations folder we actually import this here and we do a get configuration as you can see it loads in the function perfectly well here so if we actually do a get module configurations we will also see it here now because it is loaded into the session and if we do a remove module configurations we can actually remove it as well so now if we try to get the module nothing appears and if we try to run the commandlet it gives us an error now this is great if you don't necessarily want the module to always be imported or Powershell to always look at it by default when it opens up but let's say it is something that you want you don't necessarily always want to write that import module and force it to look at a specific folder what we can actually do here is if we go into our area where our configurations folder is with our Powershell module we can actually copy this and if we go into our C program files and then we can go in like Windows Powershell modules and we can actually paste it in here all right so once we actually have that in here what we could actually do is we can actually do a import Dash module configurations and if we do that we will see that it actually works and we can do our get configuration if we do our get module we will see it again if we remove the module and we do the get module we can see that it's actually not loaded but in this case if we do the get configuration it still works because it is in one of those paths that Powershell always looks for so if you try to put in a commandlet it will actually go looking through all those different folders it'll see that there's a module that has a commandlet called get configuration and will actually execute it because now we go back in here and we just delete this folder foreign and we try to run this again it still works because it's still loaded in if we remove the module now we don't have it anymore and if we try to import the module with that name we will actually say that the specified module configuration was not loaded because no valid module was found in any module directory so if you want to be able to import it with just the name of the module it does have to be in one of those folder locations of the module path if not you do have to specify the path of the module so let's just go ahead and we are just going to use the path or the module here as our method um and let's see what else we can actually do so once we have our Imports so let's import the module again and then what we could do is we could do a get command and then module configurations and as you can see we can see that we do have our get configuration in our set configuration so if we go back into the module here and let's just go and let's create a new function here and we're going to call it new configuration we're going to save that here now let's say if we actually re-ran this import module and our get command configurations as you can see we actually only still get get configuration and set configuration and that is because the import module won't actually re-import the module it'll see that the module is already imported even though it's actually changed since the last time it will not overwrite that unless you close Powershell and reopen it unless you actually pass it a parameter of Dash Force now if we import the module and then we do a get command there we have it we have our new configuration so if you ever make changes to a module and you go to re-import that module in a Powershell session that was already running make sure that you use the force commandlet and that will make sure that it reloads the newest most current version of that module you're always going to want to make sure I tend to actually do this in all my scripts I'll always just add the dash Force this way I know if I've made a change to the module it will reload and re-import that change so that is something that is very very useful to know so then what we might want to do we know that we can get commands on modules but what we've also seen is we could typically do a get help on a commandlet so if we do a get help on get configuration we will actually see here that it does not have a whole lot we have zero none for name and none for remarks and even if I do a dash bull here and we try to do a get help on it we we really don't get a whole lot we get the name is get configuration we get the syntax get configuration there's not a whole lot though so if we actually want to provide useful information to our users that might be using this module because maybe you're creating this module for your co-workers or your developer and you want to provide a module that interacts with VMware or something you're going to post it on your GitHub you're going to want to actually write some help this way if someone doesn't know how to use the commandlet they can do a get help on it and they'll get some useful information the way that we can add help information to our module is actually going to be there are multiple ways I'm going to be showing you uh one of the ways here which is adding comments to the actual function itself so what you would actually want to do is do an OP greater than or lesser than symbol um and then a pound sign so here we have the greater than symbol and then a pound sign or actually lesser than symbol and then a pound sign and then it auto closes it with a pound sign and then a uh greater than symbol so then what we want to do in here is we can actually do some dot notation so here we can actually do a DOT description and then this is the description or the get configuration commandlet written by jacked programmer and then what we can also do is do another dot here and it'll tell you all the different sections that you can add so what we can actually do is add an example here we can say get Dash configuration and then a you just go on a new line this will return all configurations let's say and we can actually add another example we can say get configuration now of course we don't have any parameters but we're just going to pretend like we had a name parameter here and we're going to put test and let's say this will return configuration with the name of best and then we can even add some other options like we can add some inputs we can add links notes outputs the parameters but let's put a link here and let's just say this is a link to my GitHub and my website that has more help for this module and all right so let's just leave it at that so as you can see there are plenty of different options that you can configure here so once again if we don't re-import it when you do a get help you will not get that additional help so you will have to do an import module Dash Force again but now if we do a get help on get configuration we can already see related links this is a link to my GitHub and my website that has more help for this module we actually see our example one we see our description we see our name we should see another example oh there it is our example two uh so we have our get configuration named test this will return configuration with the name of test you would be able to put like an example output of how it would look like so again you can add a ton of information to your modules that will make it very very helpful it could even be helpful to yourself to add these as maybe you're creating the module and months later on you are still using it maybe you haven't used it in a few months and you forgot what one of the commandlets do you could do a get help and you can write a very very big well-described description write a synopsis write some examples of how to use it maybe have a link maybe you have a GitHub that you've posted more information to you'd be able to do all of that over here or maybe it uses like another another module maybe it connects to like some sort of API that you could link to as well this way you can get some extra information but this is really the basics of creating a module this will let you create uh methods or functions that are related to I would always group my modules by like application or by like function you can say uh so if I'm creating a module and uh let's say it's like an active directory module I would only have things that affect my active directory in that module I wouldn't also have things that you know control a SQL database in that same module so I would definitely separate them so I would have like uh if I have a module that I wrote for SQL and interacting with SQL databases that would be a module one module for maybe my active directory one module for sending emails um and doing that type of work so just make sure that you separate your modules there are also other ways to separate modules like maybe you want to have one module that will be for your technicians one module for your engineers the only thing about that is if they actually share functions or share methods you'd have to update them at two places which is why you would want mostly group them by think that it affects um that's would be my recommendation this is also like Microsoft's recommendation to also just keep your modules very very specific if it's for a module for VMware have it be VMware module only and then create another module for like hyper-v in this way it's just more concise it's just easier to know when you're importing what you're importing what it will affect so those are just like some handy little tips so hopefully this helps you guys create your own modules uh so we still have a few more videos in our in this intermediate series if you guys have any questions or comments please let me know down below again I will have that new module manifest commandlet in the description this way you guys can copy paste that because I know that's kind of a long um commandlet and has a lot of different options so that will be in the description also be sure to hit that subscribe button hit that like button 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: 3,259
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, windows 10, visual studio code powershell, programming, coding, scripting, powershell, powershell for beginners, how to start with powershell, scripts, powershell 7.2, powershell 7 windows 11, windows 11, how to, methods in powershell, powershell scripting, functions in powershell, methods, functions, cmdlets in powershell, powershell tutorial, module in powershell, making powershell module, modules, powershell modules, learn powershell
Id: DAQN_djqnR8
Channel Id: undefined
Length: 21min 36sec (1296 seconds)
Published: Mon Nov 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.