/// <summary> /// Repairs a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to repair</param> /// <param name="expectedExitCode">Expected exit code</param> /// <param name="otherArguments">Other arguments to pass to msiexe.exe.</param> /// <returns>MSIExec log File</returns> public static string RepairProduct(string sourceFile, MSIExecReturnCode expectedExitCode, params string[] otherArguments) { if (String.IsNullOrEmpty(sourceFile)) { throw new ArgumentException("sourceFile cannot be null or empty"); } string logFile = string.Empty; RunMSIExec(sourceFile, MSIExecMode.Repair, otherArguments, expectedExitCode, out logFile); return(logFile); }
/// <summary> /// Executes MSIExec on a .msi file /// </summary> /// <param name="mode">Mode of execution for MSIExec</param> /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> /// <param name="expectedExitCode">Expected exit code</param> /// <returns>MSIExec exit code</returns> private string RunMSIExec(MSIExecMode mode, string[] otherArguments, MSIExecReturnCode expectedExitCode, bool assertOnError = true) { // Generate the log file name. var logFile = Path.Combine(Path.GetTempPath(), String.Format("{0}_{1}_{2:yyyyMMddhhmmss}_{4}_{3}.log", this.TestGroupName, this.TestName, DateTime.UtcNow, Path.GetFileNameWithoutExtension(this.Package), mode)); var msiexec = new MSIExec { Product = this.Package, ExecutionMode = mode, OtherArguments = null != otherArguments?String.Join(" ", otherArguments) : null, ExpectedExitCode = expectedExitCode, LogFile = logFile, }; msiexec.Run(assertOnError); return(msiexec.LogFile); }
/// <summary> /// Installs a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to install</param> /// <param name="expectedExitCode">Expected exit code</param> /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> /// <returns>MSIExec log File</returns> public static string InstallProduct(string sourceFile, MSIExecReturnCode expectedExitCode, params string[] otherArguments) { if (String.IsNullOrEmpty(sourceFile)) { throw new ArgumentException("sourceFile cannot be null or empty"); } string logFile = string.Empty; MSIExecReturnCode exitCode = RunMSIExec(sourceFile, MSIExecMode.Install, otherArguments, expectedExitCode, out logFile); // Add the product to the list of installed products if ((MSIExecReturnCode.SUCCESS == exitCode || MSIExecReturnCode.ERROR_SUCCESS_REBOOT_INITIATED == exitCode || MSIExecReturnCode.ERROR_SUCCESS_REBOOT_REQUIRED == exitCode) && (! MSIExec.InstalledMSI.Contains(sourceFile))) { MSIExec.InstalledMSI.Add(sourceFile); } return logFile; }
/// <summary> /// Uninstalls a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to uninstall</param> /// <param name="expectedExitCode">Expected exit code</param> /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> /// <returns>MSIExec log File</returns> public static string UninstallProduct(string sourceFile, MSIExecReturnCode expectedExitCode, params string[] otherArguments) { if (String.IsNullOrEmpty(sourceFile)) { throw new ArgumentException("sourceFile cannot be null or empty"); } string logFile = string.Empty; MSIExecReturnCode exitCode = RunMSIExec(sourceFile, MSIExecMode.Uninstall, otherArguments, expectedExitCode, out logFile); // Remove the product form the list of installed products if ((MSIExecReturnCode.SUCCESS == exitCode || MSIExecReturnCode.ERROR_SUCCESS_REBOOT_INITIATED == exitCode || MSIExecReturnCode.ERROR_SUCCESS_REBOOT_REQUIRED == exitCode) && (MSIExec.InstalledMSI.Contains(sourceFile))) { MSIExec.InstalledMSI.Remove(sourceFile); } return(logFile); }
/// <summary> /// Attempt to uninstall all the installed msi's /// </summary> /// <remarks> /// TODO: implement ignore_return_code option /// </remarks> public static void UninstallAllInstalledProducts() { foreach (string sourceFile in MSIExec.InstalledMSI) { // This is a best effort attempt to clean up the machine after a test run. // The loop will attempt to uninstall all the msi files registered to be installed. try { string logFile = string.Empty; MSIExecReturnCode exitCode = MSIExec.RunMSIExec(sourceFile, MSIExecMode.Uninstall, null, MSIExecReturnCode.SUCCESS, out logFile); if (MSIExecReturnCode.SUCCESS != exitCode) { Console.WriteLine(string.Format("Failed to uninstall msi '{0}'. Exit code: '{1}'", sourceFile, exitCode.ToString())); } } catch (Exception ex) { Console.WriteLine(string.Format("Failed to uninstall msi '{0}'. Exception raised: '{1}'", sourceFile, ex.Message)); } } MSIExec.InstalledMSI.Clear(); }
/// <summary> /// Executes MSIExec on a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to use</param> /// <param name="mode">Mode of execution for MSIExec</param> /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> /// <param name="expectedExitCode">Expected exit code</param> /// <returns>MSIExec exit code</returns> private static MSIExecReturnCode RunMSIExec(string sourceFile, MSIExecMode mode, string[] otherArguments, MSIExecReturnCode expectedExitCode, out string logFile) { MSIExec msiexec = new MSIExec(); msiexec.Product = sourceFile; msiexec.ExecutionMode = mode; msiexec.OtherArguments = null != otherArguments ? String.Join(" ", otherArguments) : null; msiexec.ExpectedExitCode = expectedExitCode; Result result = msiexec.Run(); logFile = msiexec.LogFile; return (MSIExecReturnCode)result.ExitCode; }
/// <summary> /// Repairs a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to repair</param> /// <param name="expectedExitCode">Expected exit code</param> /// <param name="otherArguments">Other arguments to pass to msiexe.exe.</param> /// <returns>MSIExec log File</returns> public static string RepairProduct(string sourceFile, MSIExecReturnCode expectedExitCode, params string[] otherArguments) { if (String.IsNullOrEmpty(sourceFile)) { throw new ArgumentException("sourceFile cannot be null or empty"); } string logFile = string.Empty; RunMSIExec(sourceFile, MSIExecMode.Repair, otherArguments, expectedExitCode, out logFile); return logFile; }
/// <summary> /// Executes MSIExec on a .msi file /// </summary> /// <param name="sourceFile">Path the .msi file to use</param> /// <param name="mode">Mode of execution for MSIExec</param> /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> /// <param name="expectedExitCode">Expected exit code</param> /// <returns>MSIExec exit code</returns> private static MSIExecReturnCode RunMSIExec(string sourceFile, MSIExecMode mode, string[] otherArguments, MSIExecReturnCode expectedExitCode, out string logFile) { MSIExec msiexec = new MSIExec(); msiexec.Product = sourceFile; msiexec.ExecutionMode = mode; msiexec.OtherArguments = null != otherArguments?String.Join(" ", otherArguments) : null; msiexec.ExpectedExitCode = expectedExitCode; Result result = msiexec.Run(); logFile = msiexec.LogFile; return((MSIExecReturnCode)result.ExitCode); }
/// <summary> /// Repairs a .msi file /// </summary> /// <param name="expectedExitCode">Expected exit code</param> /// <param name="otherArguments">Other arguments to pass to msiexe.exe.</param> /// <returns>MSIExec log File</returns> public string RepairProduct(MSIExecReturnCode expectedExitCode = MSIExecReturnCode.SUCCESS, params string[] otherArguments) { return(this.RunMSIExec(MSIExecMode.Repair, otherArguments, expectedExitCode)); }