Jump to content

WUApiLib in C# (windows updates library) - Need help


Gr4vitas

Recommended Posts

I'm trying to use the WUApiLib library in a program I'm writing, my goal is to automate windows updates, long story short, my program runs on my laptop and a few other PC's but my test box i've got set up likes to fail when I hit the "downloader.download();" I can't figure out why its exploding on me.

 

static public void Install_Updates()
          {   

              try
              {                   
                  UpdateSessionClass uSession = new UpdateSessionClass();
                  IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
                  ISearchResult uResult = uSearcher.Search("IsInstalled=0 and Type='Software'");

                  Console.WriteLine("check");


                  if (uResult.Updates.Count < 1)
                  {
                      Console.WriteLine("No updates to install.");                       
                  }
                  else if (uResult.Updates.Count > 1)
                  {
                      Console.WriteLine("checkuno");
                      foreach (IUpdate update in uResult.Updates)
                      {
                          Console.WriteLine(update.Title);
                      }

                      Console.WriteLine("Downloading updates...");

                      UpdateDownloader downloader = uSession.CreateUpdateDownloader();
                      Console.WriteLine("line");
                      downloader.Updates = uResult.Updates;
                      Console.WriteLine("line2");
                      downloader.Download();
                      Console.WriteLine("line3");

                      Console.WriteLine("Updates Downloaded.");
                      Console.WriteLine("Updating Collections...");
                      UpdateCollection updatesToInstall = new UpdateCollection();
                      foreach (IUpdate update in uResult.Updates)
                      {
                          if (update.IsDownloaded) updatesToInstall.Add(update);
                      }

                      Console.WriteLine("Installing Collected Updates...");
                      IUpdateInstaller installer = uSession.CreateUpdateInstaller();
                      Console.WriteLine("Check1");
                      installer.Updates = updatesToInstall;
                      Console.WriteLine("Check2");

                      IInstallationResult installationRes = installer.Install();
                      Console.WriteLine("Check3");
                      for (int i = 0; i < updatesToInstall.Count; i++)
                      {
                          Console.WriteLine("Check4");
                          if (installationRes.GetUpdateResult(i).HResult == 0)
                          {
                              Console.WriteLine("Check5");
                              Console.WriteLine("Installed : " + updatesToInstall[i].Title);
                          }
                          else
                          {
                              Console.WriteLine("Failed : " + updatesToInstall[i].Title);
                          }

                          Console.Write("pause");
                      }

                  }
            }
           catch (Exception e)
           {
               Console.WriteLine("Exception", e);
           }

       }

 

edit: all the stupid console writelines were put in because I couldnt figure out where it was failing on my test box. works fine on my development box.

Edited by Gr4vitas

Share this post


Link to post
Share on other sites

Are you sure your test box is set-up to use WUA? Go to C:\Windows\system32 and make sure the wuaueng.dll is version 5.4.3790.1000 or later.

 

roger, checked it out she's running 7.3.7600.16385

 

Everything else checks out fine, she goes and looks for updates and everything, then when she tries to download she just fails out and snags my exception handler.

Share this post


Link to post
Share on other sites

Try going to C:\Windows\SoftwareDistribution\Download and delete everything in there and then try running it again.

 

I doubt it's the script if it worked fine on other machines. It's your test box that has issues. Maybe double check Firewall/Security/Admin settings, too.

Share this post


Link to post
Share on other sites

Try going to C:\Windows\SoftwareDistribution\Download and delete everything in there and then try running it again.

 

I doubt it's the script if it worked fine on other machines. It's your test box that has issues. Maybe double check Firewall/Security/Admin settings, too.

 

 

jesus @#%& I just checked the stupid butt windows 7 user account control settings, the stupid . was on and was stopping it from downloading but wasn't complaining because my program wasn't "recognized" by it. Been beating my head over this for over an hour, thanks for the help haha :cheers:

Share this post


Link to post
Share on other sites

jesus @#%& I just checked the stupid butt windows 7 user account control settings, the stupid . was on and was stopping it from downloading but wasn't complaining because my program wasn't "recognized" by it. Been beating my head over this for over an hour, thanks for the help haha :cheers:

Haha, np. Happens to the best of us! :cheers:

Share this post


Link to post
Share on other sites

It seems I ran into another problem, I'm not sure how familiar you are with the WUApiLib but, I need to enable auto updates on the system before I can try using anything from WUApiLib, otherwise it goes out to windows and windows just says "nope auto updates aren't enabled can't do anything" and my program comes back with "...". So I'd rather not have to manually enable auto updates on every system I'm trying to do this on, is there a way to do this with WUApiLib?

Share this post


Link to post
Share on other sites

Unfortunately, I don't use WUApiLib. Try using this (vbscript):

 

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software'")


WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(I)
   WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(I)
   WScript.Echo I + 1 & "> adding: " & update.Title 
   updatesToDownload.Add(update)
Next

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

WScript.Echo  vbCRLF & "List of downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(I)
   If update.IsDownloaded Then
      WScript.Echo I + 1 & "> " & update.Title 
   End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

WScript.Echo  vbCRLF & _
"Creating collection of downloaded updates to install:" 

For I = 0 To searchResult.Updates.Count-1
   set update = searchResult.Updates.Item(I)
   If update.IsDownloaded = true Then
      WScript.Echo I + 1 & "> adding:  " & update.Title 
      updatesToInstall.Add(update)	
   End If
Next

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "N" or strInput = "n") Then 
WScript.Quit
ElseIf (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()

WScript.Echo "Installation Result: " & _
installationResult.ResultCode 
WScript.Echo "Reboot Required: " & _ 
installationResult.RebootRequired & vbCRLF 
WScript.Echo "Listing of updates installed " & _
"and individual installation results:" 

For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode 		
Next
End If

 

Just launch Command Prompt and go to the file location of where the x.vbs script is installed and run:

 

cscript x.vbs

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...