github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/provisioner/powershell/elevated.go (about)

     1  package powershell
     2  
     3  import (
     4  	"text/template"
     5  )
     6  
     7  type elevatedOptions struct {
     8  	User            string
     9  	Password        string
    10  	TaskName        string
    11  	TaskDescription string
    12  	EncodedCommand  string
    13  }
    14  
    15  var elevatedTemplate = template.Must(template.New("ElevatedCommand").Parse(`
    16  $name = "{{.TaskName}}"
    17  $log = "$env:TEMP\$name.out"
    18  $s = New-Object -ComObject "Schedule.Service"
    19  $s.Connect()
    20  $t = $s.NewTask($null)
    21  $t.XmlText = @'
    22  <?xml version="1.0" encoding="UTF-16"?>
    23  <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    24    <RegistrationInfo>
    25  	<Description>{{.TaskDescription}}</Description>
    26    </RegistrationInfo>
    27    <Principals>
    28      <Principal id="Author">
    29        <UserId>{{.User}}</UserId>
    30        <LogonType>Password</LogonType>
    31        <RunLevel>HighestAvailable</RunLevel>
    32      </Principal>
    33    </Principals>
    34    <Settings>
    35      <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    36      <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    37      <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    38      <AllowHardTerminate>true</AllowHardTerminate>
    39      <StartWhenAvailable>false</StartWhenAvailable>
    40      <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    41      <IdleSettings>
    42        <StopOnIdleEnd>false</StopOnIdleEnd>
    43        <RestartOnIdle>false</RestartOnIdle>
    44      </IdleSettings>
    45      <AllowStartOnDemand>true</AllowStartOnDemand>
    46      <Enabled>true</Enabled>
    47      <Hidden>false</Hidden>
    48      <RunOnlyIfIdle>false</RunOnlyIfIdle>
    49      <WakeToRun>false</WakeToRun>
    50      <ExecutionTimeLimit>PT24H</ExecutionTimeLimit>
    51      <Priority>4</Priority>
    52    </Settings>
    53    <Actions Context="Author">
    54      <Exec>
    55        <Command>cmd</Command>
    56  	  <Arguments>/c powershell.exe -EncodedCommand {{.EncodedCommand}} &gt; %TEMP%\{{.TaskName}}.out 2&gt;&amp;1</Arguments>
    57      </Exec>
    58    </Actions>
    59  </Task>
    60  '@
    61  $f = $s.GetFolder("\")
    62  $f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", "{{.Password}}", 1, $null) | Out-Null
    63  $t = $f.GetTask("\$name")
    64  $t.Run($null) | Out-Null
    65  $timeout = 10
    66  $sec = 0
    67  while ((!($t.state -eq 4)) -and ($sec -lt $timeout)) {
    68    Start-Sleep -s 1
    69    $sec++
    70  }
    71  function SlurpOutput($l) {
    72    if (Test-Path $log) {
    73      Get-Content $log | select -skip $l | ForEach {
    74        $l += 1
    75        Write-Host "$_"
    76      }
    77    }
    78    return $l
    79  }
    80  $line = 0
    81  do {
    82    Start-Sleep -m 100
    83    $line = SlurpOutput $line
    84  } while (!($t.state -eq 3))
    85  $result = $t.LastTaskResult
    86  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($s) | Out-Null
    87  exit $result`))