-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
115 lines (100 loc) · 3.57 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Microsoft.Deployment.WindowsInstaller;
namespace TotalUninstaller
{
class Program
{
static void Main(string[] args)
{
try
{
var command = Args.Configuration.Configure<CommandObject>().CreateAndBind(args);
// Get the installed MSI Products
IEnumerable<ProductInstallation> installations = ProductInstallation.GetProducts(null, "s-1-1-0", UserContexts.All);
if (!command.ListAll && !command.Filter && !command.Uninstall)
{
printCommands();
}
if (command.ListAll)
{
// Loop through the installed MSI Products and output information
foreach (ProductInstallation installation in installations)
{
Console.WriteLine("Name: " + installation.ProductName);
Console.WriteLine("Product Code: " + installation.ProductCode);
Console.WriteLine();
}
}
else if (command.Filter)
{
IEnumerable<string> productNameSubstrings = GetListOfProductNameSubstringsToUninstallFromConfig();
foreach (ProductInstallation installation in installations)
{
if (string.IsNullOrEmpty(installation.ProductName)) continue;
if (productNameSubstrings.Any(installation.ProductName.Contains))
{
Console.WriteLine("Name: " + installation.ProductName);
Console.WriteLine("Product Code: " + installation.ProductCode);
Console.WriteLine();
}
}
}
else if (command.Uninstall)
{
IEnumerable<string> productNameSubstrings = GetListOfProductNameSubstringsToUninstallFromConfig();
List<ProductInstallation> productsToUninstall = new List<ProductInstallation>();
foreach (ProductInstallation installation in installations)
{
if (string.IsNullOrEmpty(installation.ProductName)) continue;
if (productNameSubstrings.Any(installation.ProductName.Contains))
{
productsToUninstall.Add(installation);
Console.WriteLine("Name: " + installation.ProductName);
Console.WriteLine("Product Code: " + installation.ProductCode);
Console.WriteLine();
}
}
foreach (ProductInstallation productInstallation in productsToUninstall)
{
Console.WriteLine("Uninstalling " + productInstallation.ProductName);
Console.WriteLine();
Uninstall(productInstallation.ProductCode);
}
}
}
catch (Exception)
{
printCommands();
}
}
private static void printCommands()
{
Console.WriteLine("USAGE:");
Console.WriteLine("\t/ListAll - show all installed products");
Console.WriteLine("\t/Filter - show all products matching list of filter strings in configuration file");
Console.WriteLine("\t/Uninstall - UNINSTALL all products matching list of filter strings in configuration file");
return;
}
private static IEnumerable<string> GetListOfProductNameSubstringsToUninstallFromConfig()
{
Hashtable section = (Hashtable)ConfigurationManager.GetSection("ProductsToUninstall");
return section.Values.Cast<string>();
}
private static void Uninstall(string productCode)
{
Installer.SetInternalUI(InstallUIOptions.ProgressOnly | InstallUIOptions.SourceResolutionOnly | InstallUIOptions.UacOnly);
Installer.ConfigureProduct(productCode, 0, InstallState.Absent, "IGNOREDEPENDENCIES=\"ALL\"");
}
}
public class CommandObject
{
public bool ListAll;
public bool Filter;
public bool Uninstall;
}
}
/*"REBOOT=\"R\" /l*v uninstall.log"*/