My first time creating a PowerShell module I copied a folder structure and some files from the way DBATools.io set up their module.  But Rob Sewell (b | t) has shown me a better way and that is to use Plaster. In this article, we will walk through setting up a PowerShell module and storing functions you find useful in it for use later. For prerequires, you should have PowerShell 5.0 or greater installed, you can download PowerShell 5.1 and install it from Microsoft's website here.  

The first thing we must do is install a module from the PowerShell Gallery called Plaster using your PowerShell command line or ISE of choice. To install a module from the PowerShell Gallery you simply use the command below:

Install-Module Plaster

After Plaster has been installed you have a few more steps to set up before you can use Plaster to create the module.  The most important is deciding on the default folder structure and content of your module.  Rob Sewell has provided a GitHub repository with a default structure that you can pull down and use. After pulling down Rob's template, it's becomes really simple to create a module.

Invoke-Plaster -TemplatePath TEMPLATEDIRECTORY -DestinationPath DESTINATIONDIRECTORY

In the example below I have the template in my GitHub folder locally and will create the function in my PowerShell directory (if you are going to create in the Program Files be sure to run the ISE or PowerShell in administrator mode):

Invoke-Plaster -TemplatePath C:\GitHub\PlasterTemplate-master -DestinationPath "C:\Program Files\WindowsPowerShell\Modules\DBAFunctions"

The output from the command is below.

Plaster Output

Now that you have a module it's time to add a function that you want to use to the module.  I'm going to add one I wrote for T-SQL Tuesday #94 for checking on the status of your availability groups.  So go here to copy the code into your PowerShell editor.  Then save the function as Get-AvailabilityGroupStatus.ps1 in your "C:\Program Files\WindowsPowerShell\Modules\DBAFunctions\functions" folder. Go back and follow the same directions to get the function Get-CMSHosts and save it as well.  Now you have a module with two custom functions in it and are ready to check the status of your availability groups.  There are several examples in the linked blog post but one is to check the status of the server using the following command:

Get-AvailabiliytGroupStatus -ServerInstanceList "Servername"

I do recommend as you collect functions from different places that you make notes in the header of the functions of where you got it from the blog article that describes how to use it for reference. For example for my function, you could add the blog post in the notes section.

<#
        .NOTES
            Tags: AvailabilityGroups
            Original Author: Tracy Boggiano (@TracyBoggiano), tracyboggiano.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
            http://tracyboggiano.com/archive/2017/09/t-sql-tuesday-94/
#>

Anonymous