Powershell Tutorial - CH14 - Modules

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Wouldn’t it have been best to add shipmates by using the += command?

👍︎︎ 2 👤︎︎ u/IWorkForTheEnemyAMA 📅︎︎ Dec 12 2020 🗫︎ replies
Captions
hey everyone thank you for watching this tutorial video we are going to cover chapter 14 powershell modules so let's get started um again click on the link in the description there was a document created by goalkicker and the stack overflow community you know thank them and yeah i'm just using this as a reference so we're on chapter 14 and we're gonna cover it let's see here so first a module manifest a module manifest is imagined as just information about the module right just a little manifest or a document that talks about the module talks about variables in the module talks about modules you're going to import it's just about the module okay so to create a module manifest you there's actually a function and it's going to be the right here the new module manifest function okay so let's go ahead and let's do that all right so this one and let's do new [Music] module manifest okay and we want to specify the path is going to be um let's call it mandalorian i've been on the mandalorian binge so we'll do that and we're gonna do uh root module is going to be um mandalorian.psm1 okay so actually do this okay now the file shows up okay so as you can see the manifest was created um and it identifies that the root module is already there so because i specified it and the module version is 1.0 there's a good author and a lot of stuff that you can choose to comment in and fill out functions to export this is going to be it's recommended to specify that for performance purposes to manually specify that and just instead of just doing a an asterisk and just importing everything variables command lists a lot of things you can manage just from this manifest file okay now you have this manifest file we want to um we needed that other file so we're going to call it mandalorian.psm1 okay all right so in this mandalorian psn file we have that generated let's go back to the documentation and let's follow down further so we've already kind of completed the first step of making a manifest file and that psm1 file is exactly like a powershell file you can literally well for most times just take a powershell file and rename it psm1 then it works because psm files are just functions right okay so let's do a simple module example function add command like binding so we're going to we'll add this okay and then we'll do expert module members functions so let's go ahead and do something like that and we'll add it right and um it's not going to work because we didn't import the module so let's import module um psd1 and we do force the reason i do force is you're basically forcing it to refresh if you don't do force and if module already exists then it won't import it simple as that so force just causes it to force a refresh of importing that module otherwise if you don't have it then it won't import and you'll run into a case where why is this module that i modified not working so let's do uh let's test out add so how does this look it's a parameter it wants an x it wants a y that's pretty much it okay so let's do add five and 10 15. you guys get that so let's break down that function okay so first you guys know what a function is from our last uh chapter so we're just going to specify the module name add the parameter for providing is this and this and then we're going to do this x plus y and then we will return it which is right there okay pretty simple the export module member will just export that function add you don't actually have to include this but it just kind of you you're basically controlling what you want to export here's an example you might have multiple modules and you want to only export certain ones and some functions are actually used for that module but not really used for the intent of you you want to you know not have the user see that particular function okay let's see all right so now that we added this function um looks pretty good let's add our own function but let me actually continue through the guide and let's see what let's continue the guide and then we're going to make our own custom module okay let's do exporting a variable from a module so okay so you can also export variables from the module so export first name bob and let's include that here as well so [Music] let's make a variable here first name equals bob okay variable first name so right here did you see something so first we're going to make that variable bob right so first name now has bob but export module member variable first name you don't put the parentheses there okay this is very similar to git variable so let's let me show you an example first name i'm sorry we already have that first name we have bob get variable uh first name you also have bob you see that so when you export module variable name you don't it's almost like it expects something like that this will also work well um it's within the module so we're not we have to import it first so let's go ahead and do that let's find it push up up up until we find it okay we imported it and we'll do um first name bob does that make sense let's uh just so you know we'll call it um we'll do grogoo [Music] save it import that module again first name grogu does that make sense so you can import variables into your script from a module okay so now you can remove all those variables you have in your script put in a module and then do an import from here and it will inject it into your script okay after when you import that module structuring powershell module so we want to do this structure okay you want to first have the module this is going to be your module name then you want to include the psm the psm1 file that's the manifest okay that's the manifest the actual module is going to be here which is a psd uh i'm sorry i'm wrong guys so um this is actually the manifest psd files are manifest sorry psm1 files are the modules and we can do a loop to import every single um powershell file we're just going to import anything with the ps1 inside the functions table so you can have multiple function folders and do a lot of things like that but this would be very helpful so you can kind of split your functions and put a module and name each uh powershell file something like that i'll show you we'll do a real life example inside your my cool module psm1 file you can insert the following code get child item dash path powershell script root slash functions and then for each object dash process you want to do a dot code execution and then run that power shell item full name references the path okay does that make sense so let's make that folder real quick let's make a folder and we'll call this um functions and we'll make a new file called um get how about shipmates let's do shipmates all right now we're going to do function get shipmates which is the same name as the file and you want to follow this format whatever you name the file you name the function you kind of have a file for every function get function get ship mates and then we're going to say [Music] let's do command let binding open close parentheses you always want to use this so you can leverage the commandlets and param is going to be first we want to do a string we want to say this string is going to be actually we're not gonna do any parameters on this one this one's just a get statement so we'll just return and we'll we'll build the inventory uh we'll do an array ship mates equals and we're gonna do a ray uh mandalorian and we'll do grogu that's how the shipmate okay return shipmates okay now to test it i'm just gonna do it right here just to see if it works okay so it does work perfect and um we're done there we are done so we're going to do get ship mates okay now in the psm file we're actually going to remove all this and we're going to copy this script okay and this is going to get child for the path the powershell script root and then the functions and then we're going to pipe it and then we're going to do for each object we're then going to do something like this does that make sense so again we're going to get the child items which is going to get the list of folders inside the function column so if i do get child item dash path and i do functions that's the child items so that's going to be the list then we want to do for each of objects inside this directory i want to dot source it this dot source right here will run the code only for that session so when you close everything it goes away so you want to run this code and what that code does is it basically injects that this powershell function it basically just injects it into your script as if you were where to run it inside your script ps item the full name is if you look here and if i was to do this and i put in parentheses and i do um let's see something like that and then i want to get the first item so i'll do this right and then i'll do name get shipmate let's do a full list so this is going to be all the stuff i can get right and full name where's full name let's do star which is even more stuff full name is the path okay so it gets the path does that make sense because if you're dot sourcing you got to specify the path so okay so what's this going to do it's going to iterate through that function folder so it's gonna first we're going to get that folder then we're gonna for each object which is the loop we're gonna loop and iterate through all the folders here and then from there we have a full name okay of every single item in that list so let's save this let's um just to know that it's working i'm going to put another person um bob okay let's import module and force it let's do sh get ship mates mandalorian grogu and bob does that make sense so we are importing every single function in there isn't that cool if you don't even use functions that's pretty cool right um imagine that you have your little folders of little scriptlets and you're importing it and not having just folders of scripts here here you're actually building modules you can import and use later location of modules so the environment ps module path so if you do env.ps module module path that's the uh path wish it didn't do like that but there it is the path right here so it's just going to go it on first down and if it finds that particular script it will run it and stop pretty simple just top down so if you have a folder in front of that particular module where you edited the module so if you have a module in one path but it's maybe three lines down and then you have the same module in the folder before it you might be importing something that um you're not really using so you might be editing something and you're not really using it so keep that in mind and module member visibility so [Music] again by default only functions defined in modules are visible outside the modules in other words if you define variables and aliases in the module they won't be available unless you export those module members okay and that's pretty much it for our module so let's mess around a little bit more and uh and we'll be done with uh this particular chapter so let's do another one let's do add chipmate so let's do this so we have a shipmates and we want to add a shipmate do new file add shipmate ps1 so first we're going to get shipmates so um let's do a set ship me too yeah set ship mate ps1 so instead of getting a shipmate first we want to set the shipmate and then we want to specify a global i'm sorry script variable and we'll say we'll call this shipmates okay so shipmates um actually we'll just return it here okay um what would i do script shipmates turn shipmates i think we just do this okay and then we're going to set the shipmate so let me just copy and paste this and we're just going to change this to set shipmates and then this one's going to say script.shipmates equals um [Music] something like that now this parameter where is it coming from well we gotta provide a parameter and we'll say shipmates something like this right and we're actually gonna specify it needs to be an array actually that's fine it'll be an array okay so we're expecting it to be an array so we can kind of control that flow so let's import first right guys so we got to import the module first with the force to refresh it we're then going to set ship mates and we're going to provide an array you do it at sign you do open and close parentheses and then you do mando and grogu okay now we're going to get ship mates and you get mando grogo let's add a shipmate now so let's copy it again we go to add shipmate and we're going to add a shipmate so we don't need to provide a parameter oh actually we do because you're going to be adding one person and let's just give this let's just specify this is going to be a string so we only select one value at a time okay script dot colon shipmates dot add and we'll add person so we're just basically when you call let's change this ad guys so add shipmate when you call this it'll run this function it's going to expect we need a person to be provided which will be inserted here add is going to add shipmates to the current list already here it'll append it okay so let's save it import looks good get shipmates nobody in there we're going to set chipmates mando grogu we're going to get shipmates then we're going to add now mates so add shipmates um and then we're going to type a name bob and it doesn't work why doesn't work add with one argument collection was of a fixed size uh parameter string person dot add scriptmates dot and let's try oh let's try just uh plus equals script shipmates plus person let's try that let's try that module set set ship mate um get shipmates add chip mates get shipmates again and bob's in the list okay and just for fun we'll add another one let's add another one we'll call this git um slogan ps1 and then we'll do another one get slogan function here and we're going to get we'll do a write no i'll say return this is the way hmm that return this is the way see that works oh let's play attribute commandment bindings it's probably because i have no params okay try again import and get slogan oh you see what i did here you guys probably saw it before i did get slogan let's fix that function we got to put the right name okay one more time let's import that module and get slogan this is the way all right let's package this up and uh and put it into one of the folders so let's see what we have here so let's do um path equals ps environment ps module path actually it's split um let's do this let's do uh split actually environment ps module path i need the intellisense split by semicolon maybe okay and then we want we want the zero will that work yeah so path equals something like that okay perfect actually we'll call it a destination path okay now what are we gonna do guys we're gonna copy this so uh copy item man can't see anything copy item path um we'll do the current path and we'll say destination is destination path we want to recurse and we want to force okay so we use this does that work uh ls path okay see the cd path um there we go so now let's do uh remove module let's see let's do get module first where's mandalorian there it is so it's there remove module mandalorian okay it's not in there anymore let's import import module mandalorian okay no modules found that was called mandalorian let's rename that mandalorian let's try again and there we go the folder wasn't named right because i named it chapter 14 modules but it now works so did you guys get that get slogan awesome so thank you for watching everyone i hope this was helpful um you now know how to build modules and hopefully you'll integrate it into your workflows and your daily powershell scripting flows and if you like this video please like and subscribe uh lets me know that um you know you like the stuff so thank you everyone and stay safe [Music] you
Info
Channel: TeachJing
Views: 1,783
Rating: 4.8947368 out of 5
Keywords: Teaching, TeachJing, powershell tutorial for beginners, powershell modules tutorial, windows powershell modules tutorial, powershell, windows powershell, modules, powershell module, import-module, get-module, create a module, exporting module, windows powershell tutorial, powershell module manifest, powershell module best practices, powershell module vs script, powershell tutorial 2020, modules tutorial, teaching powershell modules
Id: I4xM4BdYmfU
Channel Id: undefined
Length: 28min 11sec (1691 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.