Powershell part 48 (How to create powershell modules the easy way)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there mr automation is back today um again on powershell modules but the easy way i already created the video and it was okay so to speak with perhaps i can extend that a little bit i also looked at on youtube when there's a lot of videos around on how to create powershell modules but i also noticed that a lot of them are quite complex um uh perhaps i can make it a little bit easier to understand and not go into much detail about the modules um so let's start yes i'm recording it's very important of course um so yeah let's go to the slides so the agenda for today we create i'm creating a simple video you know there's many complex videos around so that's the reason why i'll create this why modules i'll explain that we're going to have some examples with the normal script to skip with the function an example with the module we import that module from a random spot we will extend the module with some functionality and we import it for above availability in all powershell sessions and we try to import it as well under only your private sessions let's see if we get there don't want to make the video too long um yeah so why modules now basically if you detect yourself repeating yourself in code that's always a trigger to modularize so to speak and you can do that with the function of course you know if you only use it one times in one script you can create a function of course that works fine but if you're repeating yourselves in other scripts for instance so you create like a nice function for some login you have that inside of your script and that works very fine then you create your next script and then you need to copy that function over again and then you start again and then at some point you change your locking function inside of one of the scripts and the other script is still using the old version of course so it's um that's repeating yourself so if you detect doing that then really you should switch to module approach and it will make your life easier for sure it can be shared amongst other scripts you can put it on a network share for broader access so even other systems and other scripts can access your modules for instance and if you already creating functions that you are reusing you should switch to the module approach keep in mind if you change a module and that module is used by a lot of other scripts and you screw up the function inside of one of these modules you will affect all the scripts that use that specific function from that module so keep that in mind there are some caveats there you don't have that of course if you have like a module in each script of yourself then that is a self-containing a function so to speak so that doesn't get modified when you modify a module so keep that in mind the module if you modify it you modify it for everyone that uses that module enough explanation i think so let's go to the examples so let's create a simple script and i'll open a visual studio here and let me elevate it because i want to do some wmi stuff as well on the bios and i think you need to elevate to be able to access those kinds of namespaces in windows um so let me see i just created an empty file here a new file doesn't really matter much i will save that as ps1 perhaps we'll give it a little bit more descriptive name i'll call it demo60 it's the 60s demo i'm giving or something like that so let's see um i prepared a little bit see me type all the time it's pretty boring so for instance javascript that gets some information from the bios of your system i'll run this a little for now and you can see that inside of the bios variable we now have some information there you see that so we only selected the manufactured version and for instance i'll copy a couple of lines over now i don't want to make it too long so now we get the operating system for instance so with the os there and then we have the services so we can put that to the screen i'll show you a couple of examples here should move to a fun module or function for that so we run this and for instance this at the top part we're only focusing on the bios part for now we comment all the other stuff out for now and let's focus on that only it's a real estate and i'll need to size this up as well so let me do that now create real estate there that one can close okay perfect so for instance you use this function often inside of your scripts basically every script you create you yeah we need to type this whole bunch of uh this whole syntax again and perhaps at some point you're interested in also another property and then you need to yeah modify a lot of scripts and to uh to uh to cover that sort of speak to cover that scenario because if you forget to add one property somewhere that script will behave differently um the same goes pretty much for the operating system so let's put that one as well here so for instance you often get the information from the operating system i'll show you how it looks after clearing the screen she's got a lot of 64-bit os language you get all kinds of stuff it's microsoft probably something about windows yeah here you showed the caption it's windows 10 and for instance you only want to know the caption indeed so you're only interested in that and perhaps you have some scripts that use this input right you do always a query on the operating system and for instance based on the operating systems or based on the title of the operating system you want to perform different steps in your code for instance you can have an installer that's not applicable for windows 8 but it does work on windows 10 you can for instance get the operating system and do an ip statement on that value if it's a windows 10 then run this otherwise do not right and again if you need to do this in every script you're repeating yourself repeating yourself repeating yourself so there is a better approach i'll show you what is better approach first we create a function for it so we create the function get bios for now get pipes open and close that with no inputs very simple remove that there and we just do a return of the bios what you can do now is call that get buyouts function we all are a little bit more flexible already don't need to type the syntax all the time and let's see but we get back if we actually run that so now we have a function get bios it's a lot shorter than this whole syntax here let's run this so you get the same result back and by the way i did the bios and we were interested in the operating system so i'm not really fair here let's create another function because we need to create that anyways uh get os and i'll show you well why i do get there it's pretty important to note if you do a get verb you get a list here i'll pipe this to the grid view and you can see it a little bit better it's a pipe to the grid view yes let's open that and this is the approved verb so speak powershell is quite it's strict language you know it's all about the predictability don't want to create crappy functions but my function or my bios you know it needs to be sensible so get os is sensible and get is one of the allowed uh verbs it's very important if you write modules so don't create your own verbs that are not allowed because it's not allowed so don't don't do that always stick to the list of verbs here so you do add to clear close copy enter exit file and and so forth answer what answer whatever is applicable so if you stop something use a stop dash and you give it a name if you want to restart something you call it restart dash and you give it your name so keep that in mind that you really need to stick to that convention um so what we can do now is get bios and we can get the alex well that always let's run this and before we run it we still need to return something here so we need to return the operating system here we need to return the operating system here and let's run this so what we get back now is some buyer's information and i would expect some information about the operating system as well so let me run this again you know so there we have it we can do a get bios get the bios information back here and we could do a get os to get some os information but um i can make another function but i think you get the idea so for instance you want to use this get bias function in other scripts as well that's pretty easy to be fair and um it's really as easy as this by the way i will open this on the left side if you go to the file here let me go to the file refill and explore it's a little bit a little bit of a rubbish here it doesn't matter so it's now called demo uh ps1 right you see that so if i call it ps m1 going to change that let's see if that's allowed yes this will allow out visual studio didn't like that much i think but let's open the folder let's go to that file so hang on let's see it's there let's go to there a little bit messy to be firm it doesn't matter it's just a demo after all so we have our demo here our demo 60 psorm module and you know remove all that stuff here and if you now run this it basically it does nothing it says yeah you cannot run the document in the middle of pipeline because basically it's now a module it's not a script anymore if you now create the new file and let's save that a little bit sensible name save it as test one very sensible right and what we can do now is you know import uh module and then we can basically point to this uh this guy and that is in my uh i think you can do it just click just want to work with me it doesn't matter we copy this over so we copy this psm file copy a spot and we import the module now so now we have script here and the first line is we import the module so now this module is imported in our code what we can do now so we imported module we don't see any error but all of a sudden now we should have get os fitted with now available and it still behaves the same of course we can still do get bias so what what we basically did was move our functions to a module and then you can import it from here and i did now a little bit the lame of example and let's go back first to the slides uh simple script a little bit more advanced with the function we did that we ported the functions to module we also did that and we imported the module from a random spot we did that now we're going to extend the module okay fair enough let's see if we can extend it and i'm quite sure we can so um i will let it fail as well so then you will learn some other tricks as well i will let this fail all purpose so bear with me a little bit and this time we do something with the date yeah that's that's a pretty nice one um that's uh mrb date and i'll tell you as well why i do that because there's already a get date commandlet available and you cannot reuse that of course but since you need to start with get and then put your name behind the time still sort of save them and i'll show you as well what this piece of code does it's very simple what i do often in scripts i generate date like that that looks like this like that see that i do it again try it like this i mean so i construct the date and often i use this inside of a log file printer so i'll show you as well how that works so for instance you have that variable now what i often do is stick that inside of a log file and every time you start your script you get a new law file and and and i was a little out hours year month day underscore underscore hour minutes seconds so every every time you run a script you get a new log file that can be handy for troubleshooting purposes but again here if you have another script you want to use that same date again and every time you basically need to repeat this step inside of every script to get that date you you want or inside of your code and of course that's why we created this function you can also do it with the function and just return the date here so let's be clear here they will return otherwise it will work as well but it's not as neat we're really returning something so you need to be explicit on that one i think so we're returning a date here what you can do now is for instance if we import this module i'll show you and i told you it will fail as well and i know that it fails so that would mean and now we do get os nice that works you get bios that works and we did the get mr b something like that let's see how we called it get mrp date right we just added that to our module and let's run that one hey that's nothing you know how can that be i'll tell you why because this module is already imported inside of this specific powershell session now you see and when you um import the module it's only available inside of that specific session i'll play the screen here but it's only available in that specific session if you want to re-import it because you run your script over and over then you can specify minus force but minus force means that it's always important to module also when it already thinks it's loaded because the input module just looks at this module thing oh i already loaded that but it's not aware that we just added this new function so let's import it now with the force we did that and let's now run our date function and we need to press the correct key of course yes and there we get it so now we get nicely our date back from that function here and use your imagination of course i'm using very simple lame examples but i'm just trying to teach you the mechanism and don't want to make it too complicated you can put whole scripts in here that return something as long as you return something to the caller or not even that is needed sometimes you call something you don't need anything back right it's sort of a void in programming languages you can do that as well just wait for the call when you do that um let's see imported for all power success yeah we did not do that yet and i'll show you why that is handy for instance i launched powershell shell here now all right i just have a shell and i'm playing a little bit around and uh i don't want to do get wmi object and then win 32 and underscore bios and i don't want to do all that rubbish so i just want to use my uh get bios function for that right i created that guy and it's inside of a module so let me run that ah not recognized no indeed it's not and um and why it's not available because powershell default looks in a specific location if there are modules there and if there are they will be on imported on runtime so if you add the module you need to close your shell and start it again to get that module available inside of the shell how do you do that i'll show you so we'll take our wonderful example here our ps60 demo forgive me for the names not the best name uh and we go to the see problems files windows powershell modules folder and there are some rules here uh it's not that strict but friend you need to create a folder that has the same name as your module so i call it demo 60. that's quite important and inside of this you can put your psm file right like that like that so what we can what we now can do we don't need this by the way this script anymore what we now can do because we put it outside of the modules program files windows powershot modules demo 60 demo60.psn one if you now open the powershell shell i'll size this up a little bit and we'll check which functions are inside of our module for instance get guys should be available here now as well that bite and it is and yeah just imagine the power behind this and what you can do as well is um put this module on a network share or an xmb share somewhere and just uh during the runtime of your script you can import that module still you know then you import it locally and then you can still work with all those functions so then you can really write one function that is available for your whole organization now basically but keep in mind if you break the module by correcting some incorrect code inside of a function you know you break it you introduce a bug uh yeah all scripts that use that module and function that will suffer from that for sure let's see i think this is it already i don't want to make long and boring videos let me think a little bit more if i can make up of something else yeah i found a quite interesting function so perhaps it's a little bit more interesting to show as well so i'll size this up a little bit so let's go to the specific function so now we have a right to law that's uh that's the module we have here now i commanded out the file doesn't matter uh but it takes some input and then it produces some output as well and what kind of output it does a write host so it presents some information to the screen and i use this function a lot i also have it inside of a module and yeah let's save this guy and re-import it here with the force and we need to do that input module demo 60 because it's called now like that because it's not that file anymore let's see yeah that works fine now what we need to do is still copy that file over i forgot that i think i did not yes so hang on we do differently i'll put this one inside of there hang on do it a little bit differently so open the module now inside of here and we'll add that function again so let me copy that over but kind of type that whole function of course going to waste your time with that so here we have a right to look accept some some stuff i'm not going to in any details i saved it now if i now go to my script here and we re-import that guy again i think we also have uh right to lock something yes and let's run that and then it says a message what do you want to do write to the law how mark is here and that's a category and i'll do error now we get marcus here with the error and um for instance this is now your script you're going to do some magic that this script will do a lot of magic magic and one of the magic things is write something to the log file right so you go right to log minus message info message minus category info and then we do two more so we do a warning message and we do an error message error ratchet air roar versus yes exactly i need to change that to warning and then to i'll show you wise one inside of this uh module we're basically referring on the category if it's an error if it's a warning or of its info then basically we write a different color message to the screen okay so that's it and if we now run the script with this imported module of course with this uh module containing this that function we can now run this and you see the output here that's that's pretty nice so uh informational green yellow so this is a little bit more um sort of an advanced function but you can stick that in the module as well and basically you can add as many functions as you like don't say you should do that but you can for sure i would split it up a little bit perhaps i might do for locking perhaps a module for some wmi stuff i don't know but i would not recommend to stick everything inside one module because if you make like a syntax error you break it you break everything and if you do like small modules you have some more flexibility there um [Music] yeah i'm quite satisfied about the video so go to the end slide thank you again and hope to see you in one of my other or next video bye
Info
Channel: IT Generalist aka Mr Automation
Views: 425
Rating: 5 out of 5
Keywords: automation, powershell, learn automation, how to automate, saltstack, windows automation, linux automation, unix automation, salt automation, powershell automation, Mr Automation, IT Generalist, configuration as code, infrastucture as code, IaC, orchestration
Id: TQtBwV4MaEU
Channel Id: undefined
Length: 24min 13sec (1453 seconds)
Published: Fri Mar 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.