使用 powershell 将事件写入事件日志

问题描述 投票:0回答:2

我有一个 powershell 脚本,我想将其添加到事件日志中。

当我输入 Write-Eventlog 命令时,我收到一条错误消息。

Write-EventLog -Logname autosnapshot -Source 'D:\script autoSnapshots.ps1' -EventId 8000 -Entrytype Information -Message 'Creates Snapshots and deletes snapshots older than 1 day'

虽然语法是正确的,但我收到一条错误消息。

Write-EventLog: The term 'Write-EventLog' is not recognized as a name of a cmdlet, function, script file, or executable program.

检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

有人知道我做错了什么吗?

如有任何帮助,我们将不胜感激。

powershell event-log
2个回答
2
投票

问题的评论中给出了答案,但正如乔纳森指出的那样,这应该是一个答案。特别是为了那些可能无法阅读评论的人。

Mathias 的评论 “Write-EventLog 仅在 Windows PowerShell(版本 5.1 或更早版本)中可用” 是正确的。

我发现了这一点:由于使用了不受支持的 API,-EventLog cmdlet 已从 PowerShell 中删除。 Get-WinEvent 和 New-WinEvent 可用于在 Windows 上获取和创建事件,位于 此页面


0
投票

我使用以下解决方法通过创建一个简单的包装函数从 PowerShell 版本 5 迁移到 7:

function Write-EventLog ([parameter(position=0,Mandatory=$true)][String] $Message,[String] $LogName,[String] $Source,[String] $EntryType, [int] $EventId)
{
    eventcreate /ID $EventId /L $LogName /T $EntryType /SO $Source /D $Message
}

我从How to create Windows EventLog source from command line?

得到这个

它不适用于现有源,并且需要管理权限,因为它是创建源而不是条目。

我没有足够的声誉来在评论中写下此内容,但我希望这可以帮助有相同问题的人寻找简单的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.