package powershell import ( "text/template" ) type elevatedOptions struct { User string Password string TaskName string TaskDescription string EncodedCommand string } var elevatedTemplate = template.Must(template.New("ElevatedCommand").Parse(` $name = "{{.TaskName}}" $log = "$env:TEMP\$name.out" $s = New-Object -ComObject "Schedule.Service" $s.Connect() $t = $s.NewTask($null) $t.XmlText = @' {{.TaskDescription}} {{.User}} Password HighestAvailable IgnoreNew false false true false false false false true true false false false PT24H 4 cmd /c powershell.exe -EncodedCommand {{.EncodedCommand}} > %TEMP%\{{.TaskName}}.out 2>&1 '@ if (Test-Path variable:global:ProgressPreference){$ProgressPreference="SilentlyContinue"} $f = $s.GetFolder("\") $f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", "{{.Password}}", 1, $null) | Out-Null $t = $f.GetTask("\$name") $t.Run($null) | Out-Null $timeout = 10 $sec = 0 while ((!($t.state -eq 4)) -and ($sec -lt $timeout)) { Start-Sleep -s 1 $sec++ } $line = 0 do { Start-Sleep -m 100 if (Test-Path $log) { Get-Content $log | select -skip $line | ForEach { $line += 1 Write-Output "$_" } } } while (!($t.state -eq 3)) $result = $t.LastTaskResult [System.Runtime.Interopservices.Marshal]::ReleaseComObject($s) | Out-Null exit $result`))