|
| 1 | +#Requires -RunAsAdministrator |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Agenda pull condicional nos repos contratos* (WSL): |
| 5 | + terca e quinta as 09:00. |
| 6 | +
|
| 7 | +.PARAMETER Time |
| 8 | + Horario (HH:mm). Padrao: 09:00. |
| 9 | +
|
| 10 | +.PARAMETER DaysOfWeek |
| 11 | + Dias da semana. Padrao: Tuesday, Thursday. |
| 12 | +
|
| 13 | +.PARAMETER Test |
| 14 | + Executa executar_pull_repos.bat apos criar/atualizar a tarefa. |
| 15 | +
|
| 16 | +.PARAMETER Force |
| 17 | + Substitui a tarefa existente sem perguntar. |
| 18 | +#> |
| 19 | +param( |
| 20 | + [string]$Time = "09:00", |
| 21 | + [ValidateSet("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")] |
| 22 | + [System.DayOfWeek[]]$DaysOfWeek = @([System.DayOfWeek]::Tuesday, [System.DayOfWeek]::Thursday), |
| 23 | + [switch]$Test, |
| 24 | + [switch]$Force |
| 25 | +) |
| 26 | + |
| 27 | +$ErrorActionPreference = "Stop" |
| 28 | + |
| 29 | +$colors = @{ |
| 30 | + Success = "Green" |
| 31 | + Error = "Red" |
| 32 | + Warning = "Yellow" |
| 33 | +} |
| 34 | + |
| 35 | +Write-Host "" |
| 36 | +Write-Host "================================================================" |
| 37 | +Write-Host " AGENDAMENTO - Pull condicional (contratos* / WSL)" |
| 38 | +Write-Host "================================================================" |
| 39 | +Write-Host "" |
| 40 | + |
| 41 | +$admin = [Security.Principal.WindowsIdentity]::GetCurrent() |
| 42 | +$principalCheck = New-Object Security.Principal.WindowsPrincipal($admin) |
| 43 | +if (-not $principalCheck.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| 44 | + Write-Host "ERRO - Execute como Administrador" -ForegroundColor $colors.Error |
| 45 | + exit 1 |
| 46 | +} |
| 47 | + |
| 48 | +$WORKSPACE_DIR = Split-Path -Parent $PSScriptRoot |
| 49 | +$BATCH_FILE = Join-Path $WORKSPACE_DIR "executar_pull_repos.bat" |
| 50 | +$TASK_NAME = "MGI-Pull-Repos-Main" |
| 51 | +$RUN_AS_USER = "$env:USERDOMAIN\$env:USERNAME" |
| 52 | +$dayLabels = ($DaysOfWeek | ForEach-Object { $_.ToString() }) -join ", " |
| 53 | + |
| 54 | +Write-Host "Workspace: $WORKSPACE_DIR" |
| 55 | +Write-Host "Script: executar_pull_repos.bat" |
| 56 | +Write-Host "Tarefa: $TASK_NAME" |
| 57 | +Write-Host "Horario: $Time" |
| 58 | +Write-Host "Dias: $dayLabels" |
| 59 | +Write-Host "Fluxo: fetch + detecta branch (origin/HEAD / master) + pull --ff-only se remoto a frente" |
| 60 | +Write-Host "Usuario: $RUN_AS_USER" |
| 61 | +Write-Host "" |
| 62 | + |
| 63 | +if (-not (Test-Path $BATCH_FILE)) { |
| 64 | + Write-Host "ERRO - Arquivo nao encontrado: $BATCH_FILE" -ForegroundColor $colors.Error |
| 65 | + exit 1 |
| 66 | +} |
| 67 | + |
| 68 | +$existingTask = Get-ScheduledTask -TaskName $TASK_NAME -ErrorAction SilentlyContinue |
| 69 | +if ($existingTask) { |
| 70 | + if ($Force) { |
| 71 | + Unregister-ScheduledTask -TaskName $TASK_NAME -Confirm:$false |
| 72 | + Write-Host "OK - Tarefa anterior removida" -ForegroundColor $colors.Success |
| 73 | + } else { |
| 74 | + $choice = Read-Host "Tarefa ja existe. Atualizar? (S/N)" |
| 75 | + if ($choice -notmatch "^[Ss]") { |
| 76 | + Write-Host "Cancelado." |
| 77 | + exit 0 |
| 78 | + } |
| 79 | + Unregister-ScheduledTask -TaskName $TASK_NAME -Confirm:$false |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +$action = New-ScheduledTaskAction ` |
| 84 | + -Execute "cmd.exe" ` |
| 85 | + -Argument "/c `"$BATCH_FILE`"" ` |
| 86 | + -WorkingDirectory $WORKSPACE_DIR |
| 87 | + |
| 88 | +$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek $DaysOfWeek -At $Time |
| 89 | + |
| 90 | +$settings = New-ScheduledTaskSettingsSet ` |
| 91 | + -AllowStartIfOnBatteries ` |
| 92 | + -DontStopIfGoingOnBatteries ` |
| 93 | + -StartWhenAvailable ` |
| 94 | + -RunOnlyIfNetworkAvailable ` |
| 95 | + -MultipleInstances IgnoreNew ` |
| 96 | + -ExecutionTimeLimit (New-TimeSpan -Minutes 30) |
| 97 | + |
| 98 | +$principal = New-ScheduledTaskPrincipal ` |
| 99 | + -UserId $RUN_AS_USER ` |
| 100 | + -LogonType Interactive ` |
| 101 | + -RunLevel Highest |
| 102 | + |
| 103 | +Register-ScheduledTask ` |
| 104 | + -TaskName $TASK_NAME ` |
| 105 | + -Action $action ` |
| 106 | + -Trigger $trigger ` |
| 107 | + -Settings $settings ` |
| 108 | + -Principal $principal ` |
| 109 | + -Description "Pull condicional nos repos contratos* (ter/qui 09:00; branch origin/HEAD)" ` |
| 110 | + -Force | Out-Null |
| 111 | + |
| 112 | +Write-Host "" |
| 113 | +Write-Host "OK - Tarefa criada!" -ForegroundColor $colors.Success |
| 114 | + |
| 115 | +$taskInfo = Get-ScheduledTaskInfo -TaskName $TASK_NAME |
| 116 | +Write-Host "Proxima execucao: $($taskInfo.NextRunTime)" |
| 117 | +Write-Host "Logs: $WORKSPACE_DIR\logs\pull_repos_*.log" |
| 118 | +Write-Host "" |
| 119 | + |
| 120 | +if ($Test -or ((Read-Host "Testar agora? (S/N)") -match "^[Ss]")) { |
| 121 | + Write-Host "Executando teste..." |
| 122 | + $proc = Start-Process -FilePath "cmd.exe" ` |
| 123 | + -ArgumentList "/c `"$BATCH_FILE`"" ` |
| 124 | + -WorkingDirectory $WORKSPACE_DIR ` |
| 125 | + -Wait -PassThru -NoNewWindow |
| 126 | + if ($proc.ExitCode -eq 0) { |
| 127 | + Write-Host "OK - Teste concluido com sucesso" -ForegroundColor $colors.Success |
| 128 | + } else { |
| 129 | + Write-Host "AVISO - Teste retornou codigo $($proc.ExitCode)" -ForegroundColor $colors.Warning |
| 130 | + Write-Host "Consulte o log mais recente em logs\pull_repos_*.log" |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +Write-Host "" |
| 135 | +Write-Host "Gerenciar: taskschd.msc -> $TASK_NAME" |
| 136 | +Write-Host "Remover: desagendar_pull_repos.bat" |
| 137 | +Write-Host "Manual: executar_pull_repos.bat" |
| 138 | +Write-Host "" |
0 commit comments