Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 MyCmdlets
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message "Starting Update-ClubNumbers" -EventId 0
Get-ClubMembers -HasTemporaryNumber -show all | Update-ClubNumbers -Verbose
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message "Finished Update-ClubNumbers" -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-ClubMembers 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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
+50

What kind of data does the cmdlet return? You could save it into a string like so,

$ret = Get-ClubMembers -Whatever...
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message $ret -EventId 0

If the data returned is an array, you must create an ordinary string first. Othervise, the log message will just contain the array name. Maybe something like this

$ret = Get-ClubMembers -Whatever...
Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message $($ret -join [Environment]::NewLine) -EventId 0

Ed:

As far as I know - and I'd really like to be wrong in this one - you have run into a nasty limitation of Powershell. Keith Hill's blog has kind of a work-around. You mentioned that the cmdlet is a custom one. Maybe you could ask its developer to add a switcht that toggles the messages to stdout, so that executing the cmdlet would return its output as an easily logged string array.

share|improve this answer
    
It doesn't return anything. It prints status messages with WriteVerbose and errors with ThrowTerminatingError. Those are the things I want to log. –  Richard Jun 27 '11 at 12:39
    
Thanks for the edit. This does seem like a very nasty limitation. It seems like the easiest way to fix is to get the custom script to output a status object, but it seems messy. –  Richard Jun 28 '11 at 8:24
    
Interesting. Looks like MS has relieved us of this limitation in PS 3.0. connect.microsoft.com/feedback/… –  Dave Markle Jul 9 '13 at 14:58
add comment

As pointed out by vonPryz there does seem to be no way of capturing verbose messages from a cmdlet, however ThrowTerminatingError throws an exception and I've found that you can capture those and log the error like this:

try
{
    Get-ClubMembers -HasTemporaryEbuNumber -show all | Update-EbuNumbers
}
catch [System.Exception]
{
    Write-EventLog -LogName "Windows Powershell" -Source "Powershell" -Message $_.Exception.ToString() -EventId 0 -EntryType Error
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.