diff --git a/CHANGELOG.md b/CHANGELOG.md index c6cd8ce..314c00a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Updated `Restore-OPNSenseConfig` to accept an XML document from the pipeline or as a parameter - Changed `Export-OPNSenseConfig` and `Import-OPNSenseConfig` to use System.IO.FileInfo for the Path parameter - Added unit tests for firmware management cmdlets +- Fixed threading issue in cmdlets that was causing "WriteObject and WriteError methods cannot be called from outside the overrides" error +- Changed module structure to use DLL as root module instead of nested module ### Removed - Removed `Backup-OPNSenseConfig` cmdlet (use `Get-OPNSenseConfig` instead) diff --git a/output/PSOPNSenseAPI-01/PSOPNSenseAPI.psd1 b/output/PSOPNSenseAPI-01/PSOPNSenseAPI.psd1 new file mode 100644 index 0000000..e729d66 --- /dev/null +++ b/output/PSOPNSenseAPI-01/PSOPNSenseAPI.psd1 @@ -0,0 +1,213 @@ +@{ + # Script module or binary module file associated with this manifest. + RootModule = 'PSOPNSenseAPI.psm1' + + # Version number of this module. + ModuleVersion = '2025.04.15.1143' + + # Supported PSEditions + CompatiblePSEditions = @('Desktop', 'Core') + + # ID used to uniquely identify this module + GUID = '9a3b4c55-5f9a-4b8c-87d9-9a7a5cdd5c9f' + + # 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 = '4.0' + + # 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 = @() + + # 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 = @('lib\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 = @( + 'Apply-OPNSenseFirewallChanges', + 'Connect-OPNSense', + 'Connect-OPNSenseTailscale', + 'ConvertTo-OPNSenseNetworkNotation', + 'Disable-OPNSenseCronJob', + 'Disable-OPNSenseFirewallRule', + 'Disable-OPNSensePlugin', + 'Disable-OPNSenseTailscale', + 'Disconnect-OPNSense', + 'Disconnect-OPNSenseTailscale', + 'Enable-OPNSenseCronJob', + 'Enable-OPNSenseFirewallRule', + 'Enable-OPNSensePlugin', + 'Enable-OPNSenseTailscale', + 'Export-OPNSenseConfig', + 'Get-OPNSenseAlias', + 'Get-OPNSenseConfig', + 'Get-OPNSenseConfigBackup', + 'Get-OPNSenseConnection', + 'Get-OPNSenseCronJob', + 'Get-OPNSenseDHCPLease', + 'Get-OPNSenseDHCPOption', + 'Get-OPNSenseDHCPServer', + 'Get-OPNSenseDHCPStaticMapping', + 'Get-OPNSenseDNSForwarding', + 'Get-OPNSenseDNSForwardingHost', + 'Get-OPNSenseDNSOverride', + 'Get-OPNSenseDNSServer', + 'Get-OPNSenseFirewallRule', + 'Get-OPNSenseFirmware', + 'Get-OPNSenseGateway', + 'Get-OPNSenseInterface', + 'Get-OPNSenseInterfaceStatistics', + 'Get-OPNSensePlugin', + 'Get-OPNSensePortForwardingRule', + 'Get-OPNSenseRoute', + 'Get-OPNSenseSystemDNS', + 'Get-OPNSenseTailscaleStatus', + 'Get-OPNSenseUser', + 'Get-OPNSenseVLAN', + 'Import-OPNSenseConfig', + 'Install-OPNSensePlugin', + 'Invoke-OPNSenseNetworkCalculation', + 'New-OPNSenseAlias', + 'New-OPNSenseCronJob', + 'New-OPNSenseDHCPOption', + 'New-OPNSenseDHCPStaticMapping', + 'New-OPNSenseDNSForwardingHost', + 'New-OPNSenseDNSOverride', + 'New-OPNSenseFirewallRule', + 'New-OPNSenseGateway', + 'New-OPNSensePortForwardingRule', + 'New-OPNSenseRoute', + 'New-OPNSenseSubnetVLANs', + 'New-OPNSenseUser', + 'New-OPNSenseVLAN', + 'Remove-OPNSenseAlias', + 'Remove-OPNSenseCronJob', + 'Remove-OPNSenseDHCPLease', + 'Remove-OPNSenseDHCPOption', + 'Remove-OPNSenseDHCPStaticMapping', + 'Remove-OPNSenseDNSForwardingHost', + 'Remove-OPNSenseDNSOverride', + 'Remove-OPNSenseFirewallRule', + 'Remove-OPNSenseGateway', + 'Remove-OPNSensePortForwardingRule', + 'Remove-OPNSenseRoute', + 'Remove-OPNSenseUser', + 'Remove-OPNSenseVLAN', + 'Restart-OPNSenseFirewall', + 'Restart-OPNSenseInterface', + 'Restore-OPNSenseConfig', + 'Set-OPNSenseAlias', + 'Set-OPNSenseCronJob', + 'Set-OPNSenseDHCPOption', + 'Set-OPNSenseDHCPServer', + 'Set-OPNSenseDHCPStaticMapping', + 'Set-OPNSenseDNSForwarding', + 'Set-OPNSenseDNSForwardingHost', + 'Set-OPNSenseDNSServer', + 'Set-OPNSenseFirewallRule', + 'Set-OPNSenseGateway', + 'Set-OPNSenseInterface', + 'Set-OPNSensePortForwardingRule', + 'Set-OPNSenseRoute', + 'Set-OPNSenseSystemDNS', + 'Set-OPNSenseUser', + 'Set-OPNSenseVLAN', + 'Uninstall-OPNSensePlugin', + 'Update-OPNSenseFirmware', + 'Start-OPNSenseFirmwareUpgrade' + ) + + # 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 = @('PowerShell', 'OPNSense', 'Firewall', 'API') + + # 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 = 'https://github.com/freedbygrace/PSOPNSenseAPI/blob/main/CHANGELOG.md' + + # 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 of this module + # HelpInfoURI = '' + + # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. + # DefaultCommandPrefix = '' +} diff --git a/output/PSOPNSenseAPI/PSOPNSenseAPI.psm1 b/output/PSOPNSenseAPI-01/PSOPNSenseAPI.psm1 similarity index 100% rename from output/PSOPNSenseAPI/PSOPNSenseAPI.psm1 rename to output/PSOPNSenseAPI-01/PSOPNSenseAPI.psm1 diff --git a/output/PSOPNSenseAPI-01/lib/Newtonsoft.Json.dll b/output/PSOPNSenseAPI-01/lib/Newtonsoft.Json.dll new file mode 100644 index 0000000..341d08f Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/Newtonsoft.Json.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/PSOPNSenseAPI.dll b/output/PSOPNSenseAPI-01/lib/PSOPNSenseAPI.dll new file mode 100644 index 0000000..d0ae65d Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/PSOPNSenseAPI.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Buffers.dll b/output/PSOPNSenseAPI-01/lib/System.Buffers.dll new file mode 100644 index 0000000..f2d83c5 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Buffers.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Management.Automation.dll b/output/PSOPNSenseAPI-01/lib/System.Management.Automation.dll new file mode 100644 index 0000000..3dfa7d8 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Management.Automation.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Memory.dll b/output/PSOPNSenseAPI-01/lib/System.Memory.dll new file mode 100644 index 0000000..4617199 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Memory.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Net.IPNetwork.dll b/output/PSOPNSenseAPI-01/lib/System.Net.IPNetwork.dll new file mode 100644 index 0000000..ce68dd5 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Net.IPNetwork.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Numerics.Vectors.dll b/output/PSOPNSenseAPI-01/lib/System.Numerics.Vectors.dll new file mode 100644 index 0000000..0865972 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Numerics.Vectors.dll differ diff --git a/output/PSOPNSenseAPI-01/lib/System.Runtime.CompilerServices.Unsafe.dll b/output/PSOPNSenseAPI-01/lib/System.Runtime.CompilerServices.Unsafe.dll new file mode 100644 index 0000000..de9e124 Binary files /dev/null and b/output/PSOPNSenseAPI-01/lib/System.Runtime.CompilerServices.Unsafe.dll differ diff --git a/output/PSOPNSenseAPI/PSOPNSenseAPI.psd1 b/output/PSOPNSenseAPI/PSOPNSenseAPI.psd1 index e729d66..02c51dd 100644 --- a/output/PSOPNSenseAPI/PSOPNSenseAPI.psd1 +++ b/output/PSOPNSenseAPI/PSOPNSenseAPI.psd1 @@ -1,6 +1,6 @@ @{ # Script module or binary module file associated with this manifest. - RootModule = 'PSOPNSenseAPI.psm1' + RootModule = 'lib\PSOPNSenseAPI.dll' # Version number of this module. ModuleVersion = '2025.04.15.1143' @@ -57,7 +57,7 @@ # FormatsToProcess = @() # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess - NestedModules = @('lib\PSOPNSenseAPI.dll') + # NestedModules = @() # 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 = @() diff --git a/output/PSOPNSenseAPI/lib/PSOPNSenseAPI.dll b/output/PSOPNSenseAPI/lib/PSOPNSenseAPI.dll index d0ae65d..6af96a0 100644 Binary files a/output/PSOPNSenseAPI/lib/PSOPNSenseAPI.dll and b/output/PSOPNSenseAPI/lib/PSOPNSenseAPI.dll differ diff --git a/src/PSOPNSenseAPI/Cmdlets/ExportOPNSenseConfigCmdlet.cs b/src/PSOPNSenseAPI/Cmdlets/ExportOPNSenseConfigCmdlet.cs index 5213205..2f529c8 100644 --- a/src/PSOPNSenseAPI/Cmdlets/ExportOPNSenseConfigCmdlet.cs +++ b/src/PSOPNSenseAPI/Cmdlets/ExportOPNSenseConfigCmdlet.cs @@ -54,8 +54,8 @@ namespace PSOPNSenseAPI.Cmdlets var configService = new ConfigService(ApiClient, Logger); - var task = Task.Run(async () => await configService.ExportConfigAsync()); - var configContent = task.GetAwaiter().GetResult(); + // Execute the async method synchronously on the main thread + var configContent = configService.ExportConfigAsync().GetAwaiter().GetResult(); // Create the directory if it doesn't exist var directory = System.IO.Path.GetDirectoryName(fullPath); diff --git a/src/PSOPNSenseAPI/Cmdlets/GetOPNSenseConfigCmdlet.cs b/src/PSOPNSenseAPI/Cmdlets/GetOPNSenseConfigCmdlet.cs index 9b62fd2..98b8b3d 100644 --- a/src/PSOPNSenseAPI/Cmdlets/GetOPNSenseConfigCmdlet.cs +++ b/src/PSOPNSenseAPI/Cmdlets/GetOPNSenseConfigCmdlet.cs @@ -34,8 +34,8 @@ namespace PSOPNSenseAPI.Cmdlets { var configService = new ConfigService(ApiClient, Logger); - var task = Task.Run(async () => await configService.ExportConfigAsync()); - var configContent = task.GetAwaiter().GetResult(); + // Execute the async method synchronously on the main thread + var configContent = configService.ExportConfigAsync().GetAwaiter().GetResult(); // Convert the byte array to an XML document var xmlDoc = new XmlDocument(); diff --git a/src/PSOPNSenseAPI/Cmdlets/ImportOPNSenseConfigCmdlet.cs b/src/PSOPNSenseAPI/Cmdlets/ImportOPNSenseConfigCmdlet.cs index 00912e6..00667a2 100644 --- a/src/PSOPNSenseAPI/Cmdlets/ImportOPNSenseConfigCmdlet.cs +++ b/src/PSOPNSenseAPI/Cmdlets/ImportOPNSenseConfigCmdlet.cs @@ -62,8 +62,8 @@ namespace PSOPNSenseAPI.Cmdlets // Read the configuration file var configContent = File.ReadAllBytes(fullPath); - var task = Task.Run(async () => await configService.ImportConfigAsync(configContent)); - var result = task.GetAwaiter().GetResult(); + // Execute the async method synchronously on the main thread + var result = configService.ImportConfigAsync(configContent).GetAwaiter().GetResult(); WriteVerbose($"Imported configuration from {fullPath}: {result.Status}"); WriteWarning("The firewall is restarting. You may need to reconnect after it comes back online."); diff --git a/src/PSOPNSenseAPI/Cmdlets/RestoreOPNSenseConfigCmdlet.cs b/src/PSOPNSenseAPI/Cmdlets/RestoreOPNSenseConfigCmdlet.cs index 64322b0..eff9bc8 100644 --- a/src/PSOPNSenseAPI/Cmdlets/RestoreOPNSenseConfigCmdlet.cs +++ b/src/PSOPNSenseAPI/Cmdlets/RestoreOPNSenseConfigCmdlet.cs @@ -68,10 +68,8 @@ namespace PSOPNSenseAPI.Cmdlets if (ParameterSetName == "Filename") { - // Restore from backup file - var task = Task.Run(async () => await configService.RestoreConfigBackupAsync(Filename)); - var result = task.GetAwaiter().GetResult(); - + // Restore from backup file - execute synchronously on the main thread + var result = configService.RestoreConfigBackupAsync(Filename).GetAwaiter().GetResult(); WriteVerbose($"Restored configuration from backup: {result.Status}"); } else @@ -84,9 +82,8 @@ namespace PSOPNSenseAPI.Cmdlets configContent = memoryStream.ToArray(); } - var task = Task.Run(async () => await configService.ImportConfigAsync(configContent)); - var result = task.GetAwaiter().GetResult(); - + // Execute synchronously on the main thread + var result = configService.ImportConfigAsync(configContent).GetAwaiter().GetResult(); WriteVerbose($"Restored configuration from XML document: {result.Status}"); } diff --git a/src/PSOPNSenseAPI/PSOPNSenseAPI.psd1 b/src/PSOPNSenseAPI/PSOPNSenseAPI.psd1 index e729d66..02c51dd 100644 --- a/src/PSOPNSenseAPI/PSOPNSenseAPI.psd1 +++ b/src/PSOPNSenseAPI/PSOPNSenseAPI.psd1 @@ -1,6 +1,6 @@ @{ # Script module or binary module file associated with this manifest. - RootModule = 'PSOPNSenseAPI.psm1' + RootModule = 'lib\PSOPNSenseAPI.dll' # Version number of this module. ModuleVersion = '2025.04.15.1143' @@ -57,7 +57,7 @@ # FormatsToProcess = @() # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess - NestedModules = @('lib\PSOPNSenseAPI.dll') + # NestedModules = @() # 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 = @() diff --git a/src/PSOPNSenseAPI/PSOPNSenseAPI.psm1 b/src/PSOPNSenseAPI/PSOPNSenseAPI.psm1 deleted file mode 100644 index d4b84da..0000000 --- a/src/PSOPNSenseAPI/PSOPNSenseAPI.psm1 +++ /dev/null @@ -1,13 +0,0 @@ -# Load the main assembly directly -$mainDllPath = Join-Path $PSScriptRoot "lib\PSOPNSenseAPI.dll" - -Write-Verbose "Loading assembly from $mainDllPath" - -try { - Add-Type -Path $mainDllPath - Write-Verbose "Successfully loaded assembly from $mainDllPath" -} catch { - $errorMessage = $_.Exception.Message - Write-Error "Failed to load assembly from $($mainDllPath): $($errorMessage)" - throw -}