fix: use native scheduled task cmdlets and improve error diagnostics
This commit is contained in:
+8
-2
@@ -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')
|
$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')
|
$system = New-Object System.Security.AccessControl.FileSystemAccessRule($systemSid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
|
||||||
$acl.SetAccessRule($admins); $acl.SetAccessRule($system); Set-Acl $InstallDirectory $acl
|
$acl.SetAccessRule($admins); $acl.SetAccessRule($system); Set-Acl $InstallDirectory $acl
|
||||||
$command = '"' + (Join-Path $InstallDirectory 'agent.exe') + '" --config "' + (Join-Path $InstallDirectory 'config.json') + '"'
|
$agentPath = Join-Path $InstallDirectory 'agent.exe'
|
||||||
schtasks.exe /Create /TN 'NetBox Windows Agent' /SC $Schedule /ST $StartTime /RU SYSTEM /RL HIGHEST /TR $command /F | Out-Null
|
$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')
|
& (Join-Path $InstallDirectory 'agent.exe') --config (Join-Path $InstallDirectory 'config.json')
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
param([string]$InstallDirectory = "$env:ProgramFiles\NetBox Windows Agent")
|
param([string]$InstallDirectory = "$env:ProgramFiles\NetBox Windows Agent")
|
||||||
$ErrorActionPreference = 'Stop'
|
$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) {
|
if (Test-Path -LiteralPath $InstallDirectory) {
|
||||||
$resolved = (Resolve-Path -LiteralPath $InstallDirectory).Path
|
$resolved = (Resolve-Path -LiteralPath $InstallDirectory).Path
|
||||||
$programFiles = (Resolve-Path -LiteralPath $env:ProgramFiles).Path
|
$programFiles = (Resolve-Path -LiteralPath $env:ProgramFiles).Path
|
||||||
if (-not $resolved.StartsWith($programFiles + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Unsicheres Ziel: $resolved" }
|
if (-not $resolved.StartsWith($programFiles + '\', [StringComparison]::OrdinalIgnoreCase)) { throw "Unsicheres Ziel: $resolved" }
|
||||||
Remove-Item -LiteralPath $resolved -Recurse -Force
|
Remove-Item -LiteralPath $resolved -Recurse -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -21,7 +21,14 @@ static async Task<int> MainAsync(string[] args)
|
|||||||
Console.WriteLine(dryRun ? "Dry-Run erfolgreich; keine Änderungen geschrieben." : $"Synchronisiert: Device #{result.DeviceId}, Asset #{result.AssetId}");
|
Console.WriteLine(dryRun ? "Dry-Run erfolgreich; keine Änderungen geschrieben." : $"Synchronisiert: Device #{result.DeviceId}, Asset #{result.AssetId}");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
catch (Exception ex) { Console.Error.WriteLine($"FEHLER: {ex.Message}"); return 1; }
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var messages = new List<string>();
|
||||||
|
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 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)
|
static void Validate(AgentConfig c)
|
||||||
|
|||||||
Reference in New Issue
Block a user