# 1. 初始化與環境設定 $OutputEncoding = [System.Text.Encoding]::UTF8 $Host.UI.RawUI.WindowTitle = "USB維修碟建置程式" # 隱藏硬體錯誤彈窗 (取代原本的 0.reg 邏輯) $windowsKey = "HKLM:\SYSTEM\CurrentControlSet\Control\Windows" Set-ItemProperty -Path $windowsKey -Name "ErrorMode" -Value 2 # 2. 讀取版本資訊 $autorunPath = ".\程式\home\autorun.inf" if (-not (Test-Path $autorunPath)) { Write-Error "找不到安裝檔,請確認檔案是否放在本機內" Exit } $ver = (Get-Content $autorunPath -First 1).Trim() function Show-Header { Clear-Host Write-Output "USB維修碟建置程式:" } # 3. 偵測 DOS 隨身碟 function Find-DosUsb { Write-Output "正在搜尋含有DOS系統的隨身碟..." # 直接撈系統中所有的可移動磁碟,並確認根目錄有 COMMAND.COM $usbDrive = Get-CimInstance -ClassName Win32_Volume | Where-Object { $_.DriveType -eq 2 -and (Test-Path "$($_.DriveLetter)\COMMAND.COM") } | Select-Object -First 1 if ($usbDrive) { return "$($usbDrive.DriveLetter)" } return $null } # 主控制流程 :MainLoop while ($true) { Show-Header $usbTarget = Find-DosUsb if ($usbTarget) { Write-Output "已偵測到 $usbTarget 槽內有Dos系統是否開始進行升級?[Y/N]" $ans = Read-Host ">>" if ($ans -like "y*") { Invoke-Update -UsbTarget $usbTarget break } } else { Write-Host "未找到DOS系統!請確認隨身碟是否已經插入!!" -ForegroundColor Red } # 選單模式 Write-Output "請輸入欲使用的功能" Write-Output "1.執行第一次安裝 (Rufus)" Write-Output "2.再次偵測隨身碟" Write-Output "3.手動輸入安裝槽" Write-Output "4.執行BOOTICE修正MBR及PBR" Write-Output "5.離開" $menu = Read-Host ">>" switch ($menu) { "1" { Start-Process "rufus.exe" -Wait } "2" { continue } "3" { $usbTarget = Read-Host ">>請輸入USB所在的槽 (例如 D:)" Invoke-Update -UsbTarget $usbTarget break } "4" { Start-Process "BOOTICEx64.exe" -Wait } "5" { break MainLoop } } } # 4. 執行更新同步 (呼叫原生強大的 Robocopy) function Invoke-Update($UsbTarget) { Write-Output "!!更新中請勿拔除隨身碟!!" # 差異複製 boot robocopy .\程式\boot "$UsbTarget\boot" /mir /njh /njs # 差異複製 efi robocopy .\程式\efi "$UsbTarget\efi" /mir /njh /njs # 拷貝 AutoEXE xcopy .\程式\home "$UsbTarget\" /y /h /s Write-Host "升級完成!快去試試看你的 USB 吧 :D" -ForegroundColor Green # 恢復登錄檔原樣 Set-ItemProperty -Path $windowsKey -Name "ErrorMode" -Value 0 Pause }