2015-06-10 19:47:26 +00:00
|
|
|
param([String]$username, [String]$password, [String]$encoded_command)
|
2014-04-24 14:39:58 +00:00
|
|
|
|
|
|
|
$task_name = "WinRM_Elevated_Shell"
|
|
|
|
$out_file = "$env:SystemRoot\Temp\WinRM_Elevated_Shell.log"
|
|
|
|
|
|
|
|
if (Test-Path $out_file) {
|
|
|
|
del $out_file
|
|
|
|
}
|
|
|
|
|
|
|
|
$task_xml = @'
|
|
|
|
<?xml version="1.0" encoding="UTF-16"?>
|
|
|
|
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
|
|
|
<Principals>
|
|
|
|
<Principal id="Author">
|
2015-06-10 19:47:26 +00:00
|
|
|
<UserId>{username}</UserId>
|
2014-04-24 14:39:58 +00:00
|
|
|
<LogonType>Password</LogonType>
|
|
|
|
<RunLevel>HighestAvailable</RunLevel>
|
|
|
|
</Principal>
|
|
|
|
</Principals>
|
|
|
|
<Settings>
|
|
|
|
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
|
|
|
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
|
|
|
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
|
|
|
<AllowHardTerminate>true</AllowHardTerminate>
|
|
|
|
<StartWhenAvailable>false</StartWhenAvailable>
|
|
|
|
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
|
|
|
<IdleSettings>
|
2015-03-12 20:25:40 +00:00
|
|
|
<StopOnIdleEnd>false</StopOnIdleEnd>
|
2014-04-24 14:39:58 +00:00
|
|
|
<RestartOnIdle>false</RestartOnIdle>
|
|
|
|
</IdleSettings>
|
|
|
|
<AllowStartOnDemand>true</AllowStartOnDemand>
|
|
|
|
<Enabled>true</Enabled>
|
|
|
|
<Hidden>false</Hidden>
|
|
|
|
<RunOnlyIfIdle>false</RunOnlyIfIdle>
|
|
|
|
<WakeToRun>false</WakeToRun>
|
|
|
|
<ExecutionTimeLimit>PT2H</ExecutionTimeLimit>
|
|
|
|
<Priority>4</Priority>
|
|
|
|
</Settings>
|
|
|
|
<Actions Context="Author">
|
|
|
|
<Exec>
|
|
|
|
<Command>cmd</Command>
|
|
|
|
<Arguments>{arguments}</Arguments>
|
|
|
|
</Exec>
|
|
|
|
</Actions>
|
|
|
|
</Task>
|
|
|
|
'@
|
|
|
|
|
|
|
|
$arguments = "/c powershell.exe -EncodedCommand $encoded_command > $out_file 2>&1"
|
|
|
|
|
|
|
|
$task_xml = $task_xml.Replace("{arguments}", $arguments)
|
2015-06-10 19:47:26 +00:00
|
|
|
$task_xml = $task_xml.Replace("{username}", $username)
|
2014-04-24 14:39:58 +00:00
|
|
|
|
|
|
|
$schedule = New-Object -ComObject "Schedule.Service"
|
|
|
|
$schedule.Connect()
|
|
|
|
$task = $schedule.NewTask($null)
|
|
|
|
$task.XmlText = $task_xml
|
|
|
|
$folder = $schedule.GetFolder("\")
|
2015-06-10 19:47:26 +00:00
|
|
|
$folder.RegisterTaskDefinition($task_name, $task, 6, $username, $password, 1, $null) | Out-Null
|
2014-04-24 14:39:58 +00:00
|
|
|
|
|
|
|
$registered_task = $folder.GetTask("\$task_name")
|
|
|
|
$registered_task.Run($null) | Out-Null
|
|
|
|
|
|
|
|
$timeout = 10
|
|
|
|
$sec = 0
|
|
|
|
while ( (!($registered_task.state -eq 4)) -and ($sec -lt $timeout) ) {
|
|
|
|
Start-Sleep -s 1
|
|
|
|
$sec++
|
|
|
|
}
|
|
|
|
|
2014-05-15 16:17:57 +00:00
|
|
|
function SlurpOutput($out_file, $cur_line) {
|
2014-04-24 14:39:58 +00:00
|
|
|
if (Test-Path $out_file) {
|
2014-05-15 16:17:57 +00:00
|
|
|
get-content $out_file | select -skip $cur_line | ForEach {
|
|
|
|
$cur_line += 1
|
|
|
|
Write-Host "$_"
|
2014-04-24 14:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 16:17:57 +00:00
|
|
|
return $cur_line
|
|
|
|
}
|
|
|
|
|
|
|
|
$cur_line = 0
|
|
|
|
do {
|
|
|
|
Start-Sleep -m 100
|
|
|
|
$cur_line = SlurpOutput $out_file $cur_line
|
2014-04-24 14:39:58 +00:00
|
|
|
} while (!($registered_task.state -eq 3))
|
|
|
|
|
|
|
|
$exit_code = $registered_task.LastTaskResult
|
|
|
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
|
2014-05-15 16:17:57 +00:00
|
|
|
|
2014-04-24 14:39:58 +00:00
|
|
|
exit $exit_code
|