This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSSpiceworks.psm1
80 lines (61 loc) · 2.3 KB
/
PSSpiceworks.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
# Get all of the pub/priv scripts
$Scripts = @{
Public = Get-ChildItem "$PSScriptRoot\Public\*.ps1" -ErrorAction SilentlyContinue
Private = Get-ChildItem "$PSScriptRoot\Private\*.ps1" -ErrorAction SilentlyContinue
}
# Import them
foreach ($Type in @("Public","Private")) {
Write-Verbose "Importing $Type functions..."
foreach ($ImportScript in $Scripts.$Type) {
Write-Verbose "Importing function $($ImportScript.Basename)..."
try {
. $ImportScript.FullName
}
catch {
Write-Error "Failed to import $type function $($ImportScript.BaseName): $_"
}
}
}
# We don't have any libraries for the time being, so this isn't a concern
<#
Write-Verbose "Importing Libraries..."
$Assemblies = @{}
foreach ($Library in (Get-Childitem "$PSScriptRoot\Library\*.dll")) {
Write-Verbose "Importing $($Library.Basename)..."
try {
$Assemblies[$Library.BaseName] = Add-Type -Path $Library.Fullname -PassThru
} catch {
Write-Error "Failed to import DLL $($Library.BaseName): $_"
}
}
#>
$Classes = @{
ps1 = Get-ChildItem "$PSScriptRoot\Class\*.class.ps1" -ErrorAction SilentlyContinue
cs = Get-ChildItem "$PSScriptRoot\Class\*.cs" -ErrorAction SilentlyContinue
}
# Import classes defined via PoSH
$Classes.ps1 | ForEach-Object {
$Class = $_
Write-Verbose $Class.FullName
try {
Write-Verbose "Importing PowerShell classes from $($Class.Name)..."
. $Class.FullName
}
catch {
Write-Error "Failed to import PowerShell class from $($Class.Name): $_"
}
}
# Import classes defined via C# (because PoSH classes don't support namespaces... yet?)
if ($Classes.cs) {
Write-Verbose "Importing C# classes..."
# This is probably a bad idea, but I don't know enough about the CLR to understand why this is the case.
$Script:ReferencedAssemblies = ([System.AppDomain]::CurrentDomain.GetAssemblies() | Sort-Object -unique -Property FullName | Where-Object Location | Where-Object Location -notmatch "\.(?:exe|winmd)$" | ForEach-Object Location | Select-Object -unique)
try {
Add-Type -Path ($Classes.cs | Foreach-Object Fullname) -Verbose -ReferencedAssemblies $Script:ReferencedAssemblies
}
catch {
Write-Error "Failed to import C# classes: $_"
}
}
# Export our functions
$Scripts.Public | ForEach-Object BaseName | Export-ModuleMember