github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  if (Test-Path variable:global:ProgressPreference){$ProgressPreference="SilentlyContinue"}
    62  $f = $s.GetFolder("\")
    63  $f.RegisterTaskDefinition($name, $t, 6, "{{.User}}", "{{.Password}}", 1, $null) | Out-Null
    64  $t = $f.GetTask("\$name")
    65  $t.Run($null) | Out-Null
    66  $timeout = 10
    67  $sec = 0
    68  while ((!($t.state -eq 4)) -and ($sec -lt $timeout)) {
    69    Start-Sleep -s 1
    70    $sec++
    71  }
    72  
    73  $line = 0
    74  do {
    75    Start-Sleep -m 100
    76    if (Test-Path $log) {
    77      Get-Content $log | select -skip $line | ForEach {
    78        $line += 1
    79        Write-Output "$_"
    80      }
    81    }
    82  } while (!($t.state -eq 3))
    83  $result = $t.LastTaskResult
    84  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($s) | Out-Null
    85  exit $result`))