public static int RunProcessSynchronouslyWithConsoleOutput(string name, string args) { using (var process = CreateProcessInstance(name, args)) { process.OutputDataReceived += (_, evt) => { if (!string.IsNullOrEmpty(evt.Data)) { Console.WriteLine(evt.Data); } }; process.ErrorDataReceived += (_, evt) => { if (!string.IsNullOrEmpty(evt.Data)) { ConsoleUtils.WriteLine(evt.Data, ConsoleColor.Red); } }; process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); return(process.ExitCode); } }
public static void TryDeleteDirectoryIfExists(string path) { try { DeleteDirectoryIfExists(path); } catch (Exception exc) { ConsoleUtils.WriteLine($"An error occurred when deleting folder {path}: {exc.Message}", ConsoleColor.Red); } }
/** * Removes the specified component using install-wim-tweak synchronously * Messages from install-wim-tweak process are printed asynchronously (as soon as they are written to stdout/stderr) */ public static void RemoveComponentUsingInstallWimTweakIfAllowed(string component) { if (!Configuration.Instance.AllowInstallWimTweak) { ConsoleUtils.WriteLine($"Skipped removal of component {component} using install-wim-tweak since " + @"option ""AllowInstallWimTweak"" is set to false.", ConsoleColor.DarkYellow); return; } Console.WriteLine($"Running install-wim-tweak to remove {component}..."); int installWimTweakExitCode = SystemUtils.RunProcessSynchronouslyWithConsoleOutput(Program.InstallWimTweakPath, $"/o /c {component} /r"); if (installWimTweakExitCode == 0) { Console.WriteLine("Install-wim-tweak executed successfully!"); } else { ConsoleUtils.WriteLine($"An error occurred during the removal of {component}: " + "install-wim-tweak exited with a non-zero status.", ConsoleColor.Red); } }
private static void PrintWarningString(object sender, DataAddedEventArgs eventArgs) { var powerShellStream = (PSDataCollection <WarningRecord>)sender; ConsoleUtils.WriteLine(powerShellStream[eventArgs.Index].ToString(), ConsoleColor.DarkYellow); }
private static void PrintErrorString(object sender, DataAddedEventArgs eventArgs) { var powerShellStream = (PSDataCollection <ErrorRecord>)sender; ConsoleUtils.WriteLine(powerShellStream[eventArgs.Index].ToString(), ConsoleColor.Red); }