🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Project: Admin Toolkit

An admin toolkit is a module that groups small report functions into one loadable file. It lets one driver script print disk, service, and user reports together.

Build a disk report from a created file

The module reads a file the driver created. Each line in that file becomes one disk line in the report.

$modulePath = Join-Path $PSScriptRoot "AdminTools.psm1"
Set-Content -Path (Join-Path $PSScriptRoot "disks.txt") -Value @("C: 120 GB free", "D: 40 GB free")
Set-Content -Path $modulePath -Value 'function Get-DiskReport { $path = Join-Path $PSScriptRoot "disks.txt"; foreach ($line in (Get-Content -Path $path)) { "Disk: $line" } } Export-ModuleMember -Function Get-DiskReport'
Import-Module $modulePath
Get-DiskReport

Build a service report from fixed data

Some reports need no file at all. A fixed table holds each service name with its status.

$modulePath = Join-Path $PSScriptRoot "AdminTools.psm1"
Set-Content -Path $modulePath -Value 'function Get-ServiceStatus { $table = [ordered]@{ Print = "Running"; Backup = "Stopped" }; foreach ($name in $table.Keys) { "Service: $name is $($table[$name])" } } Export-ModuleMember -Function Get-ServiceStatus'
Import-Module $modulePath
Get-ServiceStatus

Keep a helper private and prove it

The helper formats one user line, but only the report function is exported. The command list proves the helper stays hidden.

$modulePath = Join-Path $PSScriptRoot "AdminTools.psm1"
Set-Content -Path $modulePath -Value 'function Format-UserLine { param([string]$Name, [string]$Role) "$Name ($Role)" } function Get-UserReport { $table = [ordered]@{ Ana = "Admin"; Ben = "Reader" }; foreach ($name in $table.Keys) { "User: " + (Format-UserLine -Name $name -Role $table[$name]) } } Export-ModuleMember -Function Get-UserReport'
Import-Module $modulePath
Get-UserReport
Get-Command -Module AdminTools | Select-Object -ExpandProperty Name

Example

$modulePath = Join-Path $PSScriptRoot "AdminTools.psm1"
$manifestPath = Join-Path $PSScriptRoot "AdminTools.psd1"
Set-Content -Path (Join-Path $PSScriptRoot "disks.txt") -Value @("C: 120 GB free", "D: 40 GB free")
Set-Content -Path $modulePath -Value 'function Get-DiskReport { $path = Join-Path $PSScriptRoot "disks.txt"; foreach ($line in (Get-Content -Path $path)) { "Disk: $line" } } function Get-ServiceStatus { $table = [ordered]@{ Print = "Running"; Backup = "Stopped" }; foreach ($name in $table.Keys) { "Service: $name is $($table[$name])" } } function Format-UserLine { param([string]$Name, [string]$Role) "$Name ($Role)" } function Get-UserReport { $table = [ordered]@{ Ana = "Admin"; Ben = "Reader" }; foreach ($name in $table.Keys) { "User: " + (Format-UserLine -Name $name -Role $table[$name]) } } Export-ModuleMember -Function Get-DiskReport, Get-ServiceStatus, Get-UserReport'
New-ModuleManifest -Path $manifestPath -RootModule "AdminTools.psm1" -ModuleVersion "1.0.0" -FunctionsToExport @("Get-DiskReport", "Get-ServiceStatus", "Get-UserReport")
Import-Module $manifestPath
"Admin Report"
Get-DiskReport
Get-ServiceStatus
Get-UserReport
"Exported: " + ((Get-Command -Module AdminTools | Sort-Object Name | Select-Object -ExpandProperty Name) -join ", ")

Key points

  • Module: a script file with a .psm1 ending that holds functions for reuse.
  • Use Export-ModuleMember to choose which functions leave the module.
  • Private helper: a function inside the module that is not exported and cannot be called from outside.
  • Use Get-Command -Module to list only the functions a module exports.
  • Manifest: a .psd1 file that describes a module with its name and version.
  • Driver script: a short script that imports a module and calls its functions.

In this example, the driver creates a disk data file and a module with three exported functions and one private helper. It then creates a manifest for the module and imports the module through that manifest. It prints the disk, service, and user reports, then lists the exported functions to show the helper is not among them.

Note

$PSScriptRoot is the folder that holds the running script, so paths built from it stay correct.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command lists only the functions a module exports?