Windows Update Notification Email

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
User avatar
jimimaseye
Moderator
Moderator
Posts: 8363
Joined: 2011-09-08 17:48

Windows Update Notification Email

Post by jimimaseye » 2018-07-22 20:02

Not strictly Hmailserver related, but admins of Hmailserver might like this feature as overall Administrator of the Windows Server. I have been using it since day 2011.

Set it to run daily in Task Manager (or other scheduler of your choice), it checks your windows update situation on the server and send you a nicely formatted email to your inbox.

Untitled.png

This script is taken from http://www.tachytelic.net/2007/08/windo ... ification/ - screenshot also provided there of the email you receive. I have adapted it for Hmailserver emailing, buy left the original emailing feature by CDO in there (REMmed).

1, Change email addresses accordingly.
2, Set preferences to your 'Alert' levels (set to 1 where required) and server connection/port details.
3, Download and extract UPDATETEMPLATE.HTM (attached as a Zip) and save in the same location as the script.

Code: Select all

' Taken from http://www.tachytelic.net/2007/08/windows-update-automatic-e-mail-notification/
'
'==========================================================================
' NAME:    Windows update automatic e-mail notification
' AUTHOR:    Paul Murana, Accendo Solutions
' DATE  :    26/08/2007
'==========================================================================
'Change these variables to control which updates trigger an e-mail alert
'and to configure e-mail from/to addresses
AlertCritical   = 1 
AlertImportant  = 1
AlertModerate   = 1
AlertLow        = 1
EmailFrom       = "ServerAdmin <server@mydomain.net>"
EmailFromAdd    = "server@mydomain.net"
EmailTo         = "gareth@mydomain.net"
RemoteSMTPServer= ""
RemoteSMTPPort  = "25"

Const HMSADMINUSER = "Administrator" ' Admin username
Const HMSADMINPWD  = "secret" ' Admin password
'==========================================================================

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

Set oShell = CreateObject( "WScript.Shell" )
computername = oShell.ExpandEnvironmentStrings("%ComputerName%")
DomainName = oShell.ExpandEnvironmentStrings("%userdomain%")
EMailSubject = "Windows Update Notification - " & DomainName & "\" & computername
Set oshell = Nothing

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

If searchResult.Updates.count > 0 Then
   For I = 0 To searchResult.Updates.Count-1
      Set update = searchResult.Updates.Item(I)

WScript.Echo update.Title & " '"&update.MsrcSeverity&"'"
severity="'"&update.MsrcSeverity&"'"
      
'      Select Case update.MsrcSeverity
      Select Case severity
         Case "'Critical'" 
            CriticalCount = Criticalcount+1
            CriticalHTML = CriticalHTML & MakeHTMLLine(update)
         Case "'Important'"
            ImportantCount = Importantcount + 1
            ImportantHTML = ImportantHTML & MakeHTMLLine(update)
' Next added to incorporate missing severity report (seems common) and default assume them to IMPORTANT
         Case "''"
            ImportantCount = Importantcount + 1
            ImportantHTML = ImportantHTML & MakeHTMLLine(update)
         Case "Moderate"
            ModerateCount = Moderatecount + 1
            ModerateHTML = ModerateHTML & MakeHTMLLine(update)
         Case "Low"
            Lowcount = Lowcount + 1
            LowHTML = LowHTML & MakeHTMLLine(update)
      end select         
   Next
   If searchResult.Updates.Count = 0 Then
      WScript.Quit
   Else
WScript.Echo "A"&AlertCritical & " B"&CriticalCount & " C"&AlertImportant & " D"&ImportantCount & " E"&AlertModerate & " F"&ModerateCount & " G"&AlertLow & " H"&LowCount
      If (AlertCritical=1 and CriticalCount > 0) then SendEmail=1 end if
      If (AlertImportant=1 and ImportantCount > 0) then SendEmail=1 end if
      If (AlertModerate=1 and ModerateCount > 0) then SendEmail=1 end if
      If (AlertLow=1 and LowCount > 0) then SendEmail=1 end If
      if SendEmail=1 Then
' ***********this section bespoke to match hamilserver  ***************      
      Set oApp = CreateObject("hMailServer.Application")
      Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)
      Set Message = CreateObject("hMailServer.Message")
      Message.FromAddress = EmailFromAdd
      Message.From = EmailFrom
      Message.AddRecipient "System Administrator", EmailTo
      Message.Subject = EMailSubject
      Message.HTMLBody = ReplaceHTMLTemplate()
      Message.Save
' ***********this section bespoke to match hamilserver  ***************      
'         Set objMessage       = CreateObject("CDO.Message") 
'         objMessage.Subject    = EMailSubject
'         objMessage.From    = EmailFrom
'         objMessage.To       = EmailTo
'         objMessage.HTMLBody    = ReplaceHTMLTemplate()
'         if RemoteSMTPServer <> "" then
'            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.net/cdo/configuration/sendusing") = 2 
'            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.net/cdo/configuration/smtpserver") = RemoteSMTPServer
'            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.net/cdo/configuration/smtpserverport") = RemoteSMTPPort
'            objMessage.Configuration.Fields.Update
'         end if
'         objMessage.Send
      end if
   end If
End If

Function MakeHTMLLine(update)
   HTMLLine="<tr><td>" & update.Title & "</td><td>" & update.description & "</td><td>"
   counter   =0   
   For Each Article in Update.KBArticleIDs 
      if counter > 0 then HTMLLine=HTMLLine & "<BR>"
      HTMLLine=HTMLLine & "<a href=" & chr(34) & "http://support.microsoft.net/kb/" & article & "/en-us" & chr(34) & ">KB" & article & "</a>"
      counter = counter +1
   Next 
   For Each Info in Update.moreinfourls 
      if counter > 0 then HTMLLine=HTMLLine & "<BR>"
      HTMLLine=HTMLLine & "<a href=" & chr(34) & info & chr(34) & ">" & "More information...</a>"
      counter = counter +1
      Next    
   HTMLLine = HTMLLine & "</td></tr>"
   MakeHTMLLine = HTMLLine
End function

Function ReplaceHTMLTemplate()
   Set HTMLFile = fso.opentextfile((fso.GetParentFolderName(WScript.ScriptFullName) & "\updatetemplate.htm"),1,false)
   MasterHTML = HTMLFile.Readall
   HTMLFile.close

   MasterHTML = Replace(MasterHTML, "[criticalupdatecontents]", CriticalHTML)
   MasterHTML = Replace(MasterHTML, "[importantupdatecontents]", ImportantHTML)
   MasterHTML = Replace(MasterHTML, "[moderateupdatecontents]", ModerateHTML)
   MasterHTML = Replace(MasterHTML, "[lowupdatecontents]", LowHTML)
   MasterHTML = Replace(MasterHTML, "[computername]", Computername)
   MasterHTML = Replace(MasterHTML, "[domainname]", domainname)
   MasterHTML = Replace(MasterHTML, "[timenow]", now())
   
   If (CriticalCount = 0) then
      MasterHTML = TrimSection(MasterHTML, "<!--CriticalStart-->", "<!--CriticalEnd-->")
   end if

   If (ImportantCount = 0) then
      MasterHTML = TrimSection(MasterHTML, "<!--ImportantStart-->", "<!--ImportantEnd-->")
   end if
    
   If (moderateCount = 0) then
      MasterHTML = TrimSection(MasterHTML, "<!--ModerateStart-->", "<!--ModerateEnd-->")
   end if
    
   If (LowCount = 0) then   
      MasterHTML = TrimSection(MasterHTML, "<!--LowStart-->", "<!--LowEnd-->")
   end if
   ReplaceHTMLTemplate = MasterHTML       
End Function

Function TrimSection(CompleteString,LeftString,RightString)
   LeftChunkPos=inStr(CompleteString, LeftString)
   RightChunkPos=inStrRev(CompleteString, Rightstring)
   LeftChunk=Left(CompleteString, LeftChunkPos-1)
   RightChunk=mid(CompleteString, RightChunkPos)
   TrimSection=LeftChunk & RightChunk
End Function
Attachments
updatetemplate.zip
Contains a template .HTM thats is required (forming the body of the email). Extract to same location as the script.
(1.47 KiB) Downloaded 89 times
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

User avatar
mattg
Moderator
Moderator
Posts: 20632
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Windows Update Notification Email

Post by mattg » 2018-07-23 00:56

jimimaseye wrote:
2018-07-22 20:02
This script is taken from http://www.tachytelic.co/2007/08/window ... ification/
Dead link jimimaseye
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

User avatar
mattg
Moderator
Moderator
Posts: 20632
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Windows Update Notification Email

Post by mattg » 2018-07-23 01:29

fixed

.net not .co
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

User avatar
jimimaseye
Moderator
Moderator
Posts: 8363
Joined: 2011-09-08 17:48

Re: Windows Update Notification Email

Post by jimimaseye » 2018-07-23 08:41

Thanks matt. It seems my anonymisation script (that I put all scripts through before posting) worked a little too well.
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

User avatar
RvdH
Senior user
Senior user
Posts: 925
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Windows Update Notification Email

Post by RvdH » 2018-07-23 12:47

If you like to hide hidden updates

Code: Select all

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

If you like to include Optional updates

WinUpdates.vbs

Code: Select all

... line 13
AlertOptional = 1

... line 49
If update.BrowseOnly Then ' BrowseOnly is specifically used for whether an update is deemed optional or not (True for optional)
	OptionalCount = OptionalCount + 1
	OptionalHtml = OptionalHtml & MakeHTMLLine(update)
end if

... line 62
If (AlertOptional=1 and OptionalCount > 0) then SendEmail=1 end If	

... line 111
MasterHTML = Replace(MasterHTML, "[optionalupdatecontents]", OptionalHTML)

... line 132
If (OptionalCount = 0) then	
	MasterHTML = TrimSection(MasterHTML, "<!--OptionalStart-->", "<!--OptionalEnd-->")
end if
updatetemplate.htm (after <!--LowEnd-->)

Code: Select all

<!--OptionalStart-->
<div ID=Optional>
<p><b>Optional Updates</b></p>
<table>
<tbody>
<tr>
<th width="20%">Title</th>
<th width="70%">Description</th>
<th width="10%">Links</th>
</tr>

[optionalupdatecontents]

</tbody>
</table>
</div>
<!--OptionalEnd-->
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
RvdH
Senior user
Senior user
Posts: 925
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Windows Update Notification Email

Post by RvdH » 2018-07-23 13:40

Hide annoying silverlight optional updates
taken from: https://superuser.com/questions/435124/ ... es#tab-top
run by issuing the following command:
cscript.exe WUA_xSilverlight.js
WUA_xSilverlight.js

Code: Select all

var wu_Session = WScript.CreateObject("Microsoft.Update.Session");
var wu_Searcher = wu_Session.CreateUpdateSearcher();
wu_Searcher.Online = false;

do {
  WScript.echo("Searching...");
  var silverstreak = false;
  var searchResult = wu_Searcher.Search("IsHidden=0 And IsInstalled=0");

  for(var i=0; i<searchResult.Updates.Count; i++){
    var wupdate = searchResult.Updates.Item(i);
    if ( ! wupdate.Title.indexOf("Microsoft Silverlight") ) { // if -1
    wupdate.IsHidden=1;
    WScript.echo("   Hiding update: " + wupdate.Title);
    silverstreak = true;
    }
  }
} while ( silverstreak );  // repeat search
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

User avatar
jimimaseye
Moderator
Moderator
Posts: 8363
Joined: 2011-09-08 17:48

Re: Windows Update Notification Email

Post by jimimaseye » 2018-08-02 09:13

For clarification on Critical, Important, Moderate etc and how they relate to 'optional' updates, this answer gives some insight:

https://answers.microsoft.com/en-us/win ... 105d126cc2

(you will see the categories such as 'Critical, Important etc' doesnt always apply as you think and that some optional updates do not have a category and therefore get defaulted to IMPORTANT by the script. (Hiding them from Windows Update should stop them appearing though).
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

Post Reply