|
| 1 | +Set-StrictMode -Version Latest |
| 2 | +$ErrorActionPreference = 'Stop' |
| 3 | +$ProgressPreference = 'SilentlyContinue' |
| 4 | +trap { |
| 5 | + Write-Output "ERROR: $_" |
| 6 | + Write-Output (($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1') |
| 7 | + Write-Output (($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1') |
| 8 | + Exit 1 |
| 9 | +} |
| 10 | +$name = "{{.TaskName}}" |
| 11 | +$log = "$env:SystemRoot\Temp\$name.out" |
| 12 | +$s = New-Object -ComObject "Schedule.Service" |
| 13 | +$s.Connect() |
| 14 | +$t = $s.NewTask($null) |
| 15 | +$t.XmlText = @' |
| 16 | +<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> |
| 17 | + <RegistrationInfo> |
| 18 | + <Description>{{.TaskDescription}}</Description> |
| 19 | + </RegistrationInfo> |
| 20 | + <Principals> |
| 21 | + <Principal id="Author"> |
| 22 | + <UserId>{{.Username}}</UserId> |
| 23 | + <LogonType>Password</LogonType> |
| 24 | + <RunLevel>HighestAvailable</RunLevel> |
| 25 | + </Principal> |
| 26 | + </Principals> |
| 27 | + <Settings> |
| 28 | + <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> |
| 29 | + <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> |
| 30 | + <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries> |
| 31 | + <AllowHardTerminate>true</AllowHardTerminate> |
| 32 | + <StartWhenAvailable>false</StartWhenAvailable> |
| 33 | + <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> |
| 34 | + <IdleSettings> |
| 35 | + <StopOnIdleEnd>false</StopOnIdleEnd> |
| 36 | + <RestartOnIdle>false</RestartOnIdle> |
| 37 | + </IdleSettings> |
| 38 | + <AllowStartOnDemand>true</AllowStartOnDemand> |
| 39 | + <Enabled>true</Enabled> |
| 40 | + <Hidden>false</Hidden> |
| 41 | + <RunOnlyIfIdle>false</RunOnlyIfIdle> |
| 42 | + <WakeToRun>false</WakeToRun> |
| 43 | + <ExecutionTimeLimit>PT24H</ExecutionTimeLimit> |
| 44 | + <Priority>4</Priority> |
| 45 | + </Settings> |
| 46 | + <Actions Context="Author"> |
| 47 | + <Exec> |
| 48 | + <Command>cmd</Command> |
| 49 | + <Arguments>/c {{.Command}} >%SYSTEMROOT%\Temp\{{.TaskName}}.out 2>&1</Arguments> |
| 50 | + </Exec> |
| 51 | + </Actions> |
| 52 | +</Task> |
| 53 | +'@ |
| 54 | +$f = $s.GetFolder("\") |
| 55 | +$f.RegisterTaskDefinition($name, $t, 6, "{{.Username}}", "{{.Password}}", 1, $null) | Out-Null |
| 56 | +$t = $f.GetTask("\$name") |
| 57 | +$t.Run($null) | Out-Null |
| 58 | +$timeout = 10 |
| 59 | +$sec = 0 |
| 60 | +while ((!($t.state -eq 4)) -and ($sec -lt $timeout)) { |
| 61 | + Start-Sleep -s 1 |
| 62 | + $sec++ |
| 63 | +} |
| 64 | +$line = 0 |
| 65 | +do { |
| 66 | + Start-Sleep -m 100 |
| 67 | + if (Test-Path $log) { |
| 68 | + Get-Content $log | select -skip $line | ForEach { |
| 69 | + ++$line |
| 70 | + Write-Output $_ |
| 71 | + } |
| 72 | + } |
| 73 | +} while (!($t.state -eq 3)) |
| 74 | +$result = $t.LastTaskResult |
| 75 | +if (Test-Path $log) { |
| 76 | + Remove-Item $log -Force -ErrorAction SilentlyContinue | Out-Null |
| 77 | +} |
| 78 | +[System.Runtime.Interopservices.Marshal]::ReleaseComObject($s) | Out-Null |
| 79 | +exit $result |
0 commit comments