Fix threading issue in 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
- Updated configuration cmdlets to properly handle async operations
This commit is contained in:
GraceSolutions
2025-04-15 09:24:51 -04:00
parent 68e6819131
commit 7535135bfd
19 changed files with 229 additions and 30 deletions
@@ -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);
@@ -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();
@@ -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.");
@@ -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}");
}
+2 -2
View File
@@ -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 = @()
-13
View File
@@ -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
}