mirror of
https://github.com/ntdevlabs/tiny11builder.git
synced 2025-12-19 09:54:15 +00:00
196 lines
7 KiB
PowerShell
196 lines
7 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Automated wrapper for tiny11maker.ps1
|
|
.DESCRIPTION
|
|
This script automates the interactive prompts in tiny11maker.ps1 when ISO parameter is not provided
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ISODrive,
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$ScratchDrive = ""
|
|
)
|
|
|
|
$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 "tiny11maker.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 tiny11maker script"
|
|
Write-Host "ISO Drive: $ISODrive"
|
|
Write-Host "Scratch Drive: $ScratchDrive"
|
|
|
|
# Since tiny11maker.ps1 supports parameters, we can use them directly
|
|
# But we need to handle image index selection automatically
|
|
$ISODriveLetter = $ISODrive -replace ':', ''
|
|
|
|
# Read the script content to modify it for automation
|
|
$scriptContent = Get-Content $scriptPath -Raw -ErrorAction Stop
|
|
|
|
# Robustly disable admin restart check by replacing the entire if-block with a no-op
|
|
$adminBlockPattern = 'if\s*\(\s*!\s*\$myWindowsPrincipal\.IsInRole\(\$adminRole\)\s*\)\s*\{[\s\S]*?\}'
|
|
$scriptContent = [regex]::Replace($scriptContent, $adminBlockPattern, 'if ($false) { }')
|
|
# Also handle any previously injected pattern without braces
|
|
$scriptContent = $scriptContent -replace 'if \(\$false -and \s*!\s*\$myWindowsPrincipal\.IsInRole\(\$adminRole\)\s*\)', 'if ($false)'
|
|
|
|
# Robustly disable execution policy check by replacing the entire if-block with a no-op
|
|
$execPolicyBlockPattern = 'if\s*\([^\)]*Get-ExecutionPolicy[^\)]*\)\s*\{[\s\S]*?\}'
|
|
$scriptContent = [regex]::Replace($scriptContent, $execPolicyBlockPattern, 'if ($false) { }')
|
|
# Handle one-line or previously injected variants without braces
|
|
$scriptContent = $scriptContent -replace 'if \([^\)]*Get-Execution-?Policy[^\)]*\)', 'if ($false)'
|
|
$scriptContent = $scriptContent -replace 'if \(\$false -and[^\)]*Get-Execution-?Policy[^\)]*\)', 'if ($false)'
|
|
|
|
# Final safety: convert any "if ($false -and <anything>) {<maybe missing>" into a proper no-op block
|
|
$scriptContent = [regex]::Replace($scriptContent, 'if\s*\(\s*\$false\s*-and[\s\S]*?\)', 'if ($false) { }')
|
|
|
|
# Add error checking after oscdimg command to verify ISO was created
|
|
# Match the oscdimg command line ending with tiny11.iso
|
|
$oscdimgPattern = '(&\s*"\$OSCDIMG"[^`r`n]*tiny11\.iso[^`r`n]*)'
|
|
$oscdimgReplacement = @"
|
|
`$1
|
|
if (`$LASTEXITCODE -ne 0 -and `$LASTEXITCODE -ne `$null) {
|
|
Write-Error "oscdimg failed with exit code: `$LASTEXITCODE"
|
|
exit 1
|
|
}
|
|
|
|
# Verify ISO file was created
|
|
`$expectedIsoPath = "`$PSScriptRoot\tiny11.iso"
|
|
Start-Sleep -Seconds 2
|
|
if (-not (Test-Path `$expectedIsoPath)) {
|
|
Write-Error "ISO file was not created at: `$expectedIsoPath"
|
|
Write-Host "Checking for ISO in alternative locations..."
|
|
Get-ChildItem -Path `$PSScriptRoot -Filter "*.iso" -ErrorAction SilentlyContinue | ForEach-Object {
|
|
Write-Host "Found ISO: `$(`$_.FullName)"
|
|
}
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "ISO file verified: `$expectedIsoPath"
|
|
Write-Host "ISO size: `$([math]::Round((Get-Item `$expectedIsoPath).Length / 1GB, 2)) GB"
|
|
"@
|
|
$scriptContent = $scriptContent -replace $oscdimgPattern, $oscdimgReplacement
|
|
|
|
# Create a temporary script with auto-answers for image index
|
|
$tempScriptHeader = @"
|
|
`$ErrorActionPreference = 'Continue' # Continue on errors to see full output
|
|
|
|
# Set ISO parameter if not already set
|
|
if (-not `$ISO) {
|
|
`$ISO = '$ISODriveLetter'
|
|
}
|
|
if (-not `$SCRATCH -and '$ScratchDrive' -ne '') {
|
|
`$SCRATCH = '$ScratchDrive'
|
|
}
|
|
|
|
# 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 drive letter prompt (only if ISO parameter not provided)
|
|
if (`$Prompt -match "enter the drive letter") {
|
|
Write-Host "Auto-answering: $ISODriveLetter"
|
|
return "$ISODriveLetter"
|
|
}
|
|
|
|
# Auto-answer image index prompt - get first available index
|
|
if (`$Prompt -match "enter the image index") {
|
|
Write-Host "Auto-detecting image index..."
|
|
# Try to get the first available index
|
|
`$index = "1"
|
|
Write-Host "Auto-answering: `$index"
|
|
return `$index
|
|
}
|
|
|
|
# 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 "tiny11maker-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 "Using wrapper approach to handle all prompts automatically"
|
|
Write-Host "Running script: $tempScriptPath"
|
|
|
|
# Run the wrapper script which has Read-Host override
|
|
& $tempScriptPath
|
|
$scriptExitCode = $LASTEXITCODE
|
|
|
|
# Check if script failed (PowerShell sets $LASTEXITCODE for native commands)
|
|
# If script exited normally, $LASTEXITCODE might be null or 0
|
|
if ($scriptExitCode -ne 0 -and $scriptExitCode -ne $null) {
|
|
Write-Error "Script exited with code: $scriptExitCode"
|
|
exit $scriptExitCode
|
|
}
|
|
|
|
# Verify ISO was actually created (double-check)
|
|
$expectedIsoPath = Join-Path $scriptRoot "tiny11.iso"
|
|
if (-not (Test-Path $expectedIsoPath)) {
|
|
Write-Error "Script completed but ISO file not found at: $expectedIsoPath"
|
|
Write-Host "Searching for ISO files..."
|
|
Get-ChildItem -Path $scriptRoot -Filter "*.iso" -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
|
|
Write-Host "Found: $($_.FullName)"
|
|
}
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "Build completed successfully. ISO found at: $expectedIsoPath"
|
|
} 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
|
|
}
|
|
|