You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.5 KiB
62 lines
2.5 KiB
[CmdletBinding()]
|
|
param(
|
|
# The account the task runs as. Defaults to the current user, but MUST be passed
|
|
# explicitly when launching this script elevated — if UAC is satisfied with a
|
|
# different admin account, $env:USERNAME inside the elevated process is that
|
|
# admin, not the intended owner, and the task would be registered for the wrong
|
|
# user (whose credential vault lacks the git remote's stored credential).
|
|
[string] $UserId = "$env:USERDOMAIN\$env:USERNAME",
|
|
|
|
# Optional path to write a transcript of the result to, so a caller that cannot
|
|
# see the elevated console can read the outcome.
|
|
[string] $LogPath
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$repositoryRoot = $PSScriptRoot
|
|
$syncScript = Join-Path $repositoryRoot 'sync-skills.ps1'
|
|
|
|
if (-not (Test-Path -LiteralPath $syncScript)) {
|
|
throw "Sync script not found at '$syncScript'."
|
|
}
|
|
|
|
$action = New-ScheduledTaskAction `
|
|
-Execute 'powershell.exe' `
|
|
-Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$syncScript`""
|
|
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 10:00AM
|
|
|
|
# Must run as the interactive user, NOT SYSTEM. The git remote is HTTPS and
|
|
# authenticates via credential.helper=manager, whose vault is per-user — SYSTEM
|
|
# cannot read it, so `git fetch origin main` fails and the whole sync aborts.
|
|
# Interactive logon keeps access to that vault; StartWhenAvailable catches up a
|
|
# missed run at the next logon.
|
|
$currentUser = $UserId
|
|
$principal = New-ScheduledTaskPrincipal -UserId $currentUser -LogonType Interactive -RunLevel Limited
|
|
$settings = New-ScheduledTaskSettingsSet `
|
|
-StartWhenAvailable `
|
|
-RunOnlyIfNetworkAvailable `
|
|
-MultipleInstances IgnoreNew `
|
|
-ExecutionTimeLimit (New-TimeSpan -Hours 1)
|
|
|
|
Register-ScheduledTask `
|
|
-TaskName 'PromptWorks Weekly Skill Sync' `
|
|
-Action $action `
|
|
-Trigger $trigger `
|
|
-Principal $principal `
|
|
-Settings $settings `
|
|
-Description 'Mirrors custom Microsoft Scout skills to PromptWorks.' `
|
|
-Force | Out-Null
|
|
|
|
$msg = "Registered 'PromptWorks Weekly Skill Sync' to run as $currentUser, Sundays at 10:00 AM."
|
|
Write-Host $msg
|
|
if ($LogPath) {
|
|
$info = Get-ScheduledTask -TaskName 'PromptWorks Weekly Skill Sync' | Get-ScheduledTaskInfo
|
|
$task = Get-ScheduledTask -TaskName 'PromptWorks Weekly Skill Sync'
|
|
@(
|
|
"OK: $msg",
|
|
"RunAs : $($task.Principal.UserId)",
|
|
"LogonType: $($task.Principal.LogonType)",
|
|
"State : $($task.State)",
|
|
"NextRun : $($info.NextRunTime)"
|
|
) | Set-Content -LiteralPath $LogPath -Encoding UTF8
|
|
}
|
|
|