1

We have a server running a 6 day job that just reset, because Windows Update Agent (WUA) automatically rebooted the machine to install updates.

I would like to alter my C# app to warn the user if this setting is on in the future, so we don't end up suffering from random reboots, which forces a restart of said 6-day job, wasting 4 days of CPU time.

Does any know if it is possible to check within WUA that "Important Updates" is set to "Install updates automatically (recommended)", within C#?

Contango
  • 76,540
  • 58
  • 260
  • 305
  • Would it not be better to engineer your system to cope with this (e.g. save incremental state)? Things *other* than WUA could cause the system to restart at *any* time. – Damien_The_Unbeliever Mar 07 '12 at 11:50
  • 1
    Just set the policy for the server to have updates be manually installed. I also agree that you should figure out a way to avoid 6 day task. – Security Hound Mar 07 '12 at 12:31
  • @Damien_The_Unbeliever Figuring a way to save the state, and restart the 6-day batch process would be a monumental engineering effort which wouldn't really be worth it for the time saved. As this utility is only run once every quarter (if at all), it makes far more sense to add a couple of lines of code to warn the user of WUA issues, and leave it at that. – Contango Mar 08 '12 at 12:56
  • Yes, but I was pointing out that WUA is *one* possible source of random reboots - I'd assumed from the way that the question was posed that this was code deployed on machines not generally under your control (since if they are under your control, just get WUA set right once and don't write any code). – Damien_The_Unbeliever Mar 08 '12 at 13:04

1 Answers1

0

There is an API for the Windows Update Agent (WUA), its general use is described at:

Install Windows Update Using C#

Here is the C# code:

// See http://techforum4u.com/entry.php/11-Install-Windows-Update-Using-C
var auc = new WUApiLib.AutomaticUpdatesClass();
if (auc.Settings.NotificationLevel ==
       AutomaticUpdatesNotificationLevel.aunlScheduledInstallation)
{
    Console.Write("{0}Warning W20120307-1107. Windows Update Agent (WUA) is capable of rebooting the PC automatically. Recommend switching this off for the duration of this 6-day batch process.\n", NPrefix());
}

After adding c:\WINDOWS\system32\wuapi.dll, if you get a compile error, see Interop type cannot be embedded.

Note that this approach will only work on Windows 7, add a try/catch around it to prevent a crash on Windows XP. See the forums for how to get it working on XP (you have to reference WUA version 1, WUA for Windows 7 is version 2).

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • Update: been running this for 2 weeks, and it works seamlessly on both Windows 7 and Windows Server 2008 R2. Definitely a good solution. – Contango Mar 19 '12 at 17:09