Sparhawk
Sparhawk

Reputation: 137

Automatic Updates setting via WMI

Attempting to pull the automatic update settings from the registry of a remote server. For some reason, it's returning a 0 even though a manual check of the key is 1-4. What am I overlooking? Snippet below:

ManagementScope msAutoUpdateReg = new ManagementScope(@"\\" + remoteServer + @"\root\DEFAULT:StdRegProv", connection);
msAutoUpdateReg.Connect();

ManagementClass ci = new ManagementClass(msAutoUpdateReg, new ManagementPath(@"DEFAULT:StdRegProv"), new ObjectGetOptions());
ManagementBaseObject inParams = ci.GetMethodParameters("GetDWORDValue");
inParams["hDefKey"] = 0x80000002; //HKLM
inParams["sSubKeyName"] = @"Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update";
inParams["sValueName"] = "AUOptions";
ManagementBaseObject outParams = ci.InvokeMethod("GetDWORDValue", inParams, null);
UInt32 auValue = (UInt32)outParams["uValue"];

if (auValue.ToString() != "0")
{
    if (auValue == 1)
    {
        string currentSetting = "Keep my computer up to date has been disabled in Automatic Updates.";
    }

    if (auValue == 2)
    {
        string currentSetting = "Notify of download and installation.";
    }

    if (auValue == 3)
    {
        string currentSetting = "Automatically download and notify of installation.";
    }

    if (auValue == 4)
    {
        string currentSetting = "Automatically download and scheduled installation.";
    }
}
else
{
    string currentSetting = "Unknown";
}

Upvotes: 4

Views: 500

Answers (1)

Quorfee
Quorfee

Reputation: 168

I guess a process of elimination might help here...

1) Is this happening on just one server or are you getting this on all servers? How about on your own local machine? Is it a Windows version thing? For example it seems my Windows 10 box doesn't show the SubKey name you are looking for.

2) Do you also get zero if you change the sValueName to "foo"? Is a value of 0 representing an error?

3) Can you put a watch on outParams and check to see what values have been returned?

4) Are you being blocked by UAC, firewall or other permission issues? Can you execute other WMI commands against this server without any problems? Do you need to Run As Administrator to get this to work?

5) Are you getting an other exceptions or return values? I'm guessing you've posted just a portion of the code here so is this code inside a try/catch block?

Sorry if this sounds either vague or simplistic but I think you may need to look at what does work and what doesn't to see if you can identify a pattern.

Upvotes: 1

Related Questions