/build/static/layout/Breadcrumb_cap_w.png

HOW-TO: Manage/Control WSUS-updates using script

Hi All,

Don't know if this is the proper place for adding scripts?
But if not, maybe someone can help me put it in the right place? :-)


We have a managed environent and release software updates (using MSI) to our clients on a monthly basis.
The same time we release our software updates we also wanted to install available updates from our WSUS server.

The problem we had was that the Windows Update Client downloads and then installs them on a given time of day.
We wanted WSUS updates to be installed with all the other software updates released the current month.


We're currently using these 2 scripts to make sure all the updates available are downloaded and installed from our WSUS server. This has worked extremly well for us. Installation of WSUS updates are now triggered the same way we install our other software installations.



Thought I might share for once instead of just poping in to "steal" tips and tricks ;-)
Hope this can come in handy!



peska



Copy and paste into a notepad and save the file as a .js file.
//----------------------------------------------------------------------------------------------------------------------------
//
// WUADownloadUpdates.js
//
// usage:
// wscript/cscript WUADownloadUpdates.js
//
// searches WindowsSoftwareUpdateServer for available updates and downloads needed updates to the client
//
// Requires WSUS server configured via GPO on client and Windows Update Agent 2.0 installed.
// installationCommand for Windows Update Agent 2.0: wusetup.exe /wuforce /quiet /norestart
//


//The function Downloads available updates from the WSUS
function DownloadAvailableUpdates(availableUpdates)
{
//creates an empty collection object
updatesToDownload = new ActiveXObject("Microsoft.Update.UpdateColl");
//Create Downloader agent
downloader = updateSession.CreateUpdateDownloader() ;

for(i=0;availableUpdates.Updates.Count>i;i++)
//Adds available patches to the list of patches to download
updatesToDownload.Add(availableUpdates.Updates.Item(i));

//Adds the list of updates to the downloader agent
downloader.Updates = updatesToDownload;

//Downloads updates
downloader.Download();
}

//The function returns a list of available patches on the WSUS that is not installed on the current client
function SearchForUpdates(isInstalled)
{
//skapar sökobjektet
updateSearcher = updateSession.CreateupdateSearcher();
//WScript.Echo("Söker efter uppdateringar...");
availableUpdates = updateSearcher.Search("IsInstalled=" + isInstalled + " and Type='Software'");

return availableUpdates;
}


////////////////////////////////////////////// MAIN //////////////////////////////////////////////////


wshShell = new ActiveXObject("WScript.Shell");
updateSession = new ActiveXObject("Microsoft.Update.Session");
availableUpdates = SearchForUpdates("0");

if(availableUpdates.Updates.Count>0)
{
DownloadAvailableUpdates(availableUpdates);
}

//----------------------------------------------------------------------------------------------------------------------------



Copy and paste into a notepad and save the file as a .js file.
Edit the line: \\\\YourServer\\YourShare\\YourFolder\\ to enable logging.
(Double backslashes are required in Jscript.)
//----------------------------------------------------------------------------------------------------------------------------
//
// WUAInstallUpdates.js
// Installs updates that have been downloaded from WSUS but not yet installed
// Logs eventual errors to given path ex: \\YourServer\YourShare\YourLogFolder in format %ComputerName%.ERROR.txt
//
// usage:
// wscript/cscript WUAInstallUpdates.js
//
// Requires WSUS server configured via GPO on client and Windows Update Agent 2.0 installed.
// installationCommand for Windows Update Agent 2.0 wusetup.exe /wuforce /quiet /norestart
//


try
{
wshShell = new ActiveXObject("WScript.Shell");
//Loads Windows Update Agent
updateSession = new ActiveXObject("Microsoft.Update.Session");
//Loads Search Agent
updateSearcher = updateSession.CreateupdateSearcher();
//Sets the Update Agent to OFFLINE so it does not overload the WSUS
updateSearcher.Online = false;

//Gets a list of downloaded patches
availableUpdates = updateSearcher.Search("IsInstalled=" + 0 + " and Type='Software'");
updates = availableUpdates.Updates;

//If the client have patches downloaded but not yet installed, install them.
if(updates.Count>0)
{
//Loads the Installation Agent
updateInstaller = updateSession.CreateUpdateInstaller();
updateInstaller.Updates = availableUpdates.Updates;
//Installs needed patches
installationResult = updateInstaller.Install();
//Logs an event to the eventlog if the installation completed without errors
wshShell.LogEvent(0,"Installation of downloaded patches from WSUS completed successfully");
}
}
catch(e)
{
try
{
fso = new ActiveXObject("Scripting.FileSystemObject");
network = new ActiveXObject("WScript.Network");
computerName = network.ComputerName;

//Logs errors to a logfile
f1 = fso.OpenTextFile("\\\\YourServer\\YourShare\\YourFolder\\" + computerName + ".ERROR.txt",2,true);
f1.WriteLine("An error occured trying to install updates downloaded from WSUS");
f1.WriteLine("Description:");
f1.WriteLine(e.description);
f1.Close();

//Logs error the the eventlog - more detailed error descriptions are automatically logged to the system log by Windows Update Agent.
wshShell.LogEvent(1,"Critical Error!\n\nClient was unable to install updates downloaded from WSUS.\nLook in the System log for a more detailed description");
}
catch(e)
{
}
}

//----------------------------------------------------------------------------------------------------------------------------

0 Comments   [ + ] Show comments

Answers (2)

Posted by: luckg 13 years ago
Yellow Belt
0
I've saved the script for installing the updates and specified the location for the logging. I get this for my servers;
An error occured trying to install updates downloaded from WSUS
Description:


In the Application event logs, it tells me to look in my System event log, which lists no errors or info relating to WSUS.

Any ideas?
Posted by: dunnpy 13 years ago
Red Belt
0
You could use a tool like WUInstall - there is a freeware version that can use to force WSUS updates when the tool is run (with comnmadline options).

Might be neater and easier to use than a script (not saying that there is anything wrong with the above script [;)] )

Hope that helps,

Dunnpy
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ