update install-agent.ps1 to support installing as admin (#797)

This commit is contained in:
henrygd
2025-05-05 20:21:37 -04:00
parent ffb3ec0477
commit df334caca6

View File

@@ -323,12 +323,21 @@ function Start-BeszelAgentService {
#region Main Script Execution #region Main Script Execution
# Non-admin tasks - Only run if we're not in elevated mode # Check if we're running as admin
if (-not $Elevated) { $isAdmin = Test-Admin
try {
# Determine installation method
$AgentPath = ""
try {
# First: Install the agent (doesn't require admin)
if (-not $AgentPath) {
# Check for problematic case: running as admin and need Scoop
if ($isAdmin -and -not (Test-CommandExists "scoop") -and -not (Test-CommandExists "winget")) {
Write-Host "ERROR: You're running as administrator but need to install Scoop." -ForegroundColor Red
Write-Host "Scoop should be installed without admin privileges." -ForegroundColor Red
Write-Host "Please run this script again without administrator privileges." -ForegroundColor Red
exit 1
}
Write-Host "Installing beszel-agent..."
if (Test-CommandExists "scoop") { if (Test-CommandExists "scoop") {
Write-Host "Using Scoop for installation..." Write-Host "Using Scoop for installation..."
$AgentPath = Install-WithScoop -Key $Key -Port $Port $AgentPath = Install-WithScoop -Key $Key -Port $Port
@@ -341,10 +350,15 @@ if (-not $Elevated) {
Write-Host "Neither Scoop nor WinGet is installed. Installing Scoop..." Write-Host "Neither Scoop nor WinGet is installed. Installing Scoop..."
$AgentPath = Install-WithScoop -Key $Key -Port $Port $AgentPath = Install-WithScoop -Key $Key -Port $Port
} }
}
# Check if we need admin privileges for the NSSM part if (-not $AgentPath) {
if (-not (Test-Admin)) { throw "Could not find beszel-agent executable. Make sure it was properly installed."
Write-Host "Admin privileges required for NSSM. Relaunching as admin..." -ForegroundColor Yellow }
# Second: If we need admin rights for service installation and we don't have them, relaunch
if (-not $isAdmin -and -not $Elevated) {
Write-Host "Admin privileges required for service installation. Relaunching as admin..." -ForegroundColor Yellow
Write-Host "Check service status with 'nssm status beszel-agent'" Write-Host "Check service status with 'nssm status beszel-agent'"
Write-Host "Edit service configuration with 'nssm edit beszel-agent'" Write-Host "Edit service configuration with 'nssm edit beszel-agent'"
@@ -352,23 +366,9 @@ if (-not $Elevated) {
Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$PSCommandPath`" -Elevated -Key `"$Key`" -Port $Port -AgentPath `"$AgentPath`"" Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$PSCommandPath`" -Elevated -Key `"$Key`" -Port $Port -AgentPath `"$AgentPath`""
exit exit
} }
}
catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Installation failed. Please check the error message above." -ForegroundColor Red
Write-Host "Press any key to exit..." -ForegroundColor Red
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
# Admin tasks - service installation and firewall rules
if ($Elevated) {
try {
if (-not $AgentPath) {
throw "Could not find beszel-agent executable. Make sure it was properly installed."
}
# Third: If we have admin rights, install service and configure firewall
if ($isAdmin -or $Elevated) {
# Install the service # Install the service
Install-NSSMService -AgentPath $AgentPath -Key $Key -Port $Port Install-NSSMService -AgentPath $AgentPath -Key $Key -Port $Port
@@ -377,15 +377,24 @@ if ($Elevated) {
# Start the service # Start the service
Start-BeszelAgentService Start-BeszelAgentService
# Pause to see results if this is an elevated window
if ($Elevated) {
Write-Host "Press any key to exit..." -ForegroundColor Cyan
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
} }
catch { catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Installation failed. Please check the error message above." -ForegroundColor Red Write-Host "Installation failed. Please check the error message above." -ForegroundColor Red
}
# Pause to see results before exit if this is an elevated window # Pause if this is likely a new window
Write-Host "Press any key to exit..." -ForegroundColor Cyan if ($Elevated -or (-not $isAdmin)) {
Write-Host "Press any key to exit..." -ForegroundColor Red
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} }
exit 1
}
#endregion #endregion