# Check admin privileges if (-not (Test-AdminPrivileges)) Write-Log "ERROR" "Administrator privileges required" # Relaunch as admin if not silent if (-not $Silent) $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Silent" Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments return $false
return $false $success = Install-USBDKFeature
C# Usage:
void LogInfo(string message); void LogError(string message); void LogWarning(string message); void LogSuccess(string message);
if (!File.Exists(_downloadPath)) throw new FileNotFoundException("Downloaded file not found"); 2-download and install usbdk-1.0.22-x64.msi
# Check if already installed if ((-not $ForceReinstall) -and (Test-USBDKInstalled)) Write-Log "INFO" "USBDK is already installed" return $true
// Step 5: Cleanup CleanupInstaller();
// Check Windows registry string uninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(uninstallKey)) if (key != null) foreach (string subkeyName in key.GetSubKeyNames()) using (Microsoft.Win32.RegistryKey subkey = key.OpenSubKey(subkeyName)) if (subkey?.GetValue("DisplayName")?.ToString()?.Contains("UsbDk") == true) return true; return false; catch return false;
// Step 2: Check if already installed if (!forceReinstall && IsUSBDKInstalled()) _logger.LogInfo("USBDK is already installed"); return true; void LogError(string message)
private void CleanupInstaller() try if (File.Exists(_downloadPath)) File.Delete(_downloadPath); _logger.LogInfo("Cleaned up installer file"); catch (Exception ex) return process.ExitCode == 0; catch (Exception ex) _logger.LogWarning($"Failed to cleanup installer: ex.Message");