HEMM Part 3: Automating Function Creation

๐Ÿง  TL;DR

Use New-HEMMFunction.ps1 to automatically generate PowerShell function files from a standard HEMM template. It ensures consistent naming, metadata, and folder structure, making script organization easy and repeatable.

๐Ÿ“„ Overview

If you’re following the HEMM (Hay-Ellis Master Module) series for PowerShell development, maintaining consistency and structure in your functions is key. This script, New-HEMMFunction.ps1, automates the creation of new function files using a template. It injects metadata like the function name, category, and author, then saves the file in the correct subfolder.

This is Part 3 in the series, and it builds upon the previously discussed PowerShell Function Template by automating the otherwise manual setup process.

You can clone this from my git repo.

โš™๏ธ What the Script Does

  • Takes in a function name and category
  • Copies the base template file from .\common\FunctionTemplate.ps1
  • Replaces placeholders for:
    • Function name (Get-Something)
    • Author name
    • Date (YYYY-MM-DD)
  • Drops the new script in the appropriate subfolder under .\functions\

<#
.SYNOPSIS
    Creates a new PowerShell function from the standard HEMM template.

.DESCRIPTION
    Copies the function template from the common folder, renames the function, updates metadata, 
    and places it into the appropriate functions subfolder (e.g. network, files, etc).

.PARAMETER Name
    The name of the new function (e.g. Get-EmptyOUs).

.PARAMETER Category
    The subfolder in .\functions\ to drop the file into (e.g. activedirectory, network).

.PARAMETER Author
    Optional override for the author name.

.EXAMPLE
    .\New-HEMMFunction.ps1 -Name Get-EmptyOUs -Category activedirectory

.NOTES
    Author   : Ashley Hay-Ellis
    Version  : 1.0
    License  : MIT
#>
function New-HEMMFunction {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter(Mandatory)]
        [ValidateSet("activedirectory", "files", "network", "misc")]
        [string]$Category,

        [string]$Author = "Ashley Hay-Ellis"
    )

    # Paths
    $TemplatePath = ".\common\FunctionTemplate.ps1"
    $TargetFolder = ".\functions\$Category"
    $TargetFile   = Join-Path $TargetFolder "$Name.ps1"

    # Check template exists
    if (-not (Test-Path $TemplatePath)) {
        Write-Error "Function template not found at $TemplatePath"
        exit 1
    }

    # Check destination folder
    if (-not (Test-Path $TargetFolder)) {
        Write-Host "Creating category folder: $TargetFolder"
        New-Item -Path $TargetFolder -ItemType Directory | Out-Null
    }

    # Copy and update content
    $templateContent = Get-Content $TemplatePath -Raw

    # Replace placeholders
    $today = Get-Date -Format yyyy-MM-dd
    $templateContent = $templateContent -replace 'function Get-Something', "function $Name"
    $templateContent = $templateContent -replace 'Get-Something', $Name
    $templateContent = $templateContent -replace 'Ashley Hay-Ellis', $Author
    $templateContent = $templateContent -replace 'YYYY-MM-DD', $today

    # Save to target
    Set-Content -Path $TargetFile -Value $templateContent -Encoding UTF8

    Write-Host "Created function: $TargetFile" -ForegroundColor Green
}

๐Ÿงช Example Usage

New-HEMMFunction -Name Get-EmptyOUs -Category ActiveDirectory

This will:

  • Create a file at .\functions\ActiveDirectory\Get-EmptyOUs.ps1
  • Populate it with current metadata and structure
  • Use your standard HEMM-style function format

๐Ÿง  Benefits

  • Standardization: Enforces a consistent script layout
  • Efficiency: Saves time when creating new utility functions
  • Scalability: Keeps large function libraries tidy and easy to manage

Leave a Reply

Your email address will not be published. Required fields are marked *