101 lines
3.1 KiB
PowerShell
101 lines
3.1 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string] $TaskName = 'KRI Agent Harness Workspace Verify',
|
|
[string] $At = '08:30',
|
|
[string] $WorkspaceRoot,
|
|
[switch] $Unregister
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$harnessRoot = Split-Path -Parent $scriptRoot
|
|
$runnerScript = Join-Path $scriptRoot 'run-scheduled-verification.ps1'
|
|
|
|
if ($Unregister) {
|
|
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
|
if ($null -eq $existingTask) {
|
|
Write-Host "Scheduled task is not registered: $TaskName"
|
|
exit 0
|
|
}
|
|
|
|
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
|
Write-Host "Scheduled task removed: $TaskName"
|
|
exit 0
|
|
}
|
|
|
|
if ([string]::IsNullOrWhiteSpace($WorkspaceRoot)) {
|
|
$WorkspaceRoot = Split-Path -Parent $harnessRoot
|
|
}
|
|
|
|
$scheduledTime = [TimeSpan]::Zero
|
|
if (-not [TimeSpan]::TryParseExact(
|
|
$At,
|
|
'hh\:mm',
|
|
[System.Globalization.CultureInfo]::InvariantCulture,
|
|
[ref] $scheduledTime
|
|
)) {
|
|
throw "Scheduled time must use HH:mm format. Found: $At"
|
|
}
|
|
|
|
$resolvedWorkspaceRoot = [System.IO.Path]::GetFullPath($WorkspaceRoot)
|
|
$expectedHarnessRoot = [System.IO.Path]::GetFullPath(
|
|
(Join-Path $resolvedWorkspaceRoot '_________AgentHarnessBase')
|
|
)
|
|
$resolvedHarnessRoot = [System.IO.Path]::GetFullPath($harnessRoot)
|
|
|
|
if (-not $resolvedHarnessRoot.Equals(
|
|
$expectedHarnessRoot,
|
|
[System.StringComparison]::OrdinalIgnoreCase
|
|
)) {
|
|
throw "Harness repository must be a direct child of the configured workspace root. Expected: $expectedHarnessRoot"
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $runnerScript -PathType Leaf)) {
|
|
throw "Scheduled verification runner not found: $runnerScript"
|
|
}
|
|
|
|
$account = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
$powershellCommand = Join-Path $PSHOME 'powershell.exe'
|
|
$actionArguments = @(
|
|
'-NoProfile'
|
|
'-NonInteractive'
|
|
'-ExecutionPolicy Bypass'
|
|
"-File `"$runnerScript`""
|
|
"-WorkspaceRoot `"$resolvedWorkspaceRoot`""
|
|
) -join ' '
|
|
|
|
$action = New-ScheduledTaskAction `
|
|
-Execute $powershellCommand `
|
|
-Argument $actionArguments `
|
|
-WorkingDirectory $resolvedHarnessRoot
|
|
$trigger = New-ScheduledTaskTrigger `
|
|
-Weekly `
|
|
-WeeksInterval 1 `
|
|
-DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday `
|
|
-At ([DateTime]::Today.Add($scheduledTime))
|
|
$principal = New-ScheduledTaskPrincipal `
|
|
-UserId $account `
|
|
-LogonType Interactive `
|
|
-RunLevel Limited
|
|
$settings = New-ScheduledTaskSettingsSet `
|
|
-StartWhenAvailable `
|
|
-MultipleInstances IgnoreNew `
|
|
-RestartCount 3 `
|
|
-RestartInterval (New-TimeSpan -Minutes 10) `
|
|
-ExecutionTimeLimit (New-TimeSpan -Hours 1)
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName $TaskName `
|
|
-Action $action `
|
|
-Trigger $trigger `
|
|
-Principal $principal `
|
|
-Settings $settings `
|
|
-Description 'Runs the read-only KRI Agent Harness workspace verification and stores a local log.' `
|
|
-Force | Out-Null
|
|
|
|
Write-Host "Scheduled task registered: $TaskName"
|
|
Write-Host "User: $account"
|
|
Write-Host "Schedule: Monday-Friday $At"
|
|
Write-Host "Workspace: $resolvedWorkspaceRoot"
|