1
# Runs a script and established interactive session (session 1) and
2
# tunnels the output such that WSL operations will complete
3
$ErrorActionPreference = 'Stop'
5
if ($Args.Length -lt 1) {
6
Write-Object "Usage: " + $MyInvocation.MyCommand.Name + " <script>"
10
function RegenPassword {
12
$syms = [char[]]([char]'a'..[char]'z' `
13
+ [char]'A'..[char]'Z' `
14
+ [char]'0'..[char]'9')
15
$rnd = [byte[]]::new(32)
16
[System.Security.Cryptography.RandomNumberGenerator]::create().getBytes($rnd)
17
$password = ($rnd | % { $syms[$_ % $syms.length] }) -join ''
18
$encPass = ConvertTo-SecureString $password -AsPlainText -Force
19
Set-LocalUser -Name $username -Password $encPass
26
Write-Output "Location: $cwd"
28
# Reset the password to a new random pass since it's needed in the
30
$pass = RegenPassword "Administrator"
32
$ljob = Start-Job -ArgumentList $cwd -ScriptBlock {
34
Get-Content -Wait "$cwd\tmpout"
36
$pjob = Start-Job -ArgumentList $cwd,$runScript,$pass -ScriptBlock {
37
param($cwd, $runScript, $pass)
38
$pwargs = @("-NonInteractive", "-WindowStyle", "hidden")
39
$command = "& { powershell.exe $pwargs -File " +
40
$runScript + " 3>&1 2>&1 > `"$cwd\tmpout`";" +
41
"Exit `$LastExitCode }"
42
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
43
& psexec -accepteula -w $cwd -i 1 -u Administrator -p $pass `
44
powershell.exe $pwargs -EncodedCommand $encoded
45
if ($LASTEXITCODE -ne 0) {
46
throw "failure running psexec"
50
while ($pjob.State -eq 'Running') {
51
Start-Sleep -Milliseconds 200
58
while ($ljob.HasMoreData) {
60
Start-Sleep -Milliseconds 200
63
if ($pjob.State -eq 'Failed') {
64
Write-Output "Failure occurred, see above. Extra info:"
66
throw "wsl task failed on us!"