From 8c3f297b2ca0c42a3aff19d5f35b27302a2d8da6 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 18 Nov 2026 12:17:24 +0100 Subject: [PATCH] fix: check for administrator rights first and retry locked copies in install.ps1 [skip ci] install.ps1 now stops right away with a clear message when it does not run as administrator or SYSTEM, instead of failing with "access denied" while copying. Copying retries while agent.exe is briefly locked, e.g. by an antivirus scan. Co-Authored-By: Claude Opus 5.5 --- NetBoxAgent.csproj | 2 +- README.md | 2 +- scripts/install.ps1 | 24 ++++++++++++++++++++---- src/Models.cs | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/NetBoxAgent.csproj b/NetBoxAgent.csproj index 649bf63..40f8873 100644 --- a/NetBoxAgent.csproj +++ b/NetBoxAgent.csproj @@ -6,7 +6,7 @@ enable agent NetBoxWindowsAgent - 0.2.0 + 0.2.1 diff --git a/README.md b/README.md index 46be72a..ffd83ab 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ Path: C:\Program Files\NetBox Windows Agent File or folder: agent.exe Detection method: String (version) Operator: Greater than or equal to -Value: 0.2.0 (the version of agent.exe, see agent.exe --version) +Value: 0.2.1 (the version of agent.exe, see agent.exe --version) Associated with a 32-bit app on 64-bit clients: No ``` diff --git a/scripts/install.ps1 b/scripts/install.ps1 index 7ca59af..24dc6fc 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -22,10 +22,26 @@ $syncTask = 'NetBox Windows Agent' $listenerTask = 'NetBox Windows Agent Listener' $firewallRule = 'NetBoxWindowsAgent' +$principalNow = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) +if (-not $principalNow.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + throw 'install.ps1 must run as administrator or SYSTEM (PowerShell: "Run as administrator"; Intune: install behavior "System").' +} + $sourceAgent = Join-Path $SourceDirectory 'agent.exe' $sourceConfig = Join-Path $SourceDirectory 'config.json' -if (-not (Test-Path -LiteralPath $sourceAgent)) { throw 'agent.exe is missing in the source directory.' } -if (-not (Test-Path -LiteralPath $sourceConfig)) { throw 'config.json is missing in the source directory.' } +if (-not (Test-Path -LiteralPath $sourceAgent)) { throw "agent.exe is missing in the source directory $SourceDirectory." } +if (-not (Test-Path -LiteralPath $sourceConfig)) { throw "config.json is missing in the source directory $SourceDirectory." } + +# Copies a file, retrying while it is briefly locked (e.g. an antivirus scan of the new agent.exe). +function Copy-WithRetry([string]$Source, [string]$Destination) { + for ($attempt = 1; ; $attempt++) { + try { Copy-Item -LiteralPath $Source -Destination $Destination -Force; return } + catch [System.UnauthorizedAccessException], [System.IO.IOException] { + if ($attempt -ge 10) { throw "Cannot write $Destination ($($_.Exception.Message)). Is agent.exe still running or blocked by an antivirus?" } + Start-Sleep -Seconds 2 + } + } +} if ($Schedule -ne 'DAILY') { throw "Only the schedule 'DAILY' is supported." } $config = Get-Content -LiteralPath $sourceConfig -Raw | ConvertFrom-Json @@ -44,8 +60,8 @@ Get-Process -Name agent -ErrorAction SilentlyContinue | Start-Sleep -Milliseconds 500 New-Item -ItemType Directory -Path $InstallDirectory -Force | Out-Null -Copy-Item -LiteralPath $sourceAgent -Destination (Join-Path $InstallDirectory 'agent.exe') -Force -Copy-Item -LiteralPath $sourceConfig -Destination (Join-Path $InstallDirectory 'config.json') -Force +Copy-WithRetry $sourceAgent (Join-Path $InstallDirectory 'agent.exe') +Copy-WithRetry $sourceConfig (Join-Path $InstallDirectory 'config.json') $acl = Get-Acl $InstallDirectory $acl.SetAccessRuleProtection($true, $false) $administratorsSid = New-Object System.Security.Principal.SecurityIdentifier('S-1-5-32-544') diff --git a/src/Models.cs b/src/Models.cs index 55989b2..2b49eab 100644 --- a/src/Models.cs +++ b/src/Models.cs @@ -4,7 +4,7 @@ namespace NetBoxWindowsAgent; internal static class AgentInfo { - public const string Version = "0.2.0"; + public const string Version = "0.2.1"; public const int Protocol = 1; }