问题描述
限时送ChatGPT账号..我正在尝试 M$ 站点上有关以编程方式调用 Windows 更新的示例.
I'm trying an example from M$ site regarding calling Windows Update programatically.
'http://msdn.microsoft/en-us/library/aa387102%28VS.85%29.aspx
'http://msdn.microsoft/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'")
在执行最后一行时,如果您的网络坏了,我会在 CMD 窗口中看到:
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
似乎 updateSearcher.Search 抛出异常并且整个脚本退出.如何捕捉这个异常?
It seems like updateSearcher.Search throws an exception and the whole script exits. How to catch this exception?
我对 VBScript 不是很熟悉.请提供快速提示或参考网址.
I'm not very familiar with VBScript. Please provide a quick hint or a reference URL.
推荐答案
你应该使用 On Error
语句来处理 VBScript 错误.
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/en-us/library/a3c123d4(v=vs.85).aspx
End If
On Error Goto 0 'disable error handling
如您所见,在 VBScript 中捕获错误太麻烦了.但是,也可以使用 javascript 及其 try-catch.
基于您提供的脚本的示例 WSF 包.
wu-install.wsf
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/en-us/library/aa387102%28VS.85%29.aspx
//http://msdn.microsoft/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/en-us/library/dww52sbt(v=vs.85).aspx
}
]]>
</script>
</job>
</package>
使用 cmd 运行:
Runing with the cmd:
C:\wu-script>cscript wu-install.wsf
这篇关于好像是VBscript异常,怎么处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论