foam_sync.ps1 (LIFEBALANCE) 2025-06-19T19:20:07Z

This commit is contained in:
Ben Miller
2025-06-19 19:20:07 -06:00
parent 85cc329349
commit 47ece60efe

View File

@ -16,20 +16,20 @@ $scriptName = (Get-Item $scriptPath).Name
$taskName = 'FoamGitSync' $taskName = 'FoamGitSync'
$taskDescription = "Periodically synchronizes the Git repository at $scriptDir using $scriptName." $taskDescription = "Periodically synchronizes the Git repository at $scriptDir using $scriptName."
$taskExecute = 'c:\windows\system32\conhost.exe' $taskExecute = 'c:\windows\system32\conhost.exe'
$taskArguments = "--headless powershell.exe -WindowStyle Hidden -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $scriptPath" $taskArguments = "--headless powershell.exe -WindowStyle Hidden -NoProfile -NonInteractive -ExecutionPolicy Bypass -File $scriptPath -Verbose"
$frequencyMinutes = 2 $frequencyMinutes = 2
$timeLimitSeconds = 30 $timeLimitSeconds = 30
if (-not (Test-Path -Path $logDir)) { if (-not (Test-Path -Path $logDir)) {
New-Item -ItemType Directory -Path $logDir -Force | Out-Null New-Item -ItemType Directory -Path $logDir -Force | Out-Null
} }
Start-Transcript -Path $logFilePath -Append -IncludeInvocationHeader -Force Start-Transcript -Path $logFilePath -IncludeInvocationHeader -Force
try { try {
Write-Host "Script: $scriptName at $scriptPath" Write-Verbose "Script: $scriptName at $scriptPath"
Write-Host "Repository directory: $scriptDir" Write-Verbose "Repository directory: $scriptDir"
Write-Host "Sync frequency: Every $frequencyMinutes minutes" Write-Verbose "Sync frequency: Every $frequencyMinutes minutes"
Write-Host "Log file: $logFilePath" Write-Verbose "Log file: $logFilePath"
# Run as the user who executes this script. # Run as the user who executes this script.
$taskPrincipal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive $taskPrincipal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive
@ -48,9 +48,6 @@ try {
Write-Verbose "Principal: $taskPrincipal" Write-Verbose "Principal: $taskPrincipal"
Write-Verbose "Settings: $settings" Write-Verbose "Settings: $settings"
# Task settings
# Check and configure the scheduled task # Check and configure the scheduled task
try { try {
$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
@ -71,7 +68,6 @@ try {
Write-Warning "trigger does not match:`n $(Compare-Object -IncludeEqual -ReferenceObject $currentTrigger -DifferenceObject $trigger)" Write-Warning "trigger does not match:`n $(Compare-Object -IncludeEqual -ReferenceObject $currentTrigger -DifferenceObject $trigger)"
} }
# Check Action # Check Action
Write-Host "Checking action..." Write-Host "Checking action..."
$actionMatches = $false $actionMatches = $false
@ -80,8 +76,11 @@ try {
-and $currentAction.Arguments -eq $taskArguments) { -and $currentAction.Arguments -eq $taskArguments) {
$actionMatches = $true $actionMatches = $true
} }
else {
Write-Warning "action does not match:`n $(Compare-Object -IncludeEqual -ReferenceObject $currentAction -DifferenceObject $action)"
}
# Check Principal (example, can be expanded) # Check Principal
$principalMatches = ($existingTask.Principal.UserId -eq $taskPrincipal.UserId) $principalMatches = ($existingTask.Principal.UserId -eq $taskPrincipal.UserId)
# Check Settings (specifically ExecutionTimeLimit for this change) # Check Settings (specifically ExecutionTimeLimit for this change)