Sunday, October 24, 2010

Using Write-EventLog…

I was trying to use the Write-EventLog cmdlet and was noticing additional annoying text being added to the messages.  For example:

PS> write-eventlog -logname "Windows PowerShell" -source PowerShell –event 999 -message "Hello"

Looking at the resulting event, I would see this added:

"The description for Event ID 999 from source PowerShell cannot be
found. Either the component that raises this event is not installed on
your local computer or the installation is corrupted. You can install
or repair the component on the local computer...."

I didn’t want this extra text, and couldn’t seem to figure out the problem, then came across something that Stefan Stranger did HERE:

if(![System.Diagnostics.EventLog]::SourceExists($source))
{
[System.Diagnostics.EventLog]::CreateEventSource($source,'Application')
}

The trick to getting rid of this extra text was to make my own custom source for my events.  Now, the extra text is gone.  I just made up “PowerShell_script”, and used that in my script.