04 / TESTING AND SCHEDULED AUTOMATION · LESSON 13

Package a local administration module

Expose a small, explicit module interface that another script can import and test.

12 min · learn at your pace

Put reusable code behind an interface

A .psm1 file can hold related PowerShell functions. Importing that file lets another script call the functions you deliberately export. For this lab, create Operations.psm1 with one public function: Get-CapacitySummary.

Keep calculations independent of input collection and report writing. The function accepts TotalBytes and FreeBytes, then returns FreeGiB, FreePercent, and State. That makes it useful whether observations originated in CSV, JSON, or a collector outside this browser.

Import-Module '/home/user/Example.psm1' -Force
Get-OfficeLabel

Export only the public surface

Export-ModuleMember -Function Get-CapacitySummary makes the intended interface explicit. Without an export declaration, script-module functions and aliases are normally exported by default. An internal unit-conversion helper does not need to become a command that callers depend on.

You may calculate GiB directly or use a private helper. The checks inspect the exported commands and returned values, not how you arrange the internal implementation.

Save the module using a single-quoted here-string if writing it from the lesson editor. Import with -Force while iterating so updated definitions are loaded. Required modules and tests are local; the exercise needs no Gallery access.

Preserve the capacity contract: invalid measurements are Unknown/null, exactly 20 percent is OK, and a raw value just below 20 remains Low even if its displayed value rounds up.

Go deeper

Read the official reference.

Try an example.ps1
Import-Module '/home/user/Example.psm1' -Force
Get-OfficeLabel
YOUR TURN

Put it into practice.

Create /home/user/Operations.psm1 exporting only Get-CapacitySummary -TotalBytes -FreeBytes. Return FreeGiB and FreePercent rounded to two decimals, plus State (Low below an unrounded 20%, otherwise OK). Invalid capacity (total <= 0, free < 0, or free > total) returns Unknown with both numeric properties null. Private helpers are optional. Import the module to try it.

See an explained solution
One way to solve it.ps1
@'
function ConvertTo-GiB {
    param([double]$Bytes)
    [math]::Round($Bytes / 1GB, 2)
}
function Get-CapacitySummary {
    param([double]$TotalBytes, [double]$FreeBytes)
    if ($TotalBytes -le 0 -or $FreeBytes -lt 0 -or $FreeBytes -gt $TotalBytes) {
        return [pscustomobject]@{ FreeGiB=$null; FreePercent=$null; State='Unknown' }
    }
    $ratio = 100 * $FreeBytes / $TotalBytes
    [pscustomobject]@{ FreeGiB=(ConvertTo-GiB $FreeBytes); FreePercent=[math]::Round($ratio,2); State=$(if ($ratio -lt 20) { 'Low' } else { 'OK' }) }
}
Export-ModuleMember -Function Get-CapacitySummary
'@ | Set-Content '/home/user/Operations.psm1'
Import-Module '/home/user/Operations.psm1' -Force
Get-CapacitySummary -TotalBytes 100GB -FreeBytes 19GB

The module exposes one stable command while retaining freedom to change its internal helper. Testing the saved file proves that another script can import and use the same capacity contract.