Saurabh Chauhan
Saurabh Chauhan

Reputation: 3221

How to check windows OS update status using Javascript or Node.js?

I am looking for a way to check whether windows OS and security updates are up to date or not. If not then I would like to fetch this information. Apart from this, If there is any update available then I would like to fetch this information too.

I read several blogs and StackOverflow questions and got the following answers:

  1. Using wmic qfe list but this gives information about the already installed update without status (I need to read status such as fail, aborted or success).
  2. Using the following Powershell script (this gives information about whether an update is available or not):
$u = New-Object -ComObject Microsoft.Update.Session
$u.ClientApplicationID = 'MSDN Sample Script'
$s = $u.CreateUpdateSearcher()
$r = $s.Search('IsInstalled=0')
$r.updates|select -ExpandProperty Title

Is there any way to check "Whether windows OS and security updates are up to date or not? If not then get status (failure, aborted etc.). If any update is available then I would like to fetch information about the available update".

How can I achieve this using Javascript or Node.js?

Upvotes: 1

Views: 916

Answers (1)

tukan
tukan

Reputation: 17345

To my knowledge there is no function to find out if a system is completely updated (only via windows update) for non-enterprise stations. If you would have complete list of updates needed then you could check against the list.

For update management you have to have Windows 10 Enterprise and System Center configured, then you can check if the stations have the required updates installed. With that you could check it.

To get list of installed patches with status you have to do it the following way:

$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(0, $historyCount) | Select-Object Title, Date,
     @{name='ResultCode'; expression={switch($_.ResultCode){ 0 {'Not Started'}; 1 {'In Progress'}; 
          2 {'Success'}; 3 {'Success with Errors'}; 4 {'Failed'}; 5 {'Aborted'}
     }}}

You save it as powershell script e.g. check_updates.ps1.

To run it from javascript you have to spawn the process (running from the dir where the script is saved):

var spawn = require("child_process").spawn;
spawn("powershell.exe",[".\check_updates.ps1"]);

What you have to watch out with such spawning is security. Don't forget to assign correct rights.

For Node.js check this answer: Execute Powershell script from Node.js.

For Node.js you have to write it differently, something along these lines (similar to above posted link):

var spawn = require('child_process').spawn,
    updates = spawn("powershell.exe",["C:\\path\\test\\check_updates.ps1"]);

updates.stdout.on('data', function (data) {
  console.log('stdout: ' + data.toString());
});

updates.stderr.on('data', function (data) {
  console.log('stderr: ' + data.toString());
});

updates.on('exit', function (code) {
  console.log('child process exited with code ' + code.toString());
});

updates.stdin.end();

Note: Sometimes failed update can be included in a cumulative updates so it can be tricky to find if it was installed.

Upvotes: 1

Related Questions