I'm trying an example from M$ site regarding calling Windows Update programatically.
'http://msdn.microsoft.com/en-us/library/aa387102%28VS.85%29.aspx
'http://msdn.microsoft.com/en-us/library/aa386526%28v=vs.85%29.aspx
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'")
On executing the last line, if your network is broken, I'll see on CMD window:
C:\wu-script\wu-install.vbs(9, 1) (null): 0x8024001F
It seems like updateSearcher.Search throws an exception and the whole script exits. How to catch this exception?
I'm not very familiar with VBScript. Please provide a quick hint or a reference URL.
You should use On Error
statement to handling VBScript errors.
'...
'...
On Error Resume Next 'enable error handling
Set searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'")
If Err.Number <> 0 Then
'right, this is a catch block :/
WScript.Echo "error!"
'WScript.Echo Err.Description
'more : http://msdn.microsoft.com/en-us/library/a3c123d4(v=vs.85).aspx
End If
On Error Goto 0 'disable error handling
As you can see, catching error is too troublesome in VBScript. However, could also use javascript and its try-catch.
A sample WSF package based on the script that you gave.
wu-install.wsf
<?xml version="1.0" ?>
<package>
<job id="Update">
<script language="JScript">
<![CDATA[
//http://msdn.microsoft.com/en-us/library/aa387102%28VS.85%29.aspx
//http://msdn.microsoft.com/en-us/library/aa386526%28v=vs.85%29.aspx
var updateSession = WScript.CreateObject("Microsoft.Update.Session");
var updateSearcher = updateSession.CreateupdateSearcher();
WScript.Echo("Searching for updates...");
try {
var searchResult = updateSearcher.Search("IsInstalled=1 and Type='Software'");
} catch(err){
WScript.Echo("error!");
//WScript.Echo(err.message);
// more : http://msdn.microsoft.com/en-us/library/dww52sbt(v=vs.85).aspx
}
]]>
</script>
</job>
</package>
Runing with the cmd:
C:\wu-script>cscript wu-install.wsf
User contributions licensed under CC BY-SA 3.0