Jimi2000

WindowsActivationStatus

Nov 7th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1.  
  2. //Call the method like this:
  3. ActivationStatus OSLicenseStatus = WindowsActivationStatus()
  4.  
  5.  
  6.     public static ActivationStatus WindowsActivationStatus()
  7.     {
  8.         ActivationStatus activation = new ActivationStatus();
  9.         ConnectionOptions ConnOptions = GetConnectionOptions();
  10.         SelectQuery mQuery = new SelectQuery("SELECT * FROM SoftwareLicensingProduct WHERE LicenseStatus = 1");
  11.         EnumerationOptions mOptions = GetEnumerationOptions(false);
  12.         ManagementScope mScope = new ManagementScope(@"\\" + Environment.MachineName + @"\root\CIMV2", ConnOptions);
  13.         mScope.Connect();
  14.  
  15.         using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(mScope, mQuery, mOptions))
  16.         {
  17.             foreach (ManagementObject moMonitor in moSearcher.Get())
  18.             {
  19.                 activation.ApplicationID = moMonitor["ApplicationID"]?.ToString();
  20.                 activation.Description = moMonitor["Description"]?.ToString();
  21.                 activation.ID = moMonitor["ID"]?.ToString();
  22.                 activation.LicenseFamily = moMonitor["LicenseFamily"]?.ToString();
  23.                 activation.LicenseStatus = ((ActivationStatus.LicenseStatusValue)moMonitor["LicenseStatus"]).ToString();
  24.                 activation.OSName = moMonitor["Name"]?.ToString();
  25.                 activation.OfflineInstallationId = moMonitor["OfflineInstallationId"]?.ToString();
  26.                 activation.PartialProductKey = moMonitor["PartialProductKey"]?.ToString();
  27.                 activation.ProductKeyID = moMonitor["ProductKeyID"]?.ToString();
  28.                 activation.VolumeLicensetivationInterval = moMonitor["VLActivationInterval"]?.ToString();
  29.             }
  30.         }
  31.         return activation;
  32.     }
  33.  
  34.     public class ActivationStatus
  35.     {
  36.         public enum LicenseStatusValue : UInt32
  37.         {
  38.             Unlicensed = 0,
  39.             Licensed,
  40.             OutOfBoxGrace,
  41.             OutOfToleanceGrace,
  42.             NonGenuineGrace,
  43.             Notification
  44.         }
  45.  
  46.         public string ApplicationID { get; set; }
  47.         public string Description { get; set; }
  48.         public DateTime EvaluationEndDate { get; set; }
  49.         public string ID { get; set; }
  50.         public string LicenseFamily { get; set; }
  51.         public string LicenseStatus { get; set; }
  52.         public string OSName { get; set; }
  53.         public string OfflineInstallationId { get; set; }
  54.         public string PartialProductKey { get; set; }
  55.         public string ProductKeyID { get; set; }
  56.         public string VolumeLicensetivationInterval { get; set; }
  57.     }
  58.  
  59.     private static EnumerationOptions GetEnumerationOptions(bool DeepScan)
  60.     {
  61.         EnumerationOptions mOptions = new EnumerationOptions()
  62.         {
  63.             Rewindable = false,        //Forward only query => no caching
  64.             ReturnImmediately = true,  //Pseudo-async result
  65.             DirectRead = true,
  66.             EnumerateDeep = DeepScan
  67.         };
  68.         return mOptions;
  69.     }
  70.  
  71.     private static ConnectionOptions GetConnectionOptions()
  72.     {
  73.         ConnectionOptions ConnOptions = new ConnectionOptions()
  74.         {
  75.             EnablePrivileges = true,
  76.             Timeout = ManagementOptions.InfiniteTimeout,
  77.             Authentication = AuthenticationLevel.PacketPrivacy,
  78.             Impersonation = ImpersonationLevel.Impersonate,
  79.         };
  80.         return ConnOptions;
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment