I am looking for some help with automating the installation of Windows Updates by using the wsusscn2.cab file available from the Microsoft website.
The setup I am trying to implement this onto is a bit of a strange one which is why I think I cannot find much help using Google.
I have 8 machines running Windows 7 SP1 that cannot be connected to the internet, therefore I am downloading the wsusscn2.cab file from Microsoft which apparently contains a list of all updates released and the actual update/hotfixes themselves.
The code I have so far is allowing me to use WUApiLib to read the .cab file and establish from it, which updates are not installed on the machine. This is currently returning a list of around 149 updates that are available but not installed.
When checking the .IsDownloaded() function of each update/hotfix, it is returning False and the Error Code is 'orcFailed'.
This is as close as I can get, as I said with the setup I have, Google is not providing me with alot of help as most people are mentioning things like WSUS on a Windows Server which is not possible or other online solutions which is also something I cannot do.
Here is a snippet of the code I have so far, I'm new to this library and this is my first major C# project so any help would be much appreciated as I feel I'm hitting a brick wall at the minute. Could someone also confirm if the updates are actually stored in the .cab file as I have tried extracting them to see whats inside, to no avail?
Many thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WUApiLib;
using System.Management;
using Shell32;
namespace SoftwareUpdateTool
{
class InstallUpdates
{
public static void Getupdates()
{
UpdateSession updateSession = new UpdateSession();
UpdateServiceManager updateSM = new UpdateServiceManager();
IUpdateService updateService = updateSM.AddScanPackageService("Offline Sync Service", "C:\\Users\\Admin\\Desktop\\Windows Updates\\wsusscn2.cab");
IUpdateSearcher searcher = updateSession.CreateUpdateSearcher();
IUpdateInstaller installer = updateSession.CreateUpdateInstaller();
searcher.ServerSelection = ServerSelection.ssOthers;
searcher.ServiceID = updateService.ServiceID;
ISearchResult SearchResults = searcher.Search("IsInstalled=0");
UpdateCollection foundUpdates = SearchResults.Updates;
Console.WriteLine("Number of updates found are " + foundUpdates.Count);
installer.Updates = foundUpdates;
int updateCount = 0;
foreach (IUpdate x in foundUpdates)
{
Console.WriteLine(x.Title + " " + x.IsDownloaded.ToString());
Console.WriteLine("Error Code >> " + ConvertCode(installResult.GetUpdateResult(updateCount).ResultCode.ToString()));
updateCount += 1;
}
}
private static string ConvertCode(string errorCode)
{
switch (errorCode)
{
case "0":
errorCode = errorCode + " not started";
break;
case "1":
errorCode = errorCode + " in progress";
break;
case "2":
errorCode = errorCode + " succeeded";
break;
case "3":
errorCode = errorCode + " suceeded with errors";
break;
case "4":
errorCode = errorCode + " failed";
break;
case "5":
errorCode = errorCode + " aborted";
break;
}
return errorCode;
}
}
}