【问题标题】:Writing to windows log with VBScript for Windows Updates?使用 VBScript 写入 Windows 日志以进行 Windows 更新?
【发布时间】:2013-05-31 08:04:33
【问题描述】:

我正在使用来自 Microsoft 的 this sample script 进行 Windows 更新。但是,每当安装更新时,我都想将其写入自定义日志。现在,我将代码的最后一部分调整如下:

    For I = 0 to updatesToInstall.Count - 1
        WScript.Echo I + 1 & "> " & _
        updatesToInstall.Item(i).Title & _
        ": " & installationResult.GetUpdateResult(i).ResultCode 

        Set WshShell = WScript.CreateObject("WScript.Shell")
        addLog = "eventcreate /l Application /t Information /so Test-QA /id 74 /d Windows update added: " & updatesToInstall.Item(i).Title
        WshShell.Run addLog
    Next
End If

但是,安装更新后,Windows 事件日志中没有添加任何内容。我应该怎么做才能将此信息写入日志?

【问题讨论】:

    标签: windows vbscript


    【解决方案1】:

    eventcreate 可能会失败,因为您没有在日志消息周围添加引号。改变

    addLog = "eventcreate /l Application /t Information /so Test-QA /id 74 /d Windows update added: " & updatesToInstall.Item(i).Title
    

    进入

    addLog = "eventcreate /l Application /t Information /so Test-QA /id 74 " _
      & "/d ""Windows update added: " & updatesToInstall.Item(i).Title & """"
    

    您可以像这样检查通过Run 方法运行的命令的返回码:

    rc = WshShell.Run(addLog)
    If rc <> 0 Then
      WScript.Echo "Error: Command exited with return code " & rc
    End If
    

    要查看命令输出,请在命令前面加上 %COMSPEC% /k 并在可见窗口中运行它:

    addLog = "%COMSPEC% /k eventcreate ..."
    rc = WshShell.Run(addLog, 1, False)
    

    由于您正在安装更新,我想您已经在以管理员权限运行脚本,对吗?

    【讨论】:

    • 进行了更改,没有出现错误消息,这很好。但是,Windows 事件日志没有记录我要记录的日志
    • 是的,我是。但是,您的原始答案是正确的。我有一点错字,所以这完全取决于我。感谢您的帮助!
    【解决方案2】:

    为什么不直接使用 LogEvent 方法,它可能会做你需要的一切。

    阅读更多http://technet.microsoft.com/en-us/library/ee176682.aspx

    【讨论】:

    • LogEvent 不允许使用用户定义的源名称或事件 ID。
    猜你喜欢
    • 2010-09-11
    • 2014-10-29
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2014-11-01
    相关资源
    最近更新 更多