Piping powershell messages to Write-EventLog

Posted by Richard on Server Fault See other posts from Server Fault or by Richard
Published on 2011-06-23T11:13:17Z Indexed on 2011/06/28 8:24 UTC
Read the original article Hit count: 315

Filed under:
|
|

I have a powershell script that runs a custom cmdlet. It is run by Task Scheduler and I want to log what it does.

This is my current crude version:

Add-PsSnapIn PianolaCmdlets
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message "Starting Update-EbuNumbers" -EventId 0
Get-ClubMembers -HasTemporaryEbuNumber -show all | Update-EbuNumbers -Verbose
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message "Finished Update-EbuNumbers" -EventId 0

What I would like to do is log the output of my custom cmdlet. Ideally I'd like to create different types of event log entries based on whether it was a warning or a verbose message.

Update: I don't want to log the return value of the commandlet. The Update-EbuMembers cmdlet does not return an object. I want to log any verbose messages written by WriteVerbose and I want to log errors created by ThrowTerminatingError.

© Server Fault or respective owner

Related posts about logging

Related posts about powershell