Write-Eventlog source name missing

Wrote a small script to write to my System Event Log and restart my servers

$Server = hostname
New-Eventlog -logname System -source "System Restart"
Write-EventLog -Logname System -source " System Restart" -EntryType Information -EventId 99 -Message "System Restart for Security updates."

Write-EventLog : The source name " System Restart" does not exist on computer “localhost”.
At line:1 char:1

  • Write-EventLog -Logname System -source " System Restart" -EntryType I …
  • CategoryInfo : InvalidOperation: (:slight_smile: [Write-EventLog], InvalidOperationException
  • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteEventLogCommand

I ran this command

[system.diagnostics.eventlog]::SourceExists(“System Restart”)
True

 

 

Any Ideas?

 

Thank you

Tom

Hi Tom,

I saw a couple of errors, after fixing I tested and it worked.
1st. The codes as posted used smart quotes (as in MS-Word quotes). Change them to straight double-quotes or single-quote.
2nd. You have a leading space " System Restart" in the Write-EventLog

$Server = hostname
New-Eventlog -logname System -source “System Restart”
Write-EventLog -Logname System -source “System Restart” -EntryType Information -EventId 99 -Message “System Restart for Security updates.”

Hi GNART

OMG a little space in the wrong place.

Thanks for the quick response.

 

Tom