<# Installa o aggiorna il task schedulato che lancia il bridge Panasonic live. Modalita disponibili: - system-startup: parte all'avvio macchina come SYSTEM, richiede admin e path locali affidabili - current-user-logon: parte al logon dell'utente corrente, adatta quando non hai privilegi admin Importante: - i drive mappati tipo S: non sono affidabili per i task schedulati - per produzione usa un path locale tipo C:\NetGescon o C:\ProgramData\NetGescon #> param( [string]$TaskName = 'NetGescon Panasonic Live Bridge', [string]$BaseUrl = 'https://staging.netgescon.it', [string]$Extensions = '201-208,601,603,0001,0003', [string]$LogFile = 'C:\ProgramData\NetGescon\Logs\netgescon-tapi-dotnet-events-live.log', [string]$InteropOutputDir = '', [int]$RestartDelaySeconds = 5, [ValidateSet('system-startup', 'current-user-logon')] [string]$Mode = 'system-startup', [switch]$IncludeDiagnosticEvents ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' function Ensure-ParentDirectory { param([string]$Path) if ([string]::IsNullOrWhiteSpace($Path)) { return } $parent = Split-Path -Path $Path -Parent if (-not [string]::IsNullOrWhiteSpace($parent) -and -not (Test-Path $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null } } function Ensure-Directory { param([string]$Path) if ([string]::IsNullOrWhiteSpace($Path)) { return } if (-not (Test-Path $Path)) { New-Item -ItemType Directory -Path $Path -Force | Out-Null } } function Test-IsAdministrator { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal]::new($identity) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } $runnerPath = Join-Path $PSScriptRoot 'start-netgescon-panasonic-live.ps1' if (-not (Test-Path $runnerPath)) { throw "Runner non trovato: $runnerPath" } if ([string]::IsNullOrWhiteSpace($InteropOutputDir)) { if (-not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) { $InteropOutputDir = Join-Path $env:LOCALAPPDATA 'NetGescon\tapi-interop' } else { $InteropOutputDir = 'C:\ProgramData\NetGescon\tapi-interop' } } Ensure-ParentDirectory -Path $LogFile Ensure-Directory -Path $InteropOutputDir $argumentList = @( '-NoProfile' '-ExecutionPolicy', 'Bypass' '-File', ('"{0}"' -f $runnerPath) '-BaseUrl', ('"{0}"' -f $BaseUrl) '-Extensions', ('"{0}"' -f $Extensions) '-LogFile', ('"{0}"' -f $LogFile) '-InteropOutputDir', ('"{0}"' -f $InteropOutputDir) '-RestartDelaySeconds', $RestartDelaySeconds ) if ($IncludeDiagnosticEvents.IsPresent) { $argumentList += '-IncludeDiagnosticEvents' } $action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ($argumentList -join ' ') $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries if ($Mode -eq 'system-startup') { if (-not (Test-IsAdministrator)) { throw 'La modalita system-startup richiede PowerShell avviata come amministratore.' } $trigger = New-ScheduledTaskTrigger -AtStartup $principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -RunLevel Highest -LogonType ServiceAccount } else { $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name $trigger = New-ScheduledTaskTrigger -AtLogOn -User $currentUser $principal = New-ScheduledTaskPrincipal -UserId $currentUser -RunLevel Limited -LogonType Interactive } Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Settings $settings -Principal $principal -Force -ErrorAction Stop | Out-Null Write-Host "Task installato: $TaskName" Write-Host ("Modalita: {0}" -f $Mode) Write-Host ("Log file: {0}" -f $LogFile) Write-Host ("Interop dir: {0}" -f $InteropOutputDir) Write-Host ('Per avviarlo subito: Start-ScheduledTask -TaskName "{0}"' -f $TaskName)