[CmdletBinding()] param( [string]$ScriptPath = '\\nas\MST\codex\assistent\deploy\windows\Invoke-DomovoyProductionMonitoring.ps1', [string]$TaskName = 'Domovoy Production Health Monitor' ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest if (-not (Test-Path -LiteralPath $ScriptPath -PathType Leaf)) { throw 'Monitoring script not found' } $identity = [Security.Principal.WindowsIdentity]::GetCurrent().Name $pwsh = (Get-Command pwsh.exe -ErrorAction Stop).Source $arguments = "-NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$ScriptPath`"" $action = New-ScheduledTaskAction -Execute $pwsh -Argument $arguments $repeat = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration (New-TimeSpan -Days 3650) $logon = New-ScheduledTaskTrigger -AtLogOn -User $identity $principal = New-ScheduledTaskPrincipal -UserId $identity -LogonType Interactive -RunLevel Limited $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Minutes 10) Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger @($repeat, $logon) -Principal $principal -Settings $settings -Description 'Monitor Domovoy production health and notify the interactive user about transitions.' -Force | Out-Null Write-Output "Scheduled task registered: $TaskName"