Step-by-Step: Building & Leveraging PowerShell Modules with PSGallery

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today we're going to take a look at another episode of our Powershell tutorial series and today we are going to talk about modules we're going to talk about the PS gallery and we're going to talk about making your own oops your own modules all right so let's get straight into it [Music] foreign what is a Powershell module well let's get rid of vs code for a moment and bring up just the Powershell shell and make this a little bit bigger so all the commands we run in Powershell like for example get Dash date or get that service or clear get Dash child item all of these individual commandlets are actually stored in something called modules you can imagine a module as a collection of commandlets or at least on the base level a collection of commandlets our Powershell shell here has a number of modules pre-loaded into it if I do get that module you'll see there is a Powershell management and a Powershell utility and a PS Redline module these modules are the available modules currently in memory there are more modules available on my system though and then load in all of these individual commands quite a few under there so if I do get Dash module Dash list available on the other hand you will actually see all the modules that are on this computer available to load into memory Powershell actually indexes these as well so even though they're not completely loaded into memory I could actually call the commands that are inside those modules so for example if you come up here and look for a z so you've got things like AZ accounts for Azure accounts 80 billing for Azure billing and so on and so forth so if I run connect AC account to attempt to authenticate to azure and log in with an Azure account here and now I do get Dash module you'll notice that this module has automatically been loaded for me directly into memory AZ dot accounts previously in older versions of Powershell you would have actually had to do this with an import Dash module command before you'd actually be able to use those commandlets but since Powershell version 5 1 it indexes them automatically so how do we get more modules well there is something called install Dash module this command here is inherently linked into Powershell gallery here this is where people publish their different Powershell modules that they might want you to actually use so for example maybe you want to play with VMware you'll find there's all the Powershell modules for VMware nicely loaded in here for us like VMware dot image Builder and vmware.powercli which is the main module for VMware if you go and look for AZ in here you'll find all the Azure account services down here as well AZ automation azaks AC billing and so on and so forth you'll even find a lot of fun things like Posh you so if you have Philips hue light bulbs you can actually control Philips hue light bulbs inside your house with Powershell quite useful here let's go grab something a little more interesting that we can go and play with so let's go to the Powershell gallery and let's go and have a look at the packages that are available to us and we'll sort these by popularity now one of the ones that is most popular here is one called PS Windows update and this is very very useful this tool PS Windows update actually gives us Powershell control over Windows updates on your computer very very useful especially if you're trying to do things remotely with this module you can actually kick remote computers to initialize a Windows update immediately on that machine so to get this all I actually need to do is just grab this code here literally paste it directly into Powershell and by doing that this is going to actually look at my computer and say hey do you actually have this PS Windows update I don't so it's going to go off to the Powershell gallery and pull it down for use okay cool that's done so if I do get Dash module you'll notice it's not actually loaded yet but what I could do is I click import Dash module PS Windows update and always run that now if I do get let's just do a clear if I do get Dash module you'll now see we have PS Windows update nicely loaded if we do get Dash command Dash module here's Windows update you'll see all of the available commands are now loaded into my machine okay great so I can do something like get Dash Wu settings here and actually run that and we can see that there are no updates scheduled on this machine so that's how you can download an existing module that exists on the PS Gallery we don't have to do anything special it just revolves around that command called install Dash module now what about if I want to create my own modules well there's some extra things to learn so if I come and look at my shell here and I do CD EnV colon this is going to take me to my environment variable Powershell drive we'll discuss Powershell drives later on if I do a little bit of a dir down here this contains a whole bunch of different environment variables you could think of the environment variables like the settings for my Powershell environment like for example you've got my home path and my home drive down here as well you've got my OS and my OneDrive and number of processors and so on and so forth so some of these things are actually mapped to other variables like for example if you do dollar home this will actually always retrieve the data that's set in the home EnV settings if I pull back again dollar TMP that's also going to retrieve um oh dollar temp sorry oops dollar EnV colon temp there we go that is actually going to retrieve my temp drive over here now the one that I'm actually bothered about is this one here PS module path so if I do dollar EnV colon PS module path what we're actually going to retrieve is quite a long string I actually want to split the string up so it's a little bit more readable so let's take dollar EMV colon PS module path and just do a split operation on the end of this and I want to split based on semicolons here so we'll pass that as an argument what that's done is taking in this whole long string here and just chop that down so we can actually see what's going on these are the locations where our modules are actually stored notice there is a mycadmin documents Windows Powershell modules okay that's my current user there is also a program files Windows Powershell modules that's also a location where it's system based for modules so modules that go in there will be accessible by everybody you've got this Powershell V1 modules and we've also got again this monitoring agent for powers this one is just an additional on this Windows 11 machine you guys might not see that on Windows 10 or on your existing Windows 11 machines but these three will definitely be there now this might seem super flurious if that's the right word I don't know if it is um it's hilarious this might seem that you don't need it because it's Powershell version one trust me you do this module path contains our default modules do not delete this do not play with it um these two module paths are the module pass that will actually be used when you're downloading things like PS Gallery if I go and grab this module path see program files Windows Powershell modules and we go and open that up you'll actually see all the pre-loaded modules that I grabbed from the PS gallery and you'll also find down here we've also got that PS Windows update module notice what a module looks like it's actually a folder and it's a folder the inside contains a number of different things modules can be put together in a number of different ways but let's concentrate on the easiest way that we can actually do that so since a module is a collection of commandlets and the commandlet is a bit like a function let's go see what we can do with this so let's take my existing functions uh tutorial over here I'm just going to copy this code and I'm going to make another another file here I'm going to call this Mike's mod okay dot PS1 this is going to be our Powershell code so it's going to create that Powershell this is going to be sorry our Powershell module so we've got a couple of functions here get greeting and get square okay we had a look at this previously if we go and run it we can actually go and then do these commands get Dash greeting and just do Dash greeting you can do hello on there again then we'll return hello that's great that's because these are actually loaded in as functions if we do get Dash module at the moment you'll notice that there isn't actually a module loaded here but it has actually loaded these functions into memory after I've run the debugger so how do we convert this thing into a module that we can then load and reuse for people in the future so it's very simple all we need to do is just rename this not PS1 but if we come over here and rename this to PS M1 that's it this is now a module it can be loaded and unloaded into Powershell so if I open up here again in my shell let's look at the location of this so if we reveal this in file explorer we can see this is currently in C uses my cabin in documents Powershell code let's go grab that directory and let's just go and CD to that directory over here oops it's got a space in it so I'll have to put some quotes in there now if I just list this out you'll notice I've got Mike's mod.psm1 and all I have to do now is just import Dash module actually give it that path so Mike's mod.psm1 this file is not digitally signed you cannot run the script on the current system the reason I'm getting that error is because if I do get Dash execution policy my current execution policy is set to all signed and this is not signed code so what I'm going to do is I'm just going to set that execution policy to bypass for the moment so I can load in unsigned code and I'm going to rerun that import module it's done we don't get any confirmation of this if I just clear this down now if I do get Dash module you'll notice Mike's mod is loaded and both of the functions for Mike's mod get greeting and get square are currently loaded so if I now run get greeting oops get greeting and do a greeting on here it works so that's how we can very quickly load our own modules directly into Powershell so we've taken a look here at the PS gallery we've also taken a look at creating your own module literally all you need to do is just change that directory there or change that directory sorry change that file extension to psm1 to load in a simple module and then run the command of import Dash module and point it to the path so I hope you enjoyed this quick tutorial on the Powershell modules there is a lot more in-depth to Powershell modules especially when loading large modules that contain many different commandlets we might cover those in a future episode on a future video in the meantime I hope you enjoyed this content and I hope you'll enjoy me for the next in the Powershell series and you know the routine hashtag like And subscribe and I hope you enjoyed this video and will join me next time goodbye foreign
Info
Channel: Mike in the Cloud
Views: 1,652
Rating: undefined out of 5
Keywords:
Id: GKV8s3Zzj8M
Channel Id: undefined
Length: 13min 56sec (836 seconds)
Published: Fri Mar 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.