Tiny11-Builder/.github/scripts/run-coremaker-automated.ps1
unknown 08f4f50bcc Fix critical script exit issues
- Bypass admin check to prevent early exit (runner is already admin)
- Bypass execution policy check (already set in workflow)
- Improve Read-Host to handle empty prompts (return 'yes' instead of empty)
- Override $myInvocation to prevent admin restart attempts
- Change ErrorActionPreference to Continue for better error visibility
- Add better handling for all prompt types
2025-10-30 17:11:44 +07:00

146 lines
4.5 KiB
PowerShell

<#
.SYNOPSIS
Automated wrapper for tiny11Coremaker.ps1
.DESCRIPTION
This script automates the interactive prompts in tiny11Coremaker.ps1
#>
param(
[Parameter(Mandatory=$true)]
[string]$ISODrive,
[Parameter(Mandatory=$false)]
[bool]$EnableDotNet35 = $false
)
$ErrorActionPreference = 'Continue' # Change to Continue to allow script to continue on errors
# Use GITHUB_WORKSPACE if available, otherwise calculate from PSScriptRoot
if ($env:GITHUB_WORKSPACE) {
$scriptRoot = $env:GITHUB_WORKSPACE
} else {
$scriptRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
}
$scriptPath = Join-Path $scriptRoot "tiny11Coremaker.ps1"
if (-not (Test-Path $scriptPath)) {
Write-Error "Script not found: $scriptPath"
Write-Host "PSScriptRoot: $PSScriptRoot"
Write-Host "scriptRoot: $scriptRoot"
Write-Host "Current location: $(Get-Location)"
exit 1
}
Write-Host "Starting automated tiny11Coremaker script"
Write-Host "ISO Drive: $ISODrive"
Write-Host "Enable .NET 3.5: $EnableDotNet35"
# Read the script content
$scriptContent = Get-Content $scriptPath -Raw -ErrorAction Stop
# Fix missing $ScratchDisk variable (should be $mainOSDrive)
$scriptContent = $scriptContent -replace '\$ScratchDisk', '$mainOSDrive'
# Disable admin restart check by commenting out the exit
$scriptContent = $scriptContent -replace 'if \(!\s*\$myWindowsPrincipal\.IsInRole\(\$adminRole\)\)', 'if ($false -and !$myWindowsPrincipal.IsInRole($adminRole)) # Disabled for GitHub Actions'
# Disable execution policy check by commenting out the exit
$scriptContent = $scriptContent -replace 'if \(\(Get-ExecutionPolicy\) -eq ''Restricted''\)', 'if ($false -and (Get-ExecutionPolicy) -eq ''Restricted'') # Disabled for GitHub Actions'
# Create a temporary script with auto-answers
$ISODriveLetter = $ISODrive -replace ':', ''
$dotNetAnswer = if ($EnableDotNet35) { "y" } else { "n" }
$tempScriptHeader = @"
`$ErrorActionPreference = 'Continue' # Continue on errors to see full output
# Override Read-Host BEFORE any script execution to ensure all prompts are handled
function Read-Host {
param([string]`$Prompt)
Write-Host "`$Prompt"
# Auto-answer execution policy prompt (yes/no) - handle empty prompt too
if (`$Prompt -eq "" -or `$Prompt -match "change it to RemoteSigned" -or `$Prompt -match "Execution Policy" -or `$Prompt -match "Restricted") {
Write-Host "Auto-answering: yes"
return "yes"
}
# Auto-answer continue prompt (y/n)
if (`$Prompt -match "Do you want to continue") {
Write-Host "Auto-answering: y"
return "y"
}
# Auto-answer drive letter prompt
if (`$Prompt -match "enter the drive letter") {
Write-Host "Auto-answering: $ISODriveLetter"
return "$ISODriveLetter"
}
# Auto-answer image index prompt
if (`$Prompt -match "enter the image index") {
Write-Host "Auto-answering: 1"
return "1"
}
# Auto-answer .NET 3.5 prompt
if (`$Prompt -match "enable .NET 3.5") {
Write-Host "Auto-answering: $dotNetAnswer"
return "$dotNetAnswer"
}
# Auto-answer Press Enter prompt
if (`$Prompt -match "Press") {
return ""
}
# Default: return empty or "yes" for empty prompts
if (`$Prompt -eq "") {
return "yes"
}
return ""
}
# Override $myInvocation to prevent admin restart
`$script:myInvocation = @{
MyCommand = @{
Definition = `$PSCommandPath
}
}
"@
$tempScriptPath = Join-Path $env:TEMP "tiny11coremaker-automated-$(Get-Date -Format 'yyyyMMddHHmmss').ps1"
# Write header first
$tempScriptHeader | Out-File -FilePath $tempScriptPath -Encoding UTF8
# Append script content
$scriptContent | Out-File -FilePath $tempScriptPath -Append -Encoding UTF8
try {
# Change to script directory
Push-Location $scriptRoot
Write-Host "Changed to directory: $(Get-Location)"
Write-Host "Running script: $tempScriptPath"
# Run the modified script and capture exit code
& $tempScriptPath
$scriptExitCode = $LASTEXITCODE
if ($scriptExitCode -ne 0 -and $scriptExitCode -ne $null) {
Write-Error "Script exited with code: $scriptExitCode"
exit $scriptExitCode
}
} catch {
Write-Error "Error running script: $_"
Write-Error "Exception: $($_.Exception.Message)"
Write-Error "Line: $($_.InvocationInfo.ScriptLineNumber)"
exit 1
} finally {
Pop-Location
# Cleanup temp script
Remove-Item -Path $tempScriptPath -Force -ErrorAction SilentlyContinue
}