63 lines
2.9 KiB
PowerShell
63 lines
2.9 KiB
PowerShell
#Requires -RunAsAdministrator
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$Target = "C:\MinerOpsAgent"
|
|
$BackupRoot = Join-Path $Target "backups"
|
|
$Backup = Join-Path $BackupRoot (Get-Date -Format "yyyyMMdd-HHmmss")
|
|
$RuntimeFiles = @(
|
|
"MinerOpsAgentService.ps1", "MinerOpsAgentTray.ps1", "MinerOpsAgentGUI.ps1",
|
|
"MinerOpsApi3EnrichmentPoller.ps1", "MinerOpsPoolPoller.ps1",
|
|
"MinerOpsPoolPollerTask.ps1", "EnrichOneMiner.ps1", "MinerOpsControl.ps1",
|
|
"VERSION"
|
|
)
|
|
|
|
New-Item -ItemType Directory -Force -Path $Target, $BackupRoot | Out-Null
|
|
$existing = @($RuntimeFiles | Where-Object { Test-Path (Join-Path $Target $_) })
|
|
if ($existing.Count -gt 0) {
|
|
New-Item -ItemType Directory -Force -Path $Backup | Out-Null
|
|
foreach ($name in $existing) {
|
|
Copy-Item (Join-Path $Target $name) (Join-Path $Backup $name) -Force
|
|
}
|
|
}
|
|
|
|
try {
|
|
foreach ($name in $RuntimeFiles) {
|
|
$source = Join-Path $PSScriptRoot $name
|
|
if (!(Test-Path $source)) { throw "Deployment source is missing $name" }
|
|
Copy-Item $source (Join-Path $Target $name) -Force
|
|
}
|
|
|
|
$powerShell = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
|
|
$system = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
|
$startup = New-ScheduledTaskTrigger -AtStartup
|
|
$fiveMinutes = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
|
|
-RepetitionInterval (New-TimeSpan -Minutes 5)
|
|
|
|
$definitions = @(
|
|
@{ Name = "MinerOpsAgent"; Script = "MinerOpsAgentService.ps1"; Trigger = $startup; Principal = $system },
|
|
@{ Name = "MinerOpsAgentTray"; Script = "MinerOpsAgentTray.ps1"; Trigger = (New-ScheduledTaskTrigger -AtLogOn); Principal = $null },
|
|
@{ Name = "MinerOpsApi3Enrichment"; Script = "MinerOpsApi3EnrichmentPoller.ps1"; Trigger = $fiveMinutes; Principal = $system },
|
|
@{ Name = "MinerOpsPoolPoller"; Script = "MinerOpsPoolPollerTask.ps1"; Trigger = $fiveMinutes; Principal = $system }
|
|
)
|
|
foreach ($definition in $definitions) {
|
|
$arguments = '-NoProfile -ExecutionPolicy Bypass -File "{0}"' -f `
|
|
(Join-Path $Target $definition.Script)
|
|
$action = New-ScheduledTaskAction -Execute $powerShell -Argument $arguments
|
|
if ($definition.Principal) {
|
|
Register-ScheduledTask -TaskName $definition.Name -Action $action `
|
|
-Trigger $definition.Trigger -Principal $definition.Principal -Force | Out-Null
|
|
} else {
|
|
Register-ScheduledTask -TaskName $definition.Name -Action $action `
|
|
-Trigger $definition.Trigger -Force | Out-Null
|
|
}
|
|
}
|
|
Write-Host "MinerOps Windows Agent installed. Existing config, state, and logs were preserved."
|
|
} catch {
|
|
if (Test-Path $Backup) {
|
|
foreach ($name in $existing) {
|
|
Copy-Item (Join-Path $Backup $name) (Join-Path $Target $name) -Force
|
|
}
|
|
}
|
|
throw
|
|
}
|