I'm building a script that'll automatically look for, download and install updates using Windows Update.
I'm using this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WUApiLib;
namespace update_script
{
class Program
{
static void Main(string[] args)
{
UpdatesAvailable();
EnableUpdateServices();//enables everything windows need in order to make an update
InstallUpdates(DownloadUpdates());
Console.Read();
}
//this is my first try.. I can see the need for abstract classes here...
//but at least it gives most people a good starting point.
public static void InstalledUpdates()
{
UpdateSession UpdateSession = new UpdateSession();
IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
UpdateSearchResult.Online = true;//checks for updates online
ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=1 AND IsHidden=0");
//for the above search criteria refer to
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
//Check the remakrs section
foreach (IUpdate x in SearchResults.Updates)
{
Console.WriteLine(x.Title);
}
}
public static void UpdatesAvailable()
{
UpdateSession UpdateSession = new UpdateSession();
IUpdateSearcher UpdateSearchResult = UpdateSession.CreateUpdateSearcher();
UpdateSearchResult.Online = true;//checks for updates online
ISearchResult SearchResults = UpdateSearchResult.Search("IsInstalled=0 AND IsPresent=0");
//for the above search criteria refer to
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=VS.85).aspx
//Check the remakrs section
foreach (IUpdate x in SearchResults.Updates)
{
Console.WriteLine(x.Title);
}
}
public static UpdateCollection DownloadUpdates()
{
UpdateSession UpdateSession = new UpdateSession();
IUpdateSearcher SearchUpdates = UpdateSession.CreateUpdateSearcher();
ISearchResult UpdateSearchResult = SearchUpdates.Search("IsInstalled=0 and IsPresent=0");
UpdateCollection UpdateCollection = new UpdateCollection();
//Accept Eula code for each update
for (int i = 0; i < UpdateSearchResult.Updates.Count; i++)
{
IUpdate Updates = UpdateSearchResult.Updates[i];
if (Updates.EulaAccepted == false)
{
try
{
Updates.AcceptEula();
}
catch (Exception)
{
// Write a line to tell the user that an error occured with accepting this Eula
Console.WriteLine("Det oppsto en feil under akseptering av en Eula");
}
}
UpdateCollection.Add(Updates);
}
//Accept Eula ends here
//if it is zero i am not sure if it will trow an exception -- I havent tested it.
UpdateCollection DownloadCollection = new UpdateCollection();
UpdateDownloader Downloader = UpdateSession.CreateUpdateDownloader();
for (int i = 0; i < UpdateCollection.Count; i++)
{
DownloadCollection.Add(UpdateCollection[i]);
}
// Define outside try-catch statement
UpdateCollection InstallCollection = new UpdateCollection();
try
{
Downloader.Updates = DownloadCollection;
Console.WriteLine("Downloading Updates");
IDownloadResult DownloadResult = Downloader.Download();
for (int i = 0; i < UpdateCollection.Count; i++)
{
if (DownloadCollection[i].IsDownloaded)
{
InstallCollection.Add(DownloadCollection[i]);
}
}
}
catch (Exception)
{
// Error occured, tell the user
Console.WriteLine("An error occurred while downloading the updates. Please reboot and try again.");
}
return InstallCollection;
}
public static void InstallUpdates(UpdateCollection DownloadedUpdates)
{
try
{
UpdateSession UpdateSession = new UpdateSession();
UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller;
InstallAgent.Updates = DownloadedUpdates;
//Starts a synchronous installation of the updates.
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods
IInstallationResult InstallResult = InstallAgent.Install();
Console.WriteLine("install block over");
// Check if a reboot is required
if (InstallAgent.RebootRequiredBeforeInstallation)
{
// Reboot
Console.WriteLine("Reboot required");
}
}
catch (Exception)
{
// An error occured
Console.WriteLine("An error occured while trying to install the updates. Try again but remember to run as administrator");
}
}
public static void EnableUpdateServices()
{
IAutomaticUpdates updates = new AutomaticUpdates();
if (!updates.ServiceEnabled)
{
Console.WriteLine("Not all updates services where enabled. Enabling Now" + updates.ServiceEnabled);
updates.EnableService();
Console.WriteLine("Service enable success");
}
}
}
}
I think it ran well the first time I ran it, but it threw an exception(s) the second time.
When running as administrator, this error occur:
Also, does a script like this require to be ran as admin?
Thanks for help on this! Erik
I use the following VBscript:
I use the following wrapper and have it scheduled to run once a week. I've recently modified it for Windows 10, it now enables/disables the Windows Update service to restrict updates when the script is not running. I like keeping logs of the updates in case issues develop.