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 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' } } $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)