From 61aa9ad61b8d86e0747fd5c00606056667dfce8b Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jul 2026 13:43:25 +0200 Subject: [PATCH] fix: use native scheduled task cmdlets and improve error diagnostics --- scripts/install.ps1 | 10 ++++++++-- scripts/uninstall.ps1 | 3 +-- src/Program.cs | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/scripts/install.ps1 b/scripts/install.ps1 index c25fbee..e8868d9 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -17,6 +17,12 @@ $systemSid = New-Object System.Security.Principal.SecurityIdentifier('S-1-5-18') $admins = New-Object System.Security.AccessControl.FileSystemAccessRule($administratorsSid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow') $system = New-Object System.Security.AccessControl.FileSystemAccessRule($systemSid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow') $acl.SetAccessRule($admins); $acl.SetAccessRule($system); Set-Acl $InstallDirectory $acl -$command = '"' + (Join-Path $InstallDirectory 'agent.exe') + '" --config "' + (Join-Path $InstallDirectory 'config.json') + '"' -schtasks.exe /Create /TN 'NetBox Windows Agent' /SC $Schedule /ST $StartTime /RU SYSTEM /RL HIGHEST /TR $command /F | Out-Null +$agentPath = Join-Path $InstallDirectory 'agent.exe' +$configPath = Join-Path $InstallDirectory 'config.json' +if ($Schedule -ne 'DAILY') { throw "Derzeit wird nur Schedule 'DAILY' unterstützt." } +$action = New-ScheduledTaskAction -Execute $agentPath -Argument "--config `"$configPath`"" +$trigger = New-ScheduledTaskTrigger -Daily -At $StartTime +$principal = New-ScheduledTaskPrincipal -UserId 'S-1-5-18' -LogonType ServiceAccount -RunLevel Highest +$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 15) +Register-ScheduledTask -TaskName 'NetBox Windows Agent' -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null & (Join-Path $InstallDirectory 'agent.exe') --config (Join-Path $InstallDirectory 'config.json') diff --git a/scripts/uninstall.ps1 b/scripts/uninstall.ps1 index b46f79a..ecb40e5 100644 --- a/scripts/uninstall.ps1 +++ b/scripts/uninstall.ps1 @@ -1,10 +1,9 @@ param([string]$InstallDirectory = "$env:ProgramFiles\NetBox Windows Agent") $ErrorActionPreference = 'Stop' -schtasks.exe /Delete /TN 'NetBox Windows Agent' /F 2>$null +Unregister-ScheduledTask -TaskName 'NetBox Windows Agent' -Confirm:$false -ErrorAction SilentlyContinue if (Test-Path -LiteralPath $InstallDirectory) { $resolved = (Resolve-Path -LiteralPath $InstallDirectory).Path $programFiles = (Resolve-Path -LiteralPath $env:ProgramFiles).Path if (-not $resolved.StartsWith($programFiles + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Unsicheres Ziel: $resolved" } Remove-Item -LiteralPath $resolved -Recurse -Force } - diff --git a/src/Program.cs b/src/Program.cs index e726f87..f351ec0 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -21,7 +21,14 @@ static async Task MainAsync(string[] args) Console.WriteLine(dryRun ? "Dry-Run erfolgreich; keine Änderungen geschrieben." : $"Synchronisiert: Device #{result.DeviceId}, Asset #{result.AssetId}"); return 0; } - catch (Exception ex) { Console.Error.WriteLine($"FEHLER: {ex.Message}"); return 1; } + catch (Exception ex) + { + var messages = new List(); + for (Exception? current = ex; current is not null; current = current.InnerException) + if (!messages.Contains(current.Message)) messages.Add(current.Message); + Console.Error.WriteLine($"FEHLER: {string.Join(" -> ", messages)}"); + return 1; + } } static string? ValueAfter(string[] args, string key) { var i = Array.IndexOf(args, key); return i >= 0 && i + 1 < args.Length ? args[i + 1] : null; } static void Validate(AgentConfig c)