itsfatduck/optimizerDuck · error · StepExecutionException
Revert.UsbPower.Error.CommandFailed (stderr if present…
Error message
Revert.UsbPower.Error.CommandFailed (stderr if present, else localized message with exit code)
What it means
UsbPowerRevertStep.ExecuteAsync throws StepExecutionException when the command that restores USB power settings (per-device DeviceState enable/disable) exits non-zero. Like the shell step, stderr wins over the localized 'Revert.UsbPower.Error.CommandFailed' message with the exit code, and stderr is stored as the detail.
Solutions
- Read the attached stderr for the specific failure and fix it (replug the device, install the driver).
- Run the app as administrator — USB power configuration typically requires elevation.
- Re-apply the USB power optimization to regenerate fresh revert data with current device instance paths, then revert.
- If the device is permanently gone, delete the revert file for that optimization id.
Defensive patterns
Strategy: try-catch
Validate before calling
// device present check before reverting USB power state
bool present = new ManagementObjectSearcher("SELECT InstanceName FROM Win32_USBHub")
.Get().Cast<ManagementObject>().Any(m => instanceName.Contains(m["InstanceName"].ToString())); Try / catch
try { await usbStep.ExecuteAsync(opCall); }
catch (StepExecutionException ex)
{ logger.LogError("USB power revert failed: {Err}", ex.Detail ?? ex.Message); } Prevention
- Keep the device connected during revert
- Re-apply to refresh DeviceState after driver updates
- Run elevated for powercfg changes
- Treat stale instance paths as expected failures and regenerate revert data
When it happens
Trigger: The PowerShell/CMD restoration command for USB selective suspend / device power settings failed: target device unplugged so its instance path is gone, driver reinstalled with a new instance name, or powercfg invocation blocked (rights or policy).
Common situations: USB device removed before revert; device instance renamed after driver update; power plan recreated so the stored settings reference is stale; running unelevated where powercfg changes require admin.
Related errors
- Revert.Shell.Error.CommandFailed (stderr if present, else…
- Scheduled task verify failed at
- Scheduled task verify failed at
- Missing required 'OriginalEnabled' in scheduled-task revert…
- result.Error ?? Description
AI-assisted analysis of itsfatduck/optimizerDuck@36acf585ae (2026-09-13).
Data as JSON: /api/errors/3e4724d44d6e3584.
Report an issue: GitHub.
Appendix: source
Thrown at optimizerDuck/Domain/Revert/Steps/UsbPowerRevertStep.cs:74
"$json = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('"
+ payload
+ "')); "
+ "$states = $json | ConvertFrom-Json; "
+ "foreach ($s in $states) { "
+ "$obj = Get-CimInstance -Namespace root\\wmi -ClassName MSPower_DeviceEnable -ErrorAction SilentlyContinue | "
+ "Where-Object { $_.InstanceName -eq $s.InstanceName }; "
+ "if ($obj -and $obj.Enable -ne [bool]$s.Enable) { "
+ "Set-CimInstance -CimInstance $obj -Property @{ Enable = [bool]$s.Enable } | Out-Null "
+ "}}";
var result = await shell.QueryPowerShellAsync(script, logger).ConfigureAwait(false);
if (result.ExitCode != 0)
{
var error = !string.IsNullOrWhiteSpace(result.Stderr)
? result.Stderr
: Loc.Instance["Revert.UsbPower.Error.CommandFailed", result.ExitCode];
throw new StepExecutionException(error, result.Stderr);
}
return true;
}
/// <inheritdoc />
public JObject ToData()
{
return new JObject { [nameof(States)] = JArray.FromObject(States) };
}
/// <summary>
/// Deserializes a <see cref="UsbPowerRevertStep" /> from JSON data.
/// </summary>
public static UsbPowerRevertStep FromData(JObject data)
{
var states = data[nameof(States)]?.ToObject<List<DeviceState>>() ?? [];
View on GitHub (pinned to 36acf585ae)