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
-208
View File
@@ -1,208 +0,0 @@
@{
# Script module or binary module file associated with this manifest.
RootModule = 'PSOPNSenseAPI.psm1'
# Version number of this module.
ModuleVersion = '2025.04.14.1839'
# Supported PSEditions
CompatiblePSEditions = @('Desktop', 'Core')
# ID used to uniquely identify this module
GUID = '8f7a3d7a-0e5a-4b7c-9b5a-9c5b3a5a8e7a'
# Author of this module
Author = 'PSOPNSenseAPI Contributors'
# Company or vendor of this module
CompanyName = 'PSOPNSenseAPI'
# Copyright statement for this module
Copyright = '(c) 2025 PSOPNSenseAPI Contributors. All rights reserved.'
# Description of the functionality provided by this module
Description = 'PowerShell module for interacting with the OPNSense API to configure firewalls'
# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '5.1'
# Name of the PowerShell host required by this module
# PowerShellHostName = ''
# Minimum version of the PowerShell host required by this module
# PowerShellHostVersion = ''
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
DotNetFrameworkVersion = '4.7.2'
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
# ClrVersion = ''
# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''
# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
RequiredAssemblies = @('bin\PSOPNSenseAPI.dll')
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()
# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('bin\PSOPNSenseAPI.dll')
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @(
# Connection Management
'Connect-OPNSense',
'Disconnect-OPNSense',
'Get-OPNSenseConnection',
# Firewall Rule Management
'Get-OPNSenseFirewallRule',
'New-OPNSenseFirewallRule',
'Set-OPNSenseFirewallRule',
'Remove-OPNSenseFirewallRule',
'Enable-OPNSenseFirewallRule',
'Disable-OPNSenseFirewallRule',
'Apply-OPNSenseFirewallChanges',
# Alias Management
'Get-OPNSenseAlias',
'New-OPNSenseAlias',
'Set-OPNSenseAlias',
'Remove-OPNSenseAlias',
# NAT Rule Management
'Get-OPNSenseNATRule',
'New-OPNSenseNATRule',
'Set-OPNSenseNATRule',
'Remove-OPNSenseNATRule',
# Interface Management
'Get-OPNSenseInterface',
'Set-OPNSenseInterface',
'Restart-OPNSenseInterface',
'Get-OPNSenseInterfaceStatistics',
'New-OPNSenseSubnetVLANs',
# VLAN Management
'Get-OPNSenseVLAN',
'New-OPNSenseVLAN',
'Set-OPNSenseVLAN',
'Remove-OPNSenseVLAN',
# DNS Configuration
'Get-OPNSenseDNSServer',
'Set-OPNSenseDNSServer',
'Get-OPNSenseDNSDomain',
'Set-OPNSenseDNSDomain',
'Get-OPNSenseDNSOverride',
'New-OPNSenseDNSOverride',
'Remove-OPNSenseDNSOverride',
# Configuration Management
'Backup-OPNSenseConfig',
'Restore-OPNSenseConfig',
'Export-OPNSenseConfig',
'Import-OPNSenseConfig',
'Get-OPNSenseConfigBackup',
# Plugin Management
'Get-OPNSensePlugin',
'Install-OPNSensePlugin',
'Uninstall-OPNSensePlugin',
'Enable-OPNSensePlugin',
'Disable-OPNSensePlugin',
# User Management
'Get-OPNSenseUser',
'New-OPNSenseUser',
'Set-OPNSenseUser',
'Remove-OPNSenseUser',
# Firmware Management
'Get-OPNSenseFirmware',
'Update-OPNSenseFirmware',
'Upgrade-OPNSenseFirmware',
# System Management
'Restart-OPNSenseFirewall'
)
# Variables to export from this module
VariablesToExport = '*'
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
# DSC resources to export from this module
# DscResourcesToExport = @()
# List of all modules packaged with this module
# ModuleList = @()
# List of all files packaged with this module
# FileList = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('OPNSense', 'Firewall', 'API', 'Network', 'Security')
# A URL to the license for this module.
LicenseUri = 'https://github.com/freedbygrace/PSOPNSenseAPI/blob/main/LICENSE'
# A URL to the main website for this project.
ProjectUri = 'https://github.com/freedbygrace/PSOPNSenseAPI'
# A URL to an icon representing this module.
# IconUri = ''
# ReleaseNotes of this module
ReleaseNotes = 'Initial release of the PSOPNSenseAPI module'
# Prerelease string of this module
# Prerelease = ''
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
# External dependent modules of this module
# ExternalModuleDependencies = @()
} # End of PSData hashtable
} # End of PrivateData hashtable
# HelpInfoURI = ''
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
}
-3
View File
@@ -1,3 +0,0 @@
# This file is intentionally left empty.
# The module functionality is provided by the binary module (DLL).
# This file exists to allow for future PowerShell script functions if needed.
+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.
+27 -2
View File
@@ -31,13 +31,38 @@ Copy-Item -Path "src\PSOPNSenseAPI\bin\Release\net472\*.dll" -Destination $libPa
Remove-Item -Path "output\PSOPNSenseAPI\lib" -Recurse -Force -ErrorAction SilentlyContinue
Rename-Item -Path $libPath -NewName "lib"
# Create the PSM1 file
$psm1Path = "output\PSOPNSenseAPI\PSOPNSenseAPI.psm1"
$psm1Content = @"
# 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"
foreach (`$dll in `$dllFiles) {
try {
Write-Verbose "Attempting to load assembly: `$(`$dll.FullName)"
`$dllBytes = [System.IO.File]::ReadAllBytes(`$dll.FullName)
`$Null = [System.Reflection.Assembly]::Load(`$dllBytes)
Write-Verbose "Successfully loaded assembly: `$(`$dll.FullName)"
}
catch {
Write-Warning "Failed to load assembly `$(`$dll.Name): `$_"
}
}
# Export all cmdlets from the PSD1 file
"@
Set-Content -Path $psm1Path -Value $psm1Content
# Create a release archive
$zipFile = "output\PSOPNSenseAPI-$version.zip"
Compress-Archive -Path "output\PSOPNSenseAPI\*" -DestinationPath $zipFile -Force
# Clean up old zip files
Get-ChildItem -Path "output" -Filter "*.zip" |
Where-Object { $_.Name -ne "PSOPNSenseAPI-$version.zip" } |
Get-ChildItem -Path "output" -Filter "*.zip" |
Where-Object { $_.Name -ne "PSOPNSenseAPI-$version.zip" } |
Remove-Item -Force
# Commit the changes
+36
View File
@@ -0,0 +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 -ChildPath "lib"
$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++
}
}