Advertisement
Guest User

List Installed MSI

a guest
Sep 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Microsoft.Deployment.WindowsInstaller;
  7. using Microsoft.Deployment.WindowsInstaller.Linq;
  8. using Microsoft.Deployment.WindowsInstaller.Linq.Entities;
  9. using Microsoft.Deployment.WindowsInstaller.Package;
  10.  
  11. namespace MSI_Mgmt
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             Console.WriteLine("List Installed Applications\n");
  18.             IOrderedEnumerable<ProductInstallation> ap = ProductInstallation.AllProducts.OrderBy(pi => pi.Publisher)
  19.                 .ThenBy(pi => pi.ProductName)
  20.                 .ThenBy(pi => pi.Features.Where(f => f.FeatureName == "ProductFeature").Select(f => f.FeatureName).SingleOrDefault())
  21.                 .ThenBy(pi => pi.ProductVersion);
  22.             foreach (ProductInstallation pi in ap.ToArray())
  23.             {
  24.                 if (pi.Publisher == "Microsoft Corporation") continue;
  25.                 if (pi.Publisher == "Microsoft Corporations") continue;
  26.                 if (pi.Publisher == "Microsoft") continue;
  27.                 if (pi.Publisher == "Microsoft Garage") continue;
  28.                 if (pi.Publisher == ".NET Foundation") continue;
  29.                 if (pi.Publisher == "Adobe Systems Incorporated") continue;
  30.                 if (pi.Publisher == "Adobe Systems, Inc") continue;
  31.                 if (pi.Publisher == "Apple Inc.") continue;
  32.                 if (pi.Publisher == "CyberLink Corp.") continue;
  33.                 if (pi.Publisher == "Dell Products, LP") continue;
  34.                 if (pi.Publisher == "Dell") continue;
  35.                 if (pi.Publisher == "Dell Inc.") continue;
  36.                 if (pi.Publisher == "Intel") continue;
  37.                 if (pi.Publisher == "INTEL") continue;
  38.                 if (pi.Publisher == "Intel Corporation") continue;
  39.                 if (pi.Publisher == "Google") continue;
  40.                 if (pi.Publisher == "Google Inc") continue;
  41.                 if (pi.Publisher == "Google LLC") continue;
  42.                 if (pi.Publisher == "Google, Inc.") continue;
  43.                 if (pi.Publisher == "Oracle Corporation") continue;
  44.                 if (pi.Publisher == "Plex") continue;
  45.                 if (pi.Publisher == "Plex, Inc.") continue;
  46.                 if (pi.Publisher == "Python Software Foundation") continue;
  47.                 if (pi.Publisher == "MariaDB Corporation Ab") continue;
  48.                 if (pi.Publisher == "The Document Foundation") continue;
  49.  
  50.                 if (pi.Publisher == "LULU Software Limited") continue;
  51.                 if (pi.Publisher == "pdfforge GmbH") continue;
  52.                 if (String.IsNullOrEmpty(pi.Publisher) && String.IsNullOrEmpty(pi.ProductName) && pi.ProductVersion == null) continue;
  53.                 foreach(FeatureInstallation fi in pi.Features) {
  54.                     Console.WriteLine("{0} = {1} - ({2})", fi.FeatureName, fi.State.ToString(), fi.Usage.UseCount);
  55.                 }
  56.                 //break;
  57.                 Session s = null;
  58.                 String f = "";
  59.                 String u = "";
  60.                 if (pi.LocalPackage != null && pi.LocalPackage.Length>0)
  61.                 {
  62.                     try
  63.                     {
  64.                         s = Installer.OpenPackage(pi.LocalPackage, true);
  65.                     }
  66.                     catch(InstallerException e)
  67.                     {
  68.                         String p = pi.LocalPackage.ToLower().Replace("c:\\windows\\installer\\", "D:\\Installer-Backup\\");
  69.                         s = Installer.OpenPackage(p, true);
  70.                     }
  71.                     u = s.GetProductProperty("UpgradeCode");
  72.                     try
  73.                     {
  74.                         f = s.Features.Where(fs => fs.Name == "ProductFeature").Single().Title;
  75.                     }
  76.                     catch(Exception e)
  77.                     {
  78.                         f = "";
  79.                     }
  80.                     foreach (FeatureInfo fi in s.Features)
  81.                     {
  82.                         Console.WriteLine("@ {0} = {1} ({2} - {3}) - {4} - {5}", fi.Name, fi.Title, fi.CurrentState, fi.RequestState, fi.Attributes.ToString(), fi.Description);
  83.                     }
  84.                     s.Close();
  85.                 }
  86.                 try
  87.                 {
  88.                     if(String.IsNullOrEmpty(f)) f = pi.Features.Where(fs => fs.FeatureName == "ProductFeature").Single().GetHashCode().ToString();
  89.                 }
  90.                 catch(Exception e)
  91.                 {
  92.                     f = "";
  93.                 }
  94.                 Console.WriteLine("{0} - {1} - {2} - {3} - {4} ({5})", pi.Publisher, pi.ProductName, f, pi.AdvertisedLanguage, pi.ProductVersion, pi.IsInstalled);
  95.                 Console.WriteLine("{0} {1} {2}", pi.ProductCode, pi.AdvertisedPackageCode, u);
  96.                 Console.ReadLine();
  97.             }
  98.             //Thread.Sleep(10000);
  99.             Console.ReadLine();
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement