我可以从Windows脚本语言(如JScript)写入Windows事件日志吗?

Can I write into Windows Event Log from a Windows Scripting Language like JScript?

本文关键字:Windows 事件 写入 日志 脚本 语言 我可以 JScript      更新时间:2023-09-26

我有一个小的JScript代码,需要输出它的错误消息。它从Windows脚本主机(作为.js文件)运行。我想知道是否有可能从它写入Windows事件日志?

您可以使用WscriptShell对象本身直接记录事件:

var oWSS = new ActiveXObject("WScript.Shell"); 
oWSS.LogEvent(1,"this is error"); 
oWSS.LogEvent(2,"this is warning"); 
oWSS.LogEvent(4,"this is Info");

这个博客是这样做的:

function Log(message) {
    var shell = new ActiveXObject("WScript.Shell")
    shell.Exec('eventcreate /id 1 /l [LOG] /SO [SOURCE] /T ERROR /D "' + String(message).replace('"', '""') + '"')
}