if ($success) Write-Log "SUCCESS" "USBDK installation completed successfully" exit 0
private const string USBDK_DOWNLOAD_URL = "https://github.com/daynix/UsbDk/releases/download/v1.0.22/UsbDk_1.0.22_x64.msi"; private const string INSTALLER_FILENAME = "UsbDk_1.0.22_x64.msi"; private readonly string _downloadPath; private readonly ILogger _logger;
return $false function Install-USBDK Write-Log "INFO" "Installing USBDK..."
// Step 2: Check if already installed if (!forceReinstall && IsUSBDKInstalled()) _logger.LogInfo("USBDK is already installed"); return true; 2-download and install usbdk-1.0.22-x64.msi
public void LogSuccess(string message) Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"[SUCCESS] DateTime.Now:HH:mm:ss - message"); Console.ResetColor();
_logger.LogSuccess("USBDK installed successfully!"); return true; catch (Exception ex) _logger.LogError($"Installation failed: ex.Message"); return false;
function Install-USBDKFeature Write-Log "INFO" "Starting USBDK installation process" private readonly string _downloadPath
private async Task<bool> DownloadInstallerAsync() try using (var httpClient = new HttpClient()) httpClient.Timeout = TimeSpan.FromMinutes(5); using (var response = await httpClient.GetAsync(USBDK_DOWNLOAD_URL, HttpCompletionOption.ResponseHeadersRead)) response.EnsureSuccessStatusCode(); using (var fileStream = new FileStream(_downloadPath, FileMode.Create, FileAccess.Write, FileShare.None)) await response.Content.CopyToAsync(fileStream);
C# Usage:
using (Process process = Process.Start(psi)) if (process == null) throw new InvalidOperationException("Failed to start msiexec"); if (!process.WaitForExit(300000)) // 5 minute timeout process.Kill(); throw new TimeoutException("Installation timed out"); return process.ExitCode == 0; catch (Exception ex) _logger.LogError($"Installation error: ex.Message"); return false; private readonly ILogger _logger
public USBDKInstallerFeature(ILogger logger = null) _downloadPath = Path.Combine(Path.GetTempPath(), INSTALLER_FILENAME); _logger = logger ?? new ConsoleLogger();
catch Write-Log "ERROR" "Download failed: $($_.Exception.Message)" return $false
public class USBDKInstallerFeature