PowerShell版本1.0 - 写入事件日志

3
我们的客户使用PowerShell 1.0版本。我知道在PowerShell 2.0中有Write-EventLog命令,但是这个命令在PowerShell 1.0中不存在。我希望我的脚本可以将事件记录到事件日志中,主要是为了调试目的。
我该如何在PowerShell 1.0中实现这个功能呢?不过,我不能将这台计算机升级到PowerShell 2.0,因为这是一台正在生产中的客户机器,他们不想更改上面的软件。
2个回答

3
您可以使用.NET EventLog类来完成这个操作。它非常简单易用,例如:
$eventSource = "MyAppName"
if (![Diagnostics.EventLog]::SourceExists($eventSource))
{
    [Diagnostics.EventLog]::CreateEventSource($eventSource, "Application")
}

[Diagnostics.EventLog]::WriteEntry($eventSource, "message", [Diagnostics.EventLogEntryType]::Error)

谢谢。这解决了问题。我先尝试创建一个对象,但这种方式更容易。 - David W.

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接