예제 #1
0
        public static List <RemoteUpdate> GetInstalledUpdates()
        {
            // Use WMI to retrieve a list of installed Microsoft updates.
            var updates    = new List <RemoteUpdate>();
            var taskResult = new TaskResult();

            Result = taskResult;
            const string microsoftKbUrl = "http://support.microsoft.com/?kbid=";

            // Setup WMI query.
            var options = new ConnectionOptions();

            if (GlobalVar.UseAlternateCredentials)
            {
                options.Username  = GlobalVar.AlternateUsername;
                options.Password  = GlobalVar.AlternatePassword;
                options.Authority = $"NTLMDOMAIN:{GlobalVar.AlternateDomain}";
            }
            var scope    = new ManagementScope($@"\\{ComputerName}\root\CIMV2", options);
            var query    = new ObjectQuery("SELECT HotFixID,InstalledOn FROM Win32_QuickFixEngineering WHERE HotFixID <> 'File 1'");
            var searcher = new ManagementObjectSearcher(scope, query);

            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    var update = new RemoteUpdate();

                    update.UpdateId = (m["HotFixID"] != null) ? m["HotFixID"].ToString() : string.Empty;
                    if (m["InstalledOn"] != null)
                    {
                        DateTime installDate;
                        if (DateTime.TryParse(m["InstalledOn"].ToString(), out installDate))
                        {
                            update.InstallDate = installDate;
                        }
                    }
                    update.ExternalLink = (ExtractKbNumber(update.UpdateId).Length > 0) ? microsoftKbUrl + ExtractKbNumber(update.UpdateId) : string.Empty;

                    if (update.UpdateId.Length > 0)
                    {
                        updates.Add(update);
                    }
                }
                taskResult.DidTaskSucceed = true;
            }
            catch
            {
                taskResult.DidTaskSucceed = false;
            }

            return(updates);
        }
예제 #2
0
        public static DialogResult UninstallUpdate(RemoteUpdate update)
        {
            // UninstallUpdate() uses WMI to execute the Microsoft update uninstaller.
            // It returns a DialogResult which will be used to display the results.
            var  dialog         = new DialogResult();
            bool didTaskSucceed = false;

            string commandLine = $"wusa /uninstall /kb:{ExtractKbNumber(update.UpdateId)} /quiet /norestart";

            // Setup WMI query.
            var options = new ConnectionOptions();

            if (GlobalVar.UseAlternateCredentials)
            {
                options.Username  = GlobalVar.AlternateUsername;
                options.Password  = GlobalVar.AlternatePassword;
                options.Authority = $"NTLMDOMAIN:{GlobalVar.AlternateDomain}";
            }
            var scope = new ManagementScope($@"\\{ComputerName}\root\CIMV2", options);

            // Connect to WMI and invoke the process creation method.
            try
            {
                scope.Connect();
                var objectGetOptions = new ObjectGetOptions();
                var managementPath   = new ManagementPath("Win32_Process");
                var managementClass  = new ManagementClass(scope, managementPath, objectGetOptions);

                ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
                inParams["CommandLine"] = commandLine;
                ManagementBaseObject outParams = managementClass.InvokeMethod("Create", inParams, null);

                int  returnValue;
                bool isReturnValid = int.TryParse(outParams["ReturnValue"].ToString(), out returnValue);
                if (!isReturnValid || returnValue != 0)
                {
                    throw new Exception();
                }

                didTaskSucceed = true;
            }

            catch
            { }

            if (didTaskSucceed)
            {
                // Process terminated.  Build DialogResult to reflect success.
                dialog.DialogTitle = "Success";
                dialog.DialogBody  = $"The target computer is processing the request to uninstall Microsoft update {update.UpdateId}." +
                                     $"{Environment.NewLine}{Environment.NewLine}" +
                                     "It might take several minutes to complete the uninstallation and the computer might need to be rebooted when finished.";
                dialog.DialogIconPath  = "/Resources/success-48.png";
                dialog.ButtonIconPath  = "/Resources/checkmark-24.png";
                dialog.ButtonText      = "OK";
                dialog.IsCancelVisible = false;
            }
            else
            {
                // Error terminating process.  Build DialogResult to reflect failure.
                dialog.DialogTitle     = "Error";
                dialog.DialogBody      = $"Failed to uninstall Microsoft update {update.UpdateId}.";
                dialog.DialogIconPath  = "/Resources/error-48.png";
                dialog.ButtonIconPath  = "/Resources/checkmark-24.png";
                dialog.ButtonText      = "OK";
                dialog.IsCancelVisible = false;
            }

            return(dialog);
        }