| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This example shows how PSScriptBuilder builds a PowerShell module instead of a standalone script. The output is a .psm1 file that is loaded via using module in a separate demo script, giving callers access to the module's types (ConfigEntry, AppConfig) as strongly-typed objects.
using module makes the types defined in a module available as first-class PowerShell types in the consuming script. Without it, objects returned by module functions have the correct data but their type names are not accessible for things like [ConfigEntry] parameter types or $obj.GetType().Name checks.
Why a separate demo script? The using module statement must appear before any executable code in a script file. Because Run-Example.ps1 has already executed code (the build), it cannot issue using module itself. Calling a child script with & starts a clean parsing scope where using module is valid.
The module manifest (AppConfig.psd1) is committed to source control and points to AppConfig.psm1 as the root module. PSScriptBuilder only generates the .psm1 body - the manifest stays unchanged across builds.
09-module-build/
+-- Run-Example.ps1 Entry point - builds module, then calls Demo-Module.ps1
+-- Demo-Module.ps1 Loads built module via 'using module', demonstrates types
+-- README.md This file
+-- psscriptbuilder.config.json Build configuration
+-- src/
| +-- Classes/
| | +-- ConfigEntry.ps1 Class: Key, Value, Description; ToString()
| | +-- AppConfig.ps1 Class: Name, Entries; Add(), Get(), Contains(), Count()
| +-- Functions/
| +-- New-ConfigEntry.ps1 Factory function, returns [ConfigEntry]
| +-- New-AppConfig.ps1 Factory function, returns [AppConfig]
| +-- Get-ConfigValue.ps1 Gets a value by key, supports -Default parameter
+-- build/
+-- Templates/
| +-- AppConfig.psm1.template Template with {{CLASS_DEFINITIONS}} and {{FUNCTION_DEFINITIONS}}
+-- Output/
+-- AppConfig.psd1 Module manifest (pre-existing, not generated)
Warning: Run in a fresh PowerShell session
The using module statement loads types into the current session.
Reloading or switching between examples in the same session can cause type conflicts.
.\Run-Example.ps1For detailed build output:
.\Run-Example.ps1 -VerboseBuild Summary Output: ...\build\Output\AppConfig.psm1 Size : 2.02 KB Time : 1.03 s Components Classes : 2 Functions: 3 Total : 5 --- Running Demo-Module.ps1 --- Config : AppSettings Entries : 4 Environment = Production # Deployment environment LogLevel = Warning # Minimum log level MaxRetries = 3 # Maximum retry attempts Timeout = 30 Environment : Production ApiKey : (not set) Type of entry : ConfigEntry Type of config : AppConfig
| Back | FazBrowse Home | New Git URL |