<# .SYNOPSIS Porneste (sau opreste) inregistrarea curbei de descarcare, ca task programat sub SYSTEM - ca sa nu moara odata cu sesiunea SSH. .EXAMPLE powershell -ExecutionPolicy Bypass -File .\start-trace.ps1 powershell -ExecutionPolicy Bypass -File .\start-trace.ps1 -Stop #> [CmdletBinding()] param( [string]$InstallDir = 'C:\ROMFAST\ups-monitor', [ValidateRange(5, 300)][int]$IntervalSecunde = 20, [switch]$Stop ) Set-StrictMode -Version 2.0 $ErrorActionPreference = 'Stop' $TaskName = 'ROMFAST UPS Trace' $csv = Join-Path $InstallDir 'log\trace-baterie.csv' $stopFile = Join-Path $InstallDir 'log\STOP-TRACE' if ($Stop) { New-Item -ItemType File -Path $stopFile -Force | Out-Null Start-Sleep -Seconds ($IntervalSecunde + 5) if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) { Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false } Remove-Item -LiteralPath $stopFile -Force -ErrorAction SilentlyContinue Write-Host "Inregistrare oprita. Datele raman in $csv" if (Test-Path -LiteralPath $csv) { Write-Host '' Get-Content -LiteralPath $csv -Tail 10 | ForEach-Object { " $_" } } return } $script = Join-Path $InstallDir 'trace-baterie.ps1' if (-not (Test-Path -LiteralPath $script)) { throw "Lipseste $script - copiaza-l intai in $InstallDir" } $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ( '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File "{0}" -IntervalSecunde {1}' -f $script, $IntervalSecunde) # Aceleasi doua setari ca la monitor: fara ele, Windows opreste taskul exact cand # serverul trece pe baterie - adica exact cand incepe ce vrem sa masuram. $settings = New-ScheduledTaskSettingsSet ` -AllowStartIfOnBatteries ` -DontStopIfGoingOnBatteries ` -MultipleInstances IgnoreNew ` -ExecutionTimeLimit ([TimeSpan]::Zero) $principal = New-ScheduledTaskPrincipal -UserId 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount -RunLevel Highest if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) { Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false } Register-ScheduledTask -TaskName $TaskName -Description 'Inregistreaza curba de descarcare a UPS-ului in timpul unui test de autonomie.' ` -Action $action -Settings $settings -Principal $principal | Out-Null Start-ScheduledTask -TaskName $TaskName Start-Sleep -Seconds ($IntervalSecunde + 8) Get-ScheduledTask -TaskName $TaskName | Format-Table -AutoSize TaskName, State Write-Host "CSV: $csv" if (Test-Path -LiteralPath $csv) { Get-Content -LiteralPath $csv -Tail 5 | ForEach-Object { " $_" } }