PowerShell Module and Manifest- create and configure your tools

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the more powerful kobe wrote the more functions scripts we have flying around and at some point it becomes annoying to actually manage all of these things especially when we might have multiple scripts working on the similar things or like a common domain and that's where powershell module and module manifest comes into action so let's have a look [Music] hello everyone coming here with another powershell video which i've been recording for some time now so yes as mentioned powershell modules is a way to bundle multiple scripts together when they all manage one common thing like for example have active directory module which manages active directory i have bunch of azure modules and each of them manages specific like vms or storage but modules gives you much more than just bundling things together because effectively you could create a script with multiple functions in it and that will kind of do the same thing with module we have things like for example obviously you can offer them so that's for one but we can version them so you know obviously we fix something we bring up newer version we add new functionality we add new version we can also bundle all the scripts with it or even other dlls with it so the c-sharp libraries with it we can also spread that require a certain version of module of of powershell or specific modules actually you know that this module depends on other modules so for example when you're wrapping that set active directory module in our own needs then we can just say that my module requires active directory module and when you try to load it you will complain unless actually you have this one so let's have a look how we can come around this okay so for the purpose of today's video i've prepared three simple files very simple implementation each function they all will just go to some api and ask for data and that's it the purpose of to show you how to build a module out of functions you have already other than you know getting into implementation so let's load my three functions one two three it's already getting annoyed i need to do it one by one and then yeah i just make a point i can call them so this one works and i know let's say this one get what certainly they do work but what if i want them all together yeah because obviously at this point i could do a couple of things one will be to create so-called wrapper script that will go and then dot shows my scripts will be one that will be two and that will be three yeah and the effect of this will be exactly the same like what i've just done so that pretty much says grab this file and then dot at this point says load it to the memory so this is equivalent but again i need to create another file to load my free files not ideal isn't it so what i can do instead i could for example gap all of them into my module okay so let's just call it my staff dot ps1 and for now i will just literally copy and paste them one two three okay so i have my three functions and now pretty much if i run them i again get same same method because same result because they're all three functions i think to this point we actually yeah i just load bunch of functions so when i have it how do i actually convert it into powershell module i do this vsm one see so that's the defense i just add psm1 which stands for powershell module and at this point we know this is powershell module and that's kind of all about it we have module actually loaded it because yeah by default powershell modules don't get executed by default you need to do something like import module and then point it to the file you obviously know that psm5 is already there so that will have the same effect but why is the added value here well there's so much value added yet because we're now in this point we just indicate this is powershell module but we don't really tell powershell what to do with this module so we just load my functions like in normaldo what we need now we need a module manifest so what we do we go new module manifest and i'm going to give you the paths adobe mystar.psd1 for example yeah we need to give it psd1 because it's powershell data file and this is what powershell use for its configuration so then we go and then pretty much it's like a template because you could literally have one psd one file and then copy this across and as long as you randomize the the grid that it comes with it will then just work but pretty much what we have here let's just look at some values at the first yeah first of all is root module and that is actually what to load when you load this module so our rule will be pointing into my staff dot ps and one file yeah that's pretty much the common way module version this is obviously very fast one and when we get to the point of grabbing this for the devops pipelines or github actions we actually automate versioning it but we can i don't know let's put something that this is version 1.3. i don't know 34. yeah just to show you that this will come out at least we have version actually a method to version our code uh compatibility ps edition specifies whether we work on the it is only powershell core which is powershell 6 and newer only partial or desktop which is up to version 5. if we don't specify this powershell we assume it works on everything and this is the grid and pretty much that's what uniquely identifies this module so when you're publishing that module to let's say powershell gallery or something else this must be unique that's what says that this is this unique version yeah so when you will be actually later on copying psd1 files across then pretty much always undermines this yeah you literally just go new it and powershell generates you some random grid overwrited done how far so it picks the username for the alpha company name so we can actually specify which company did that then we have some copyrights but what else we have we have for example description so for description we can i see all i need to do is uncomment it i just say you know this bundles together my important functions whatever i have something to indicate actually what is it what it does so a person looking for modules can use it to find powershell version this is the minimal partial version so for example if i want to limit myself to powershell version i know five i can just 5.1 windows powershell i can just do this one word of caution if you actually go and grab peers version table we on the on on the call if we go from here to peers version table you will give me this which is pressure version and this is actually windows build yeah so at some point i made a mistake and actually i pasted this all and it worked on everything apart from all the windows 10 versions operating windows versions so unless you really want to limit your windows partial version to specific os you can do it that way by seeing the whole thing like a minimum otherwise just give it first few versions and as you can see this is the edition we had before and this is desktop and call will be saying somewhere here call that this is called so this is a new powershell okay we can require this to hostname so for example we can specify we only want console or vs code or stuff like that version i think it's version menu version of dotnet framework so if you want again powershell 5 windows password was built.net framework 4.5 if i remember correctly so you can again limit by powershell version or maybe you load specific mo dls i have specific version same this will be also related to the net i believe process architecture well if we want to go you know only 64-bit only 64-bit then we can do that i think i've used honesty 86-bit ones with snap pins were still around i might be wrong but this is pretty much non-existent anymore like all everything has been 64-bit powershell this one is really interesting required modules so that's where we can specify either by name but i want for example a random module yeah that's where we can pretty much specify the the modules we need or we can even go step further we can specify specific module name equals you know az.storage and that we want module version of for example 1.0.0 i don't know i'm just imagining but pretty much what happens here at least usually when i develop something i tend to pick the versions that already have installed my machine on on the production machine so i can actually at least you know i can say that we need at least that version of module obviously i cannot as we can see this is i so i can add another lot of modules so this property is really useful when we are actually wrapping all the modules or we just have dependencies i will put this in place and i just want to show you actually that it will complain because obviously i think there is we are not that high with azure modules assemblies it's dll specimen so when you need site and load certain uh dls we can do it other scripts to process so pretty much what we can do we can bundle this module with all the ps1 files that we run before the module is executed for example if you ever load that vmware esxi module for managing yeah esxi it's pretty much at the very first thing you when you load it shows you hey this is you know a esxi module from vmware and this is how you use it that's how they pull it off they use pretty much this with the code they've shipped another ps1 file that displays this this one is ps1 for formatting of device and this is more advanced topic although that's how we can do it nested modules so when i am actually shipping more modules or scripts with this module yeah i bundle something together i can nest it that way and what powershell will do when it's loading the module we go and automatically load for me these modules oh yeah obviously i didn't mention required modules also load these modules yep so before before it starts it allows it actually loads these required modules so otherwise in my code i don't need to even worry about things like import module testing if module is there if it's there it loads it if it's not that it complains it's not there and doesn't load so same story this this option is very interesting functions to export so this is actually means which functions will be exported for the user so which functions can be used by the end user so in other words let's speak for example mine and what we'll do will happen now powershell will load this module but only this one function will be available to you as the user so i can still reference this other functions in my code so just for sake of it let's do headquarter here but pretty much these functions will be hidden from the user i know you might wonder why actually i would want that and you want that when you have your own let's say so-called helper functions actually they help you to i don't know format things or pull data but something that user done won't really use it that's why you you have them as so-called private functions yeah so you don't export them or if you want to load all of them you just put put asterisk like i think was before oh log in here yeah then just it loves everything commandlets so that's how we again load functions from dlls we have variables is dsc that's the list of modules that we ship this is just for the data and for the private data we certainly need things like one tag and this one magic tag and this comes when mid later when we build bundling things as the nugget packages is ps module so that will indicate the powershell that this is ps module uh well with the license ui icon what we released you know in this version and etc etc and at the very bottom very interesting one default command prefix so probably by now you are custom that powershell modules when they are common they all have like a prefix they like get ad user side ad use and etc etc so same thing we can do here now let's say put my initials so pretty much what what does it do in front of each of these functions you will add kp so in that case get kp quad here get kp whether when it's exposed when it's public get kp card fact so that's how you can very easily add that prefix to each of your functions okay so i think we can try to load this now and let's just see how it complains about the dependency output so we can either do import module or we can do ipmo which is analysis for this and then we tile it to my stack psd and we can voice force means load always reload it if the function is already in the memory you will not and if you don't force it you will not load that to memory so you know otherwise especially when you're working on the module and you want your things you know you change something you you build your module whatever you want to force it otherwise it will not load it so let's see and here we go and we can see ac storage is not loaded yep so that's the thing if that if it was available there it will load it it's not uh let's see i just put some low version i'm not sure if i have these modules at all on this machine ah i must have it because it just loaded and if i go get module i should see this somewhere there and then so you see it loaded easy storage for me of this version which is fine it also loaded a z account so apparently probably if we will go open ac storage module we see that it has dependency on azure storage and there's also my staff of this version which i specified here so this is literally it module manifest gives you a lot of power with a very little effort and takes away from you things like validating whether modules are there or not although versioning of things like keeping track of what's which functions are public or not might be a bit annoying and for that we're gonna have another video right so that's pretty much all about module manifest if you want to dig deeper you can always open microsoft documentation about it but pretty much what i showed you is what i've been using most of the time that's how you prepare your code to be shared with others really so on the next step because all this work is bit annoying when we do that i will show you actually a framework and how to organize your code so that you can build it i would say following the conventions that exist in powershell community but for now bundle your scripts together grab them in the nice powershell module share with the users and hopefully that will be start like the next the next step in your tool making career but for now to the next time see ya so you
Info
Channel: Kamil Pro
Views: 2,500
Rating: undefined out of 5
Keywords: powershell, pwsh, script, function, require, advanced, module, validation, code, manifest, version, psd1, psm1
Id: xPQq0ui8j78
Channel Id: undefined
Length: 16min 39sec (999 seconds)
Published: Wed Feb 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.