MyPowerShellTemplates.psm1
|
#requires -version 5 <# .SYNOPSIS MyPowerShellTemplates module - PowerShell development toolkit .DESCRIPTION Root module file for MyPowerShellTemplates. This module provides comprehensive tools for creating PowerShell functions, scripts, and modules with best practices and progressive complexity levels. Module metadata (version, author, etc.) is defined in MyPowerShellTemplates.psd1 .NOTES For module information, see the module manifest (.psd1 file) Author: numidia Module Version: See MyPowerShellTemplates.psd1 #> #region Initialisations Set-StrictMode -Version Latest #endregion Initialisations #region Modules ## Import-Module ActiveDirectory #endregion Modules #region Declarations # Get module version from manifest (single source of truth) $manifestPath = Join-Path -Path $PSScriptRoot -ChildPath 'MyPowerShellTemplates.psd1' $manifest = Import-PowerShellDataFile -Path $manifestPath $Script:PstModuleVersion = [version]$manifest.ModuleVersion $Script:ModuleRootFilePath = $MyInvocation.MyCommand.Path $Script:ModuleRootFileName = $MyInvocation.MyCommand.Name $Script:ModulePSScriptRoot = $PSScriptRoot $Script:PublicPath = Join-Path -Path $Script:ModulePSScriptRoot -ChildPath Public $Script:PrivatePath = Join-Path -Path $Script:ModulePSScriptRoot -ChildPath Private # Global automation mode variable - when $true, bypasses all interactive prompts if (-not (Get-Variable -Name PstAutomationMode -Scope Global -ErrorAction SilentlyContinue)) { $Global:PstAutomationMode = $false } # Helper function to check if we're in automation mode function script:Test-AutomationMode { return ($Global:PstAutomationMode -eq $true) -or ($env:CI -eq 'true') -or ($env:GITHUB_ACTIONS -eq 'true') -or ($Host.Name -eq 'ServerRemoteHost') -or (-not [Environment]::UserInteractive) } #endregion Declarations #region Execution try { Write-Debug -Message "'$($Script:ModuleRootFileName)' module import starting at '$(Get-Date)', version ''" Write-Verbose -Message "get Public Functions in '$($Script:PublicPath)' and Export them if ps1 files exist " if (Test-Path "$($Script:PublicPath)\*.ps1") { Write-Information "'$($Script:PublicPath)' contains .ps1 files, dot sourcing files" Get-ChildItem -Path "$($Script:PublicPath)\*.ps1" | ForEach-Object { Write-Verbose "dot sourcing '$($_.FullName )'" . $_.FullName } Export-ModuleMember -Function (Get-ChildItem -Path "$($Script:PublicPath)\*.ps1").BaseName } else { Write-Warning "'$($Script:PublicPath)' does not contain any .ps1 files." } Write-Verbose -Message "get Private Functions in '$($Script:PrivatePath)' and Export them if ps1 files exist " if (Test-Path "$($Script:PrivatePath)\*.ps1") { Write-Information "'$($Script:PrivatePath)' contains .ps1 files, dot sourcing files" Get-ChildItem -Path "$($Script:PrivatePath)\*.ps1" | ForEach-Object { . $_.FullName } } else { Write-Warning "'$($Script:PrivatePath)' does not contain any .ps1 files." } # get Resources and create FileHashTables varuables $Resources = Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath Resources) -Directory foreach ($Resource in $Resources) { Set-Variable -Name $Resource.Name -Value $(Get-FileHashtable -Path $Resource.FullName) } Write-Output "'$($Script:ModuleRootFileName)' imported successfuly at '$(Get-Date)'" } catch { if ($_.Exception -and $_.Exception.Message) { Write-Error "An error occurred: $($_.Exception.Message)" } else { Write-Error "An error occurred, but no additional information is available." } } finally { Write-Debug -Message "Finally $($Script:ModuleRootFileName) at '$(Get-Date)'" } #endregion Execution |