-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
PSWriteOffice.psm1
129 lines (119 loc) · 4.74 KB
/
PSWriteOffice.psm1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue -Recurse )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue -Recurse )
$Classes = @( Get-ChildItem -Path $PSScriptRoot\Classes\*.ps1 -ErrorAction SilentlyContinue -Recurse )
$Enums = @( Get-ChildItem -Path $PSScriptRoot\Enums\*.ps1 -ErrorAction SilentlyContinue -Recurse )
# Get all assemblies
$AssemblyFolders = Get-ChildItem -Path $PSScriptRoot\Lib -Directory -ErrorAction SilentlyContinue
# Lets find which libraries we need to load
$Default = $false
$Core = $false
$Standard = $false
foreach ($A in $AssemblyFolders.Name) {
if ($A -eq 'Default') {
$Default = $true
} elseif ($A -eq 'Core') {
$Core = $true
} elseif ($A -eq 'Standard') {
$Standard = $true
}
}
if ($Standard -and $Core -and $Default) {
$FrameworkNet = 'Default'
$Framework = 'Standard'
} elseif ($Standard -and $Core) {
$Framework = 'Standard'
$FrameworkNet = 'Standard'
} elseif ($Core -and $Default) {
$Framework = 'Core'
$FrameworkNet = 'Default'
} elseif ($Standard -and $Default) {
$Framework = 'Standard'
$FrameworkNet = 'Default'
} elseif ($Standard) {
$Framework = 'Standard'
$FrameworkNet = 'Standard'
} elseif ($Core) {
$Framework = 'Core'
$FrameworkNet = ''
} elseif ($Default) {
$Framework = ''
$FrameworkNet = 'Default'
} else {
Write-Error -Message 'No assemblies found'
}
if ($PSEdition -eq 'Core') {
$LibFolder = $Framework
} else {
$LibFolder = $FrameworkNet
}
$Assembly = @(
if ($Framework -and $PSEdition -eq 'Core') {
Get-ChildItem -Path $PSScriptRoot\Lib\$Framework\*.dll -ErrorAction SilentlyContinue -Recurse
}
if ($FrameworkNet -and $PSEdition -ne 'Core') {
Get-ChildItem -Path $PSScriptRoot\Lib\$FrameworkNet\*.dll -ErrorAction SilentlyContinue -Recurse
}
# if ($AssemblyFolders.BaseName -contains 'Standard') {
# @( Get-ChildItem -Path $PSScriptRoot\Lib\Standard\*.dll -ErrorAction SilentlyContinue -Recurse)
# }
# if ($PSEdition -eq 'Core') {
# @( Get-ChildItem -Path $PSScriptRoot\Lib\Core\*.dll -ErrorAction SilentlyContinue -Recurse )
# } else {
# @( Get-ChildItem -Path $PSScriptRoot\Lib\Default\*.dll -ErrorAction SilentlyContinue -Recurse )
# }
)
# This is special way of importing DLL if multiple frameworks are in use
$FoundErrors = @(
# We load the DLL that does OnImportRemove if we have special module that requires special treatment for binary modules
# Get library name, from the PSM1 file name
$LibraryName = $myInvocation.MyCommand.Name.Replace(".psm1", "")
$Library = "$LibraryName.dll"
$Class = "$LibraryName.Initialize"
try {
$ImportModule = Get-Command -Name Import-Module -Module Microsoft.PowerShell.Core
if (-not ($Class -as [type])) {
& $ImportModule ([IO.Path]::Combine($PSScriptRoot, 'Lib', $LibFolder, $Library)) -ErrorAction Stop
} else {
$Type = "$Class" -as [Type]
& $importModule -Force -Assembly ($Type.Assembly)
}
} catch {
Write-Warning -Message "Importing module $Library failed. Fix errors before continuing. Error: $($_.Exception.Message)"
$true
}
Foreach ($Import in @($Assembly)) {
try {
Add-Type -Path $Import.Fullname -ErrorAction Stop
} catch [System.Reflection.ReflectionTypeLoadException] {
Write-Warning "Processing $($Import.Name) Exception: $($_.Exception.Message)"
$LoaderExceptions = $($_.Exception.LoaderExceptions) | Sort-Object -Unique
foreach ($E in $LoaderExceptions) {
Write-Warning "Processing $($Import.Name) LoaderExceptions: $($E.Message)"
}
$true
} catch {
Write-Warning "Processing $($Import.Name) Exception: $($_.Exception.Message)"
$LoaderExceptions = $($_.Exception.LoaderExceptions) | Sort-Object -Unique
foreach ($E in $LoaderExceptions) {
Write-Warning "Processing $($Import.Name) LoaderExceptions: $($E.Message)"
}
$true
}
}
#Dot source the files
Foreach ($Import in @($Private + $Classes + $Enums + $Public)) {
Try {
. $Import.Fullname
} Catch {
Write-Warning -Message "Failed to import functions from $($import.Fullname).Error: $($_.Exception.Message)"
$true
}
}
)
if ($FoundErrors.Count -gt 0) {
$ModuleName = (Get-ChildItem $PSScriptRoot\*.psd1).BaseName
Write-Warning "Importing module $ModuleName failed. Fix errors before continuing."
break
}
Export-ModuleMember -Function '*' -Alias '*' -Cmdlet '*'