BornToBeRoot/NETworkManager · error · Exception

string.Join("; ", ps.Streams.Error.Select(e =>…

Error message

string.Join("; ", ps.Streams.Error.Select(e => e.ToString()))

What it means

RunCommands executes a PowerShell script built by callers (ConfigureNetworkInterface, FlushDns, ReleaseRenew, Add/RemoveIPAddressToNetworkInterface) and, when checkExitCode is set and ps.HadErrors is true, throws a generic Exception whose message is the ToString() of every error record joined with "; ". The message therefore contains cmdlet-level errors (invalid parameters, missing elevation, cmdlet not found).

Solutions

  1. Run the application as administrator before changing network interface settings.
  2. Refresh/validate the target interface name and state (connected, not renamed) before invoking the operation.
  3. Catch the exception and display the joined error records, which identify the failing cmdlet and parameter.
  4. Check ps.HadErrors/errors per command and fall back to querying Get-NetAdapter to diagnose.

Example fix

// before
await NetworkInterface.FlushDns(); // throws with joined PS errors if not elevated
// after
try
{
    await NetworkInterface.FlushDns();
}
catch (Exception ex)
{
    Log.Warn($"Flush DNS failed: {ex.Message}");
    MessageBox.Show("This operation requires administrator privileges.");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-checks before network interface operations
var target = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
    .FirstOrDefault(n => n.Id == adapterId || n.Name == adapterName);
bool isAdmin = new System.Security.Principal.WindowsPrincipal(
    System.Security.Principal.WindowsIdentity.GetCurrent())
    .IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
if (!isAdmin) throw new InvalidOperationException("Network configuration requires administrator privileges");

Type guard

static bool IsElevated() => new System.Security.Principal.WindowsPrincipal(
    System.Security.Principal.WindowsIdentity.GetCurrent())
    .IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);

Try / catch

try
{
    await NetworkInterface.FlushDns();
}
catch (Exception ex)
{
    // message contains each PS error record joined with "; "
    foreach (var part in ex.Message.Split("; "))
        Log.Warn($"PowerShell: {part}");
    throw new InvalidOperationException("Network operation failed (requires elevation?).", ex);
}

Prevention

When it happens

Trigger: Calling any wrapped network configuration operation while the generated script errors: renaming/configuring a NIC with invalid values (Enable-NetAdapter / Set-NetIPInterface / New-NetIPAddress failures), flushing DNS without rights, or ipconfig release/renew when no adapter matches.

Common situations: App not elevated (most NetAdapter/NetIPAddress cmdlets need admin); adapter name/alias changed or disconnected; conflicting static IP already assigned; media disconnected during release/renew.

Related errors


AI-assisted analysis of BornToBeRoot/NETworkManager@2780d65469 (2026-09-12). Data as JSON: /api/errors/551b54f67212a715. Report an issue: GitHub.

Appendix: source

Thrown at Source/NETworkManager.Models/Network/NetworkInterface.cs:575

    /// Use <see langword="false"/> for <c>netsh</c>, whose exit codes are unreliable
    /// for idempotent operations (e.g. returns 1 when already on DHCP).
    /// </summary>
    private static void RunCommands(IEnumerable<string> commands, bool checkExitCode = false)
    {
        string script;

        if (checkExitCode)
            script = string.Join(Environment.NewLine, commands.Select(cmd =>
                $"{cmd}{Environment.NewLine}if ($LASTEXITCODE -ne 0) {{ Write-Error \"Command failed with exit code $LASTEXITCODE\" }}"));
        else
            script = string.Join(Environment.NewLine, commands);

        using var ps = SMA.PowerShell.Create();
        ps.AddScript(script);
        ps.Invoke();

        if (checkExitCode && ps.HadErrors)
            throw new Exception(string.Join("; ", ps.Streams.Error.Select(e => e.ToString())));
    }

    #endregion

}

View on GitHub (pinned to 2780d65469)