-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostLoad.ps1
More file actions
61 lines (55 loc) · 2.64 KB
/
Copy pathPostLoad.ps1
File metadata and controls
61 lines (55 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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.
# Call it immediately to avoid PSScriptAnalyzer 'PSUseDeclaredVarsMoreThanAssignments'
$ThisModuleLoaded = $true
$ThisModuleLoaded
# Non-function exported public module members might go here.
#Export-ModuleMember -Variable SomeVariable -Function *