$SetupProcess = Start-Process -FilePath $SetupPath -ArgumentList "$arguments" -PassThru -NoNewWindow
$Signature = @"
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AttachConsole(uint dwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern bool FreeConsole();
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
"@
Add-Type -Namespace
Win32 -Name Funcs -MemberDefinition $Signature -PassThru | Out-Null
$StartTime = Get-Date
do {
$TimeElapsed = $(Get-Date) - $StartTime
if ($($TimeElapsed).TotalMinutes -ge 5) {
Write-Output "Could not find EULA choice process after 5 minutes."
throw
}
Start-Sleep -Seconds 1
$SetupProcessId = Get-WmiObject Win32_Process | Where-Object {$_.ProcessName -eq "Setup.tmp"} | Select-Object -ExpandProperty ProcessId
$CmdProcessId = Get-WmiObject Win32_Process | Where-Object {$_.ParentProcessId -eq $SetupProcessId} | Select-Object -ExpandProperty ProcessId
} until ($CmdProcessId -ne 0)
$WM_KEYDOWN = 0x0100
$VK_Y_KEY = 0x59
[
Win32.Funcs]::FreeConsole() | Out-Null
[
Win32.Funcs]::AttachConsole($CmdProcessId) | Out-Null
$
Handle = [
Win32.Funcs]::GetConsoleWindow()
[
Win32.Funcs]::PostMessage($
Handle, $WM_KEYDOWN, $VK_Y_KEY, 0) | Out-Null
[
Win32.Funcs]::FreeConsole() | Out-Null