PSPowerhour episode 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my stream deck's not working that's the problem [Music] there it goes [Music] [Music] what's up [Music] did i spell your name right mike sweet i did it without looking so i was kind of confused is the music coming out i haven't muted because i don't want i would want to reverb okay yeah i made that mistake last time i played it and then my microphone picked it up and it just started echoing [Music] [Music] why did i lose i lost the background what happened that stupid cat [Music] oh there okay that's weird these [Music] eleven [Music] oh all of it [Music] yeah so yeah yeah yeah so we're gonna have like we'll have a little conversation kind of a little interview type thing and then mike will do this presentation uh and then we'll kind of wrap it up with more questions and whatnot [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] about it all right there we go hello everyone my name is james patty and welcome to episode 8 uh ps power hour i am joined today by the most awesome john up in this corner and our guest host and our guest host for today one of the uh co-founders and uh uh organizers of the raleigh power show user group mr phil bossman and of course our guest for the day mr mike i can never pronounce your last name just go with mike hi guys hey guys how's it going doing guys so uh it's uh tuesday it's five o'clock here uh in tennessee i think you know mike and phil are out we're also on the east coast but john's two o'clock over at your time yeah it is two o'clock yep that's an afternoon awesome i know i know tomorrow we'll get the deadly update but i hope you hope you enjoyed your vacation and i hope you got some good deli food while you're out hiking it it was and it is so good stuff awesome uh so phil i know i kind of threw you in through you at the last minute i apologize for that i was supposed to ask you two weeks ago and i forgot uh so thanks so thanks for joining us we're starting a new thing kind of bringing in guests uh kind of guest host that way it's just not me john and dylan thomas uh uh every time so tell us a little bit about yourself uh well my name's phil bosman and 40 minutes is perfect enough time for notice you know that's that's all it works that's all you really need so um yeah i run the uh research triangle powershot user group and uh you know mike is uh you know a great guy and uh i think he he's really brought a lot to the group and uh you know i'm super excited to you know hear what he has to say today or actually you know just just be a part of that and uh you know we're we're we're a good team the research research triangle powersheet user group you know we're a really good team and you know we're really uh fortunate to have mike on that team and uh it should be good this is going to cost me too many nice words i'm going gonna build you up so we can tear you down all right so mike why don't you uh uh introduce yourself for all of our uh 17 people watching and all the ones that are the thousands of people are going to watch us on youtube hey everybody my name is mike kanakis i am an ad infrastructure engineer for a company called the lion systems they make invisalign braces i'm also pretty active in the community you guys probably know or seen me on twitter i'm associated with powershell.org i help them with some of the community events i blog and i have a i blog for various websites as well as my own and just your general sysadmin trying to get by every day awesome now but so before we uh hop into mike's presentation i do want to remind everyone about the 24-hour live stream that we're doing in conjunction with uh ps ps conf asia and also the ps uk day uh so the cfp is open it closes september 15th so uh we're approaching that today's the first of september ironically off or uh the 184th day of march kind of however you want to look at it but uh so go ahead and get your cfp's in uh we do need a lot more and especially in the earlier utc time zone so if you're on the other side of the world from where we're at uh we'd love to have you so you know i would say you know support that as well as if you know certainly put in a time but also if you're like hey i got no problem waking up at 2 a.m and actually being a part of this and i'd really like to participate you're the opportunity's there for you and so that is awesome that was awesome because you don't want your note live you know at the right hour and you can put it in there so if you're available to you know or you feel like you want to or you're available to do it feel free to do it so because you know the content you have to share is valuable to everybody and you know we really want to encourage that for you so absolutely absolutely all right well mike why don't you go ahead uh can i have one more thing also uh the powershell conference book version three is out and it is currently um in works uh it is like 70 done the last couple chapters are getting um uh proofread with the editor but that is actually published right now you can buy your copy today and it is technically a conference book and a i'm actually a participating editor and there's a ton of great content in there and you know if just the world of conference is the value of a conference is to you know walk into a room and just absorb some information that and just walk to another room and get a whole nother view that's exactly what this book is and so this is version three of that book and actually that's version two um but on top of that is that the the idea of really diving deep into a different topic and not so much to you know grasp enough that you can get the content but also you know enough to to wet your whistle and want some more so i really think it's a great book so definitely yeah so uh oh yeah deal posted the link in the chat uh for those of you that are going to watch this later this is the social events channel both in discord and slack uh it's also leanpub.com ps3 ah yeah so with that mike why don't you tell us a little bit what you're gonna talk about and uh take it away uh so what's gonna be talking to me let's just let's start that again we're gonna be talking today about how to create a module and we'll do it by hand um so this is foundational knowledge uh if you if most people think you need to know how arrays work the pipeline all these things this is another piece of that foundational knowledge how to build modules so you can share code with others and before we jump into that um that powershell conference book that phil was just talking about for those who might be listening that might be might not be aware all the pro all the proceeds from that book 100 of the proceeds not just the profits go to fund charitable um learning programs at powershell.org at the devops collective uh there's a sister program that rides alongside the powershell summit called the dev uh the on-ramp program and the past couple of years we've been able to send about 10 people a year all expenses paid so they can spend a week learning from powershell experts in the field getting not just technical advice but advice on career as well so that's what the book is about there's lots of great content but know that every dollar that you spend towards that book goes towards helping people get started in the community so that being said um i don't ever do presentations without some slides so let's get some slides going i know everybody loves slides so uh here we go all right so you guys should be able to see my screen now right good to go all right so let me just minimize that so i'm not looking at myself all right so today we're going to be talking about making your first tools module tools meaning you know most people when they get started they're not making some modules that super specific like this is my certificate management module it's more like hey i made a bunch of cool scripts and either i've been asked to share them or i would like to share them with others so maybe you've turned those scripts into functions maybe you've built them into reusable tools and now you want to package them together and you want to get them out there so that's what we're going to be talking about today so real simple we're just going to go through some quick slides on module basics it's like 8 slides it's going to be quick i'm going to jump right into making some modules so i'm going to try to demo three different modules and then if time allows after we make those modules we can talk about how we share those modules simple stuff should be easy and interesting i mentioned earlier that i'm an infrastructure tools engineer and i'm pretty active on the community if you'd like to track me down please follow me on twitter i'd be more than happy to talk to you um this talk borrows heavily from the microsoft docs and also from kevin marquette's powershell explained website both of those are excellent resources if there's something that i didn't cover today that didn't make 100 sense specifically kevin's website is where i did a lot of the research on some of the particulars for myself so chances are if there's something that i didn't cover today that you're interested in if you head to kevin's site you'll probably see some of the similar content all right so with that let's go back to school so there's a picture of a calculator on here because i'm using it as a reference to remind you of when you may have went to school way back in the day and you had to do the math yourself addition subtraction multiplication division and then one day you show up at school for the new school year and they say you know what this year you guys are allowed to use calculators and you start using a calculator and you say omg why the hell did i have to do all that math prior to this this is so much easier however you still need to know how to do the math and it's it's expected that you're using a calculator now because you're going to be moving on to bigger and better problems more complex things and things like doing simple math and simple division and simple subtraction isn't really the focus anymore we want to get you past that and move you on to onto more complex things and that's a great analogy for why we should be learning about how to build modules the reason they say that is there's tools out there that can build modules for you and automate away much of the work however is assumed that there's some foundational knowledge when you come to use those modules those module builders it's assumed that you know how a module works and what the moving pieces are and the tool is really taking away the work of you having to do it by hand so if you're not familiar with that i don't recommend that you start with some of the automated tools to build modules you do it in the way that we're going to talk about today so that being said let's dive right into some of the basics what is a module it's really simple it's just a simple way to share code it's a portable package that contains things like functions and aliases commandments helper scripts and it's a simple way for you to package things together and be able to give that code to other people so as i kind of just alluded to why would you make a module it's all about portability if you think about it most of the code that you've consumed from a vendor or from microsoft has come through the way of a module and a modules really serves two purposes really the same purpose but from two different viewpoints if you're looking to share code with someone else you kind of want to give people your code but you may not want to have to sit there and have to figure out how people figure out how to install that code and if you're looking to consume code like for example i work in the active directory module quite a bit i want to consume that code from microsoft i don't necessarily want to have to open a support ticket to figure out how to get that module installed i want to follow a simple procedure and import that module into my machine and just be able to use it so a module does that for us it creates a package that we could share and if we do it the right way we can publish that module in a number of different locations people can then consume that code and without just a little bit of effort they should be able to use the scripts and the functions and the other things that you're sharing without any real challenge so very quickly there's some major pieces of a module to consider and first and foremost is the code itself talking about the ps1 files so when you save a file you're saving as a ps1 file that's the code that's the part you want to share you can also include things like supporting files like helper scripts but you don't have to and then another major piece would be something called a manifest and a manifest is really data about data it's metadata and i would like to explain that to people is like it's the index of a book the book is what you read the index tells you how to get to the parts that you're interested in and last but not least we package that all together into a directory and if i'm going through this fast it's because i really want to get to the code and less about the basics because this is something that anybody could pick up but it's good foundational knowledge to cover so that when we're talking about the pieces in a few minutes it doesn't seem so far into everybody so going through those major pieces again we talked about the code there's something called a psm1 file and that's where you're going to park your code and i'll show that off in a second and then i mentioned there's a manifest the manifest is the metadata about the modules so that's data that describes the module and that's saved in a psd1 file and i like to think of the d as data data about the module and then there's this concept of public and private data meaning that if i had let's say 20 scripts that i wanted to share let me rephrase that and say i had 20 scripts that i wanted to put into my module but for some reason 15 of those scripts are not meant for people to see they may do back end work i could publish five of them and i can hide 15 of them now this is not a security context people can still find those hidden scripts it's just what i'm showing to the end user when they import my module and then last but not least it's always about documentation and what i mean by documentation is i'm less concerned about the documentation about the module itself i'm more concerned about documentation that talks about the code in the module so remember if you're sharing code with others and you're going to give code to people for them to consume you want them to be able to figure out how to use the code by themselves so that means you need to have supporting documentation for your scripts to explain how they work what the parameters there are uh different examples how they work so that they can be self-sufficient and use the module without having to rely on you that mod that documentation can also be turned into online documentation that you can send to a website or to a github repo that can be either the same as what's in the module or it can be even more detailed about your module so if we go through those pieces one at a time we talked about a psm1 file that's where i said the code libs then a psm1 file as i said it's where the code libs and any valid powershell script can be a psm1 file so if you have a file like my get my ad user dot ps1 and you simply rename that to a psm1 file congratulations you just created your first module it's really that simple now from there of course you can expand and do a lot more but the psm1 is the tool that we use to share the code with other people so once you save it as a psm1 file it can then be imported into another machine to be consumed by another user and we'll talk about that more in a little bit a psm1 file needs to be saved in a folder of the same name so if you're making a module called my awesome module you're going to have a folder called my awesome module and you're going to have a psm1 file called myawesomemodule.psm1 we'll go into that a little bit of it a little bit a little bit here but these are just some of the basics that you need to understand of how they work and last but not least there is this idea of a module path so there are certain folders on your machine that control uh where modules are stored and powershell is pre-programmed to look at those areas and if it sees any files there it will suck them in and make them available for you so if you're building a module we need to place it into one of those paths so that's available we can also import a module so that it's available for like one-time use and then when we close this session it's gone but if we want to keep it in a in sort of a permanent state there's some predetermined paths where things need to live we'll look at that in a second so that psd file which i talked about which is the manifest it's the data about data it's the info about the module so if i was to share a psm1 file with someone that would get them the raw code but i don't necessarily i haven't shared with them any information about what version of the module it is or who built it or even what i want to call my module all that information sort of lives in the manifest it's not a requirement but it's pretty much a standard file that would be included in any module that's built to be shared on a large scale basis and as i said this file is really describes the contents and the attributes that are contained in the module it's as i said module name version author if we're going to share it on the powershell gallery we need to have a unique guide so that it can tell these modules apart and on and on and on also you can use the psd to require prerequisites so if i'm building a bunch of scripts that require active directory i may want to put a requirement in there that says in order for this module to run you need to have the active directory module loaded so that people don't load the script and they get a bunch of red text when they try to run your commands and then they think you've wrote some really garbage code and they start wondering what happened so you can say hey i have some requirements that are needed for this module to run and if you don't meet those requirements then the module is not going to be able to run and be able to produce the code that you want and at the same time will also make them aware of what those requirements are as i said psd file is not required but if we're talking in practical terms if you're going to build a module you're going to have a psd it's an important piece of a module all right so that's it we're done with slides so let's take a look at some demos here all right so let me give you guys a little bit of the lay of the land of what we're going to talk about today i have possibly five demos that i could run through i'm not sure that we're gonna get to all five we can definitely do the first three which is about building modules and i've named these modules very simple straightforward names no one would ever name their modules these names but this is for demonstration purposes so what we've what we've got here is we have three modules that we're going to work with one's called demo module simple another one is going to show how you use a manifest and then we're going to move it up to when you would consider using like public and private data now i have some other stuff here for the other demos we'll see if we can get to it i would like to talk about how you can share code internally and online we'll see what we're allowed to get to today so now i also have this folder here called functions this has nothing to do with the module this is for me to organize my data for the presentation and answer the actual presentation files i'm going to show today for for for lack of a better description the only things that you need to be concerned with are these three folders here each one of these folders represents a different module now inside these modules are going to be these three functions now for all three modules we have the same three functions because we're not here to talk about what the functions do we're really here to talk about how do we get started building a module and how the pieces work together so we're gonna use the same real quick yes you increase the resolution or the zoom level on your visual studio code makes it a little easier on folks reading um in chat you guys got to tell me how big they go looks like the bit rate is slow too enhanced better that looks great okay so yeah that'll probably all right so i'll try to make this as as readable as possible as we go but so you're going to see these three folders we're going to be working in this afternoon and these three folders are really going to be doing the same three functions which i'll go into in a second and i chose these three functions for a particular reason which we'll get into all right so i have some notes for myself here at the top let's let's clean this up a little bit and let's talk about what we got here all right so i'm going to just get some things set here all right so the first thing i'd like to show off is i mentioned oh that looks a little gross let's clean that up and do that again all right so we mentioned earlier a ps module path and that may not make a lot of sense to people so ps module path is a variable on your machine that contains a bunch of file paths that are relative to where code related to mic to powershell may live and some of these have specific uses some are geared towards a user so these are things this is a place where i could put things for myself these are more geared towards the whole like sharing a module to anybody who uses the machine and then we have some directories like this one here which isn't which you might say why would i want to see a version one modules folder that's really a relic of when powershell first was introduced this is where microsoft publishes all their modules so if we were to browse this folder you would see all the modules that most people work with there's lots of stuff in there so this is actually not a legacy thing it's just an old naming convention so we're going to be function forwarding um we're going to be focusing on getting our modules into one of these folders and how they work and then we're not even really going to be touching on that we're going to be importing mostly today but if you want to have your module available for people to use this is where they're going to have to go in one of these locations and they all vary a little bit like this is powershell 7 that's because i have powershell 7 loaded on my machine there's windows powershell which is geared towards powershell 5 which is windows powershell powershell 7 is technically powershell core and then you have some other folders so i wanted to explain that to you and the easiest way to see that is just by pulling up this variable and then you can split on the semicolon to make a little easier all right so these are the functions we're going to be talking about today i think by name they probably make a lot of sense on their own get drive info get pc info and get pc uptime so uh let me just clean this up and make sure that i don't have anything running all right good all right so first thing we're going to do here is load this all right so bear with me one second here presentation appeal okay so these are the functions okay i showed you already we're not going to use these we're going to use them but this path doesn't mean anything here they are and i'll show you how they work in a second now the first thing i want to start with is all my modules here they start with the word demo okay so you can take a look and you can see which modules are already installed on your machine by typing get module what we're going to end up with though is a list of everything that's installed on the machine what i'm interested in here is i would like to see if i have any of my demo modules installed already and i don't which is what i would expect if i'm doing my presentation to show you how we're going to install these so this is just to show you that we're starting with a clean environment all right now the first thing we're going to do is we're going to take a look at what a module looks like so if you remember in the first example uh one of the first slides i said that you could rename a file a psm1 a psm excuse me a ps1 to a psm1 and that's effectively what i've done here so if you remember i showed you that we had three functions now i didn't show you what's in the functions but i think we all know what most functions look like i've simply taken those three functions copied the code into one file called demo module simple psm because i'm calling my module demo module simple to highlight the idea that this is the simplest version of a module that you can make so by simply copying in these three functions to one file renaming it with a psm1 file i've created my first module it's that simple so now all i need to do is to import that module and then i can use it so i wanted to show here uh get command let's see something here okay [Music] okay get drive okay good [Music] okay so i just tested none of my mod none of my functions are loaded so here we're going to do we're going to do a simple import statement and what we're going to do here is i've just created some variables to help me get around the machine easier as just saving to some of these paths right here so what we're using is we're using a command called import module to actually take the module and import it into my machine to this and we simply have a path to the file so if i run this here i've now imported the module now if i go back here and i say get module right i want to switch to my oops i want to switch them i didn't want to do that sorry yes i want to switch to a different directory for a second and i want to show you uh that we now have a module installed so there we go we didn't have to do really any work at all and now we have a module and i'm going to shut off this bar for a second here and if you see here we have some basic info about a module so it picked up the name of the module from the folder and the psm1 itself but it doesn't know anything about version info it knows that it's a collection of scripts script module and this got cut off here but it you can see here uh there we have it knows that there's three commandlets in the module so not too bad we didn't really have to do any work we simply copied three files into a psm1 ran an import module statement and there you go you've made your first module and if we were sharing this with someone else they would simply have to type import module and browse to the file and you have now shared your code with someone else probably a lot easier than doing net or emailing files to each other but this is really the basics we don't really know too much about the module if you're trying to get some credit for the work that you do and it gets circulated at work nobody knows that you've made these files or anything like that so we can definitely do better but at the very basics we've created a module and we've shared a module which a lot of people think is a challenging task on its own and actually you see here it's quite easy so now that i have the module loaded i can run my commands so if i run get pc info it's thinking comes back i'm in my lab environment here so it says the machine i'm honest pco1 lab information and we have some drive information here i have a separate drive info module that pulls up the drive info that's contained here there's also a way to export it and then last but not least we have some pc uptime and there's a reason that i selected these modules so i'm going to start these commands that i that i got into now so what you may not realize by looking at this code is is that this get pc info commandlet calls this get drive info commandlet behind the scenes i've built the module out of the command the function out separately so that you can run get drive info if you wanted to but if you notice here when we did get pc info you'll see that there's a drive info here and i could have pulled that info out do a variable give that a second come back and then i could say here foo and i'll say format table foo dot excuse me drive info all right so and we're going to talk about this more in a little bit so this get drive info can run on its own but it's also a sub file for this get pc info so when git pc info is run it reaches out to get drive in both behind the scenes and goes and gets some information that's going to come to be a little bit more important later on when we talk about public and private folders so um i'll just check in with the guys any questions so far now you're doing good yeah i'll get here all right thanks sir looks great so let's let's get our environment set up here again so that's really all that is to our simple module okay so now i'm going to move over here and we're going to take it up a little bit we're going to introduce the concept of a manifest so this is all just noise for me my we really start here and what i want to do is i first want to show you what a mod what a manifest looks like so as you may have remember when i started i talked about the psm1 file and the psd1 file and we said that the psd1 is the data about the module so if we click on that we look at that file we can see here there's a file that's created here with 150 lines of info we're not going to go through it all i just want to highlight a few sections so this is the info about the module and you can see it has a path to it has a module number it has a guide that has an author i didn't put a company name and it's got some copyright info and there's other information here that i could use if i wanted to like require modules things like that they're all commented out now at first blush you might say to yourself wow i really was not interested in typing up 150 lines of information about my module and i'm not expecting you to do that as well what i'm showing you here is a mod a psd file that's been pre-produced so in order to create a psd one file there is a command called new module manifest and what that will do is that will suck in some information that you provided and it will create this file so that you don't have to do the work so if we back up here just a little bit i have this created already so in order for this to work i need to get rid of that file because we already have one and we want to make one fresh so let's rename that file so you see here we have an old so now we're missing a psd one for this module here all right and if we come down here i've built a little hash table of some basic info that i want to pass to my uh my command that's going to build the manifest hash table is just simply a combination of keys and values so i'm setting here there's a key well there's a field called path and this is the value i want to populate in there so in essence i'm saying hey i want to pass these four keys of information to the manual uh to the manifest commandlet that's going to build the commandment and as you can see down here i can now show you what this uh let's load this so here we are and then i can run i can just pull that up hey now i should have just you missed the last line of the half stable ah thank you sir let's try that again it's good to have friends okay okay so here we have a manifest and like i said name and value so we have four values four fields or value uh keys and four matching values that are going to go with that so now that that's loaded into memory we've done this we're going to pass that hash table to this command so when we do that the key the thing here is you should see a new file show up over here so if we do this what did i do wrong i have a path you're in the wrong path yeah yeah yeah so let me get oh why did i take that off all right so any cd to it thank you all right let's try that again so now we're going to run this and call that hash table okay so you see over here we now have a file that was just created so let's take a look at that file it's going to look very similar to the one we just created uh um to the one i just showed off and let's make some room here but you can see here it's generated today it's got my name right so it's the information i passed to it and i put a random version number in here a quick blurb about version numbers this is really you could put anything that you like some people believe that you would put version one when you think that your code is sh is good to be shared with production some people start at zero zero zero i just use 0.6 to illustrate a difference here it created a good for me i never specified that it did that for me it plugged in my name is the author i didn't pass any company company info but here it made me look official with some legal info and the rest of the stuff is commented out because the rest of the stuff that's here is not a requirement even though these lines are not commented um these are these are saying export all the commands we'll talk about that in a second the rest of the stuff for what we're doing at this point is not really valid so now that we have a manufacturer so one of the questions there um mike is that the reason you'd want to do this is just to add these other context information to it so the reason is that is that correct why you'd want to the difference between this module and the next one the first one and this one is just use the same code but just now i want to add some extra detail to the module so that i can share it better is that one correct so so that is correct so what we're doing here is we've gone from the first module which was just simple code and then we've added in information about the module so we have a little more detail and if and at first glance when you're looking at this you say well maybe i don't really need a manifest file but we're going to move on to the next example soon and we're going to start to talk about public and private data and other things that we can do and when we get to that point a manifest is going to become a requirement because we won't be able to do those things without the manifest so if your intention is to take 50 scripts and make a quickie module and share with someone and have them let them have access to all 50 scripts and you don't care about having an author name or a module version number or anything like that then obviously just creating a simple psm1 file is fine but if you need to start to do some of the more advanced things that come with the module a manifest is going to become a required piece of information and i'm going to show that here shortly are the reference things that we can do uh well so we're going to talk about when we get to public and private how we can have code in different folders and how we show different functions to people and we hide different functions that's really what i'm going to focus on today but you could also add in helper scripts you can add in aliases we talked about those require statements in the beginning if i want my module to not run unless there's some required modules loaded on the machinery all that stuff is going to come down via the manifests so remember that psm1 file that's think of that as raw code if you have any sort of special situations that you need to design for you can't do that in a psm you need the psd to handle all those situations for you hiding modules making requirements version information help stuff all that stuff comes in through them to the manifest but i didn't start there because i wanted people to see that it isn't a required piece of information for simple code sharing do you think that i covered that in enough detail um okay great so let's come back to this public private in a second let's look at this manifest for a second so um i'll get my bearings okay so now we're going to do exactly what we did in the last example is we're going to import that module okay and with any luck we'll import this so now if you remember from earlier examples if we want to see what modules we have installed we use the get module command and in this case i want to limit my view to the modules that start with demo so now we have two modules installed and it's the same functions in both modules we're not here to talk about the functions today we're here to talk about the other stuff so you see in our first example we don't have any version information and we have just demo modules simple now in this second iteration we have the name but we also picked up the version but if i then look at this module again and i say format list and i pull this up a little bit you can see we're starting to get a little bit more information here about the module okay um but we still really haven't done a whole bunch i would say now i want to caution people here if if you have no experience with modules i want to start talking about using the mod the manifest in a little bit more advanced way this is an awesome place for you to stop i don't want you to stop watching the demo but if you've never done this practice with the two modules that i've made get good at that because once you start getting into public and private hiding require statements and all that good stuff it gets more complex and you can get yourself confused so i would caution that if you've never done this before you maybe just don't want to jump into some of the detailed stuff if you're up for a challenge and you think you can handle it obviously go for it but build yourself up so you have some confidence all right so if we move on here we don't need that open anymore we can look at this so now what we're going to do is we're going to look at this public private scenario so what we're looking to do here is remember we have three functions and what i'm looking to achieve in this demo is to say hey you know what this get drive info that's the command that i said was being called by get pc info i want to make that invisible to the end user okay so first let's take a look that's the goal that we're trying to go for here so first let's take a look at what we have here so now if we go and look at this module and i'm going to minimize some stuff here to make it easier to see if we look at this module we now have some subfolders we have a public and a private area we still have a psd-1 and a psm-1 but now we have a public at a private as i just started to mention the psm-1 now is different because we have to deal with folder paths so if we look at the uh the psm1 this is extraneous text it's supposed to be here so you see now i have to load my paths and i have to pull those file names in right so that's what i'm doing here i'm i'm basically telling the module through the psd i mean in the psm1 where these files are located in the folder structure all right and so again this is information that wouldn't normally be there so my psm now instead of having a bunch of functions in there i now have placed my functions inside these folders and i'm referencing them here right so we're starting to get a little different in the way that we're building this so are you saying that through in looking at that you can see that the public and the private are just reference names you can call it foo and bar you can call it you know don't look at you can call these folders anything you want and in the psm1 those files are referenced directly and so you just reference them in whatever way you want to and and that is exactly right so if we were to take this farther if you were to look at some modules by someone like jeff hicks who creates tons of modules he may have a different folder structure than public and private but he may also have 5 or 10 or 15 extra folders that are important to his module the structure isn't so much as important as long as the module pieces know how to navigate through that module and find the pieces that they need so i've gone and used public and private because it makes it very simple to understand the concept private is going to be code that i think people aren't going to see and public is code that i expect people will see and as i said this is not a security feature this is really an organizational feature as phil said we could call the foo and bar or anything we want what i'm doing here is organizing my data in a way that makes sense when i build the module so that i understand what i'm doing again for most people they're going to get files passed along to them and they're just going to suck them in and it's going to work the way i intended they may never look at this however because these are just files someone can open up this folder and these files and look at it so i would not advocate by making a public and a private or anything like that that you are actually hiding information from people you're just sort of organizing data in a meaningful way all right so let's take a look at this for a second here uh public private okay so here we have a psm and i had made a psd already i'm not going to do it again but if we see psd now now the psd gets a little bit more interesting so we're seeing the same stuff that we saw before this is an old one i created i'm reusing but if you come down here i have this section called functions to export and this is related to what i'm calling public they're not the same what i'm saying here is in this manifest these two files are the files i want to expose to the end user and then i think i have so that's it so even though i'm putting files into folders i'm stating through the manifest which files people are going to get to see so i could have put these all in one folder and just said hey i only want to export these two and i would have had the same net result but you also have to remember something if you're looking at doing something like this and you build a module and you go back to doing your daily work and you come back to that module in a month two months or six months maybe you want to do some updating if you don't build yourself some structure to understand how things work you're going to have to go back and pick through all the files to understand how you built that structure and what you'll see in some of the automation tools that build modules is they build out a structure for you to populate and then you just place your data in there so again you don't necessarily have to have this structure per se but without it it makes it very hard to figure out later on when you come back you have to really go through and start over and figure out everything in order to say what was i doing here so for myself i've built in a public and a private just for organization sake this functions to export what's going to happen is when we read in the module we do our endpoint statement and that reads the psd it's going to tell the computer remember when we had uh when we went up here we looked at our module statements and we saw that there was three functions exported well that's what this is here well now we're going to say only export 2. so on the next iteration of the module that we import it should only show two commandments so let's do that oh i need some space here because it's a little hard to see with all this big stuff here all right so let's browse down here um i'm not going to run this but i want to show you that i made a new madness file and it incremented it up which we'll see in a second here i could have run the manifest i could have done create a new manifest and done all the same things nothing is different here same exact process and now i'm simply going to import my function here and now if i'm going to just make this a little easier to see if i go up to get module and again we're looking at demo now you can see i have three modules in here and now you can see the function the export function in in actuality so the module that we imported here actually has three functions in there but to the user who doesn't know any different they only see two and we know that it's still there so what i'm going to do is i'll install the uninstall these modules in a second because i would like to run these and show you that it still works but because i've loaded this module three times in the same command let's if i go and i call the command i can't really be guaranteed which version of the command is going to run so we're going to clean it up in a second to show that but the idea here is is that when we run get pct info we're still calling get driving for behind the scenes it's just that the user who's using the module doesn't realize there's a second mod of second function doing some extra work behind the scenes so now that we have imported this here what i want to do is i want to remove some modules all right so here i just removed one module right now i'm going to go and remove the other one i'm going to say with okay so now we just have our one module installed and again the reason for doing this is just to highlight how these functions work so now let's test our command let's let's make sure they still work right let's start with get pc uptime we haven't saw that one i don't think right so this is just a simple function i built right that works right now if i go and i want to run get pc info and what we should see here is we still have that drive info but if you remember from earlier we talked about the piece that did that as another commandlet so now let me try and run that commandlet get drive info now yeah i know you can't see what i'm doing but i'm hitting tab that function should have auto completed if it was installed which means since it's not completing it's not installed however we know that we got information back so we know behind the scenes that get pc info called this module i'll call this command line so does that make sense what we did there yep yep absolutely all right so do we have questions from the community about anything that i've done no nothing in the chat okay well we wanted to stick to about 45 50 minutes this is a good breaking point we could talk about this a little farther but i have some more information i could pass along all depends on time we have a left well i think one of the things that we could just discuss is that some of the reasons why you'd want to do some of that stuff and i think is right is that why would you want to create some private functions just strictly to keep things private or not so much private or just to have some extra work in the background and what other things would you want to organize within your functions well so i we can leave that open for discussion because i think all of us probably have our own reasons as to why we would do that i think for the most part people hide functions either because they're gathering some information that they don't necessarily want someone to be able to see easily but is required for some other part of the module or very simply i'm making a module for a group of individuals that just need to do one or two things and they don't understand that those one or two things are dependent on three or four or five or ten other things they just want one or two commandments so it's a simplicity thing more than anything there are other reasons why you could hide commandlets but i the most the reason i think of mostly is to take away functions that don't really have any value to the end user so another example i'll give of that is because i hid that get drive info function i could have called it anything i wanted and then there is this idea that when people start hiding code let's say or make it private or not exported that they use different function names to know that if they got an error that that came from a private function versus a public function now those are not requirements those are really best practices everybody sort of has their own method of how they work but the idea is if you run some code and it produces an error and the error happens with one of your private functions if you put an oddball name in the error message that is a tip when you get the feedback from the user that they received an error that it wasn't the exported function that had a problem it's this the function behind the scenes that may be not working as expected well does it kind of make sense to actually you know only expose or export the functions or the commandlets you know as you decide for that that the users are supposed to actually be running and so it could be doing other work but you know hey i need to do some logging stuff i need to do all that kind of stuff and you want to modular you want to create functions for them so that it's better to manage but then also you know it might be a good idea to you know you don't need to expose that to the user not that you're trying to hide it it's just like why do you want to clutter somebody's you know uh tab complete with all the stuff that that then you know injected to other people's work so i think one of the things is that private is to try and you know if the function needs to do some work well that can be private but if you want the user to actually interact with the module then that's what stuff you put in pr in public so that they're the ones interfacing with this you know there's no reason why somebody needs to you know the logging parts of your function if that's part of it right another way that you could explain and phil is 100 right there and there's no right or wrong here um it comes down to use case what do we usually do for executive summaries for senior leaders in our organizations they get the one paragraph version of the technical document right use that as an example here to say hey i want to give someone the summary view of what's happening if i have to build a spreadsheet and behind the spreadsheet i produce nine tables of information and then at the end i combine those nine tables of information into the final result maybe that person doesn't need to see that nine tables of temp data but they just want to see the final result that's just an example of a way that you may want to think about it to conceptualize why you might want to obscure some of the data from someone because maybe it's just not pertinent to what they need to do what i didn't show in here is is how to do the require statements or anything like that that there's a lot of time you could spend just in the psd alone um making lots of different examples of things that you could do to customize your environment my goal here today was to take someone who had no experience creating a module and giving them the confidence that with some simple examples they could find a way to easily share some code so we could obviously go deeper into doing more things but the goal today was really just to make it seem tangible enough that someone might want to go back and say you know what i think i could actually do this so i kept it to very simple examples i think any module that you find online that you're going to download or consume from a vendor is going to be much more advanced than what i've shown you today but this is the stuff you need to understand if you ever want to go into the world of making your own modules and sharing your code so i hope that people enjoyed what i had to show we can talk about any individual pieces and i i would like to show how you can share that code online or internal repositories but i don't think we have the time to cover that today there's a lot more involved with that and this is probably a good breaking point to give people enough information to absorb and take it home with them and work on i'd like to point out mike that all of the demo files are available on your github which i've been following along through your presentation and so if a person wanted to get started with a scaffold and basically follow your video start to finish and see each iteration it's all put it up there so thanks for that i think we'll probably provide a link via youtube um or it'd be the community through uh twitch slack discord and all the communication channels so i really liked it because again if you have a place to start you can make a comparison with a more complex module and you can know you know if you've got a place to start from you know where you can get going as far as module development is concerned so it's really great yeah i don't think uh didn't you guys uh about a year or so ago at the research triangle powershell user group i'm that you and uh phil and kevin and i think there was one other i think there's a presentation on youtube that has how to publish to the gallery as well so the uh the funny part about that statement is we actually did that for your group as well james i don't think mine was recorded though no um so i would say that um my knowledge has come along a great amount since i did that presentation but the idea of our group and that presentation is uh phil myself and kevin lox one of the other leaders of the group we sometimes do presentations where the presentation isn't necessarily complete polished like this is reading a manual on how to do something that particular presentation was phil myself and kevin saying we need to share code with our well co-workers here's three different ways that we achieve that goal let's just show you how we did it and we were cognizant enough to say during the presentation that these are not the only ways to do it um and you may have better ideas but we were trying to share with the community some examples of how you could share code i think kevin was using box.com shared folders phil was doing if i remember correctly a file share internally but not a new get repository and i was doing a nuget repository where you could publish a repository uh so get ps repository we didn't talk about this but this is a list of the repositories that your machine knows about and the only one that comes pre-installed is the powershell gallery but it's untrusted so um i have some other information which i could share if it's a future date where you can then publish your own powershell nuget repository internally and then you could take these files and put it in the repository and then nobody needs to do any of these import statements if they set up their machine with the setting that and make it a trusted gallery which you could push through a group policy or a reg key or a startup script then when they open up their powershell command prompt and you have your module in one of these trusted locations they just have the code and there's nothing for them to do so that's really the part two here um but i will tell you from doing this myself it's very easy to get yourself tangled up very quickly so that's why i really want to push the idea stick with the simple stuff and get with it really well and i'll give one other example of that last year i changed positions i went and worked for align technology and for before i left i worked for my old company i had produced some code for the company and a couple of my friends that said hey man i really would love to get that code from you um man i really would like to get that before you leave and i was like yeah no problem so i produced a psm one uh just like this i'm trying to make a habit of putting in help files into all my work so i didn't have to go and create a bunch of help files in the last two weeks to try to teach him how to use this stuff each function had its own help file so i gave them the psm one and i told them just you know do an import statement and they're like okay can you show me how well these functions work and i was like no every function has its own help file it is a self-sufficient file you can import it and you can run the help for any one of the functions and you actually don't need me anymore if you have questions about my functions i'd be happy to help you try to figure them out but there was enough information in there so that i could leave the company gracefully share the work that i did with my co-workers and give them enough information so that they could work after i was gone and still be able to be productive all right michael we appreciate it it is at the top of the hour so we are out of time for the day uh so john and phil thank you guys so much for uh for co-hosting with us mike thanks uh for a fantastic presentation like we said this will be posted on youtube uh in about in about a week or so we'll try to put your github remember for your github file on there uh so we'll start yeah so we'll see everyone again in two weeks september 15th at 10 a.m pacific uh 1 p.m eastern uh and then for the rest of the time zones we'll try to put the every time zone link out so that you know uh exactly what time to join in and also make sure to get your papers in for the 24 hour live stream other than that we'll see you guys in two weeks have a great day
Info
Channel: PowerShell.org
Views: 159
Rating: 5 out of 5
Keywords: twitch, games
Id: JRi5iCd6D3w
Channel Id: undefined
Length: 72min 49sec (4369 seconds)
Published: Sun Oct 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.