Files
douyi_guaji_main/scripts/setup_auto_commit.ps1
T

103 lines
3.8 KiB
PowerShell

# Setup Auto Commit Task Script
# This script will create a Windows scheduled task to run git auto commit every hour
# Check if running as administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "This script requires administrator privileges." -ForegroundColor Red
Write-Host "Please run PowerShell as Administrator and try again." -ForegroundColor Yellow
Write-Host ""
Write-Host "Right-click PowerShell -> Run as Administrator" -ForegroundColor Yellow
pause
exit 1
}
$taskName = "Git自动提交推送"
$scriptPath = Join-Path $PSScriptRoot "auto_commit_push.ps1"
$workingDir = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Setting up Git Auto Commit Task" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Check if task already exists
$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($existingTask) {
Write-Host "Task '$taskName' already exists." -ForegroundColor Yellow
$response = Read-Host "Do you want to remove and recreate it? (Y/N)"
if ($response -eq "Y" -or $response -eq "y") {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Host "Old task removed." -ForegroundColor Green
} else {
Write-Host "Cancelled." -ForegroundColor Yellow
exit 0
}
}
Write-Host "Creating scheduled task..." -ForegroundColor Yellow
# Create action
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -File `"$scriptPath`"" `
-WorkingDirectory $workingDir
# Create trigger (start now, repeat every hour, duration 365 days)
$trigger = New-ScheduledTaskTrigger `
-Once `
-At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Hours 1) `
-RepetitionDuration (New-TimeSpan -Days 365)
# Create principal (run with highest privileges)
$principal = New-ScheduledTaskPrincipal `
-UserId "$env:USERNAME" `
-RunLevel Highest
# Create settings
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-MultipleInstances IgnoreNew
# Register the task
try {
Register-ScheduledTask `
-TaskName $taskName `
-Action $action `
-Trigger $trigger `
-Principal $principal `
-Settings $settings `
-Description "每小时自动保存修改、提交并推送到GitHub" | Out-Null
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "Task created successfully!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Task Name: $taskName" -ForegroundColor Cyan
Write-Host "Schedule: Every 1 hour" -ForegroundColor Cyan
Write-Host "Script: $scriptPath" -ForegroundColor Cyan
Write-Host ""
Write-Host "You can manage this task in Task Scheduler:" -ForegroundColor Yellow
Write-Host " Task Scheduler -> Task Scheduler Library -> $taskName" -ForegroundColor Yellow
Write-Host ""
Write-Host "To test the task, run:" -ForegroundColor Yellow
Write-Host " Start-ScheduledTask -TaskName '$taskName'" -ForegroundColor White
Write-Host ""
Write-Host "Or manually run: .\auto_commit_push.ps1" -ForegroundColor Yellow
Write-Host ""
} catch {
Write-Host ""
Write-Host "Error creating task: $_" -ForegroundColor Red
Write-Host "Please check the error message above." -ForegroundColor Yellow
exit 1
}
pause