91 lines
3.1 KiB
PowerShell
91 lines
3.1 KiB
PowerShell
# Auto-elevate to administrator
|
|
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
$maxRetries = 5
|
|
$retryCount = 0
|
|
$launched = $false
|
|
|
|
while ($retryCount -lt $maxRetries -and -not $launched) {
|
|
$retryCount++
|
|
try {
|
|
Write-Host "[Elevate $retryCount/$maxRetries] Requesting admin privileges..." -ForegroundColor Cyan
|
|
$proc = Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs -Wait -PassThru -ErrorAction Stop
|
|
if ($null -ne $proc) {
|
|
$launched = $true
|
|
}
|
|
}
|
|
catch {
|
|
Write-Host "Elevation failed: user cancelled or permission error" -ForegroundColor Yellow
|
|
if ($retryCount -ge $maxRetries) {
|
|
Write-Host "Max retries ($maxRetries) reached, exiting." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
Start-Sleep -Milliseconds 300
|
|
}
|
|
}
|
|
|
|
if ($launched) {
|
|
exit 0
|
|
}
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "Running as administrator!" -ForegroundColor Green
|
|
|
|
# ==========================================================
|
|
# ImDisk-based FAT32 disk image builder
|
|
# ==========================================================
|
|
|
|
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition
|
|
$PROJECT_DIR = Split-Path -Parent $SCRIPT_DIR
|
|
$DISK_IMG = Join-Path $PROJECT_DIR "disk.img"
|
|
$DISK_DIR = Join-Path $PROJECT_DIR "disk"
|
|
$MOUNT_LETTER = "Z:"
|
|
|
|
$IMAGE_SIZE = 64 * 1024 * 1024
|
|
|
|
Write-Host "=== Creating disk image ===" -ForegroundColor Cyan
|
|
|
|
if (-not (Get-Command "imdisk.exe" -ErrorAction SilentlyContinue)) {
|
|
Write-Host "Error: Cannot find imdisk.exe. Please ensure ImDisk is installed and added to system PATH." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
if (-not (Test-Path $DISK_DIR)) {
|
|
Write-Host "Error: Source directory not found $DISK_DIR" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
if (Test-Path $DISK_IMG) {
|
|
Write-Host "Removing existing disk image..."
|
|
Remove-Item -Force $DISK_IMG
|
|
}
|
|
|
|
Write-Host "Creating $IMAGE_SIZE byte disk image..."
|
|
$fsutilFile = [System.IO.File]::Create($DISK_IMG)
|
|
$fsutilFile.SetLength($IMAGE_SIZE)
|
|
$fsutilFile.Close()
|
|
|
|
if (Test-Path $MOUNT_LETTER) {
|
|
Write-Host "Warning: $MOUNT_LETTER already exists. Attempting to detach..." -ForegroundColor Yellow
|
|
imdisk -D -m $MOUNT_LETTER
|
|
Start-Sleep -Seconds 1
|
|
}
|
|
|
|
Write-Host "Mounting and formatting disk image to $MOUNT_LETTER ..."
|
|
imdisk -a -f $DISK_IMG -m $MOUNT_LETTER -o hd --% -p "/fs:fat32 /q /y"
|
|
|
|
Start-Sleep -Seconds 2
|
|
|
|
Write-Host "Copying files to disk image..."
|
|
robocopy $DISK_DIR "$MOUNT_LETTER\" /E /NJH /NJS /np
|
|
if ($LASTEXITCODE -le 7) {
|
|
Write-Host "Files copied successfully." -ForegroundColor Green
|
|
} else {
|
|
Write-Host "Warning: File copy encountered an error. Robocopy exit code: $LASTEXITCODE" -ForegroundColor Yellow
|
|
}
|
|
|
|
Write-Host "Unmounting disk image..."
|
|
imdisk -D -m $MOUNT_LETTER
|
|
|
|
Write-Host "Disk image created: $DISK_IMG" -ForegroundColor Green
|