7

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.

  • 3
    Which version of PowerShell are you using? As far as I know, `Write-EventLog` is only available in Windows PowerShell (version 5.1 or older) – Mathias R. Jessen Feb 25 '22 at 14:38
  • 3
    Mathias' comment on Write-EventLog 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: https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7.2&viewFallbackFrom=powershell-6 – Darin Feb 25 '22 at 15:01
  • @Darin you should convert your comment to an answer since it is the solution to the OP. – Jonathan E. Landrum Nov 08 '22 at 16:44
  • 1
    @JonathanE.Landrum, thank you for pointing this out. I had long forgot about this question and didn't think at the time my comment was worthy of being a answer. But it will probably save everyone time if they see an answer instead of looking through the comments. Also, sorry I didn't fix this sooner, been busy. – Darin Dec 27 '22 at 19:04

2 Answers2

2

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.

Darin
  • 1,423
  • 1
  • 10
  • 12
0

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.

Max
  • 3
  • 2