plaster/ModuleBuild/scaffold/src/other/PostLoad.ps1
# Use this variable for any path-sepecific actions (like loading dlls and such) to ensure it will work in testing and after being built $MyModulePath = $( Function Get-ScriptPath { $Invocation = (Get-Variable MyInvocation -Scope 1).Value if($Invocation.PSScriptRoot) { $Invocation.PSScriptRoot } Elseif($Invocation.MyCommand.Path) { Split-Path $Invocation.MyCommand.Path } elseif ($Invocation.InvocationName.Length -eq 0) { (Get-Location).Path } else { $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\")); } } Get-ScriptPath ) # Load any plugins found in the plugins directory if (Test-Path (Join-Path $MyModulePath 'plugins')) { Get-ChildItem (Join-Path $MyModulePath 'plugins') -Directory | ForEach-Object { if (Test-Path (Join-Path $_.FullName "Load.ps1")) { Invoke-Command -NoNewScope -ScriptBlock ([Scriptblock]::create(".{$(Get-Content -Path (Join-Path $_.FullName "Load.ps1") -Raw)}")) -ErrorVariable errmsg 2>$null } } } $ExecutionContext.SessionState.Module.OnRemove = { # Action to take if the module is removed # Unload any plugins found in the plugins directory if (Test-Path (Join-Path $MyModulePath 'plugins')) { Get-ChildItem (Join-Path $MyModulePath 'plugins') -Directory | ForEach-Object { if (Test-Path (Join-Path $_.FullName "UnLoad.ps1")) { Invoke-Command -NoNewScope -ScriptBlock ([Scriptblock]::create(".{$(Get-Content -Path (Join-Path $_.FullName "UnLoad.ps1") -Raw)}")) -ErrorVariable errmsg 2>$null } } } } $null = Register-EngineEvent -SourceIdentifier ( [System.Management.Automation.PsEngineEvent]::Exiting ) -Action { # Action to take if the whole pssession is killed # Unload any plugins found in the plugins directory if (Test-Path (Join-Path $MyModulePath 'plugins')) { Get-ChildItem (Join-Path $MyModulePath 'plugins') -Directory | ForEach-Object { if (Test-Path (Join-Path $_.FullName "UnLoad.ps1")) { Invoke-Command -NoNewScope -ScriptBlock [Scriptblock]::create(".{$(Get-Content -Path (Join-Path $_.FullName "UnLoad.ps1") -Raw)}") -ErrorVariable errmsg 2>$null } } } } # Use this in your scripts to check if the function is being called from your module or independantly. $ThisModuleLoaded = $true # Non-function exported public module members might go here. #Export-ModuleMember -Variable SomeVariable -Function * # This file cannot be completely empty. Even leaving this comment is good enough. |