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

发布于 2025-01-10 03:38:22 字数 545 浏览 0 评论 0原文

我有一个 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.

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

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

任何帮助表示赞赏。

I have a powershell script which i would like to add to the eventlog.

When i type in the Write-Eventlog command i get a error message.

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

I though the syntax was correct but I get a error message.

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

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Does anybody know what I am doing wrong?

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

メ斷腸人バ 2025-01-17 03:38:22

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

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

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

The answer was given in the comments to the question, but as Jonathan pointed out, it should be an answer. Especially for the sake of anyone that might fail to read the comments.

Mathias' comment "Write-EventLog is only available in Windows PowerShell (version 5.1 or older)" is correct.

I found this: Due to the use of unsupported APIs, the -EventLog cmdlets have been removed from PowerShell. Get-WinEvent and New-WinEvent are available to get and create events on Windows on this page.

东风软 2025-01-17 03:38:22

我使用以下解决方法通过创建一个简单的包装函数从 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
}

我从 如何从命令行创建 Windows EventLog 源?

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

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

I used the following workaround for moving from PowerShell Version 5 to 7 by creating a simple wrapper function:

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
}

I got this from How to create Windows EventLog source from command line?

It does not work for existing Sources and requires administrative permissions, as it is to create a source and not entries.

I do not have enough reputation to write this in a comment but I hope this can help someone having the same issue looking for a simple solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文