Update PSM1 file to properly load assemblies and let PowerShell handle cmdlet export

This commit is contained in:
GraceSolutions
2025-04-15 00:44:55 -04:00
parent 2916619bf3
commit 412f46ec8d
14 changed files with 95 additions and 234 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
Description = 'PowerShell module for interacting with the OPNSense API to configure firewalls'
PowerShellVersion = '5.1'
CompatiblePSEditions = @('Desktop', 'Core')
DotNetFrameworkVersion = '4.7.2'
#DotNetFrameworkVersion = '4.7.2'
CLRVersion = '4.0.0'
FunctionsToExport = @()
CmdletsToExport = @(
+31 -20
View File
@@ -1,25 +1,36 @@
# Load assemblies from the lib folder using System.Reflection.Assembly::LoadBytes
# This avoids file lock issues when the module is loaded
$libPath = Join-Path $PSScriptRoot "lib"
$dllFiles = Get-ChildItem -Path $libPath -Filter "*.dll"
$libPath = Join-Path $PSScriptRoot -ChildPath "lib"
foreach ($dll in $dllFiles) {
try {
$dllBytes = [System.IO.File]::ReadAllBytes($dll.FullName)
$Null = [System.Reflection.Assembly]::Load($dllBytes)
Write-Verbose "Attempting to load assembly: $($dll.FullName)"
}
catch {
Write-Warning "Failed to load assembly $($dll.Name): $_"
}
$DLLFileList = Get-ChildItem -Path $libPath -Filter "*.dll"
$DLLFileListCount = ($DLLFileList | Measure-Object).Count
$DLLFileListCounter = 1
For ($DLLFileListIndex = 0; $DLLFileListIndex -lt $DLLFileListCount; $DLLFileListIndex++)
{
Try
{
$DLLFile = $DLLFileList[$DLLFileListIndex]
Write-Verbose "Attempting to load DLL assembly $($DLLFileListCounter) of $($DLLFileListCount). Please Wait..."
Write-Verbose "Path: $($DLLFile)"
$DLLFileBytes = [System.IO.File]::ReadAllBytes($DLLFile.FullName)
$Null = [System.Reflection.Assembly]::Load($DLLFileBytes)
Write-Verbose "Successfully loaded DLL assembly $($DLLFileListCounter) of $($DLLFileListCount)"
}
Catch
{
Write-Warning "Failed to load DLL assembly $($DLLFileListCounter) of $($DLLFileListCount): $($_.Exception.Message)"
}
Finally
{
$DLLFileListCounter++
}
}
# Export all cmdlets
$cmdlets = [System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.FullName -like "*PSOPNSenseAPI*" } |
ForEach-Object { $_.GetTypes() } |
Where-Object { $_.IsPublic -and $_.IsClass -and $_.Name -like "*Cmdlet" } |
ForEach-Object { $_.Name }
Export-ModuleMember -Cmdlet $cmdlets
Binary file not shown.
Binary file not shown.