Author Posts

September 20, 2013 at 1:07 am

Hi Guys, newbie here on the forums.

I looked for an answer to my question first, so this is more of a follow up to this post:

Save the output of powershell script

I use PowerShell quite a lot, but I am no means experienced with scripting/coding.

I have built, what I would class as crude PowerShell applications. A series of functions and a menu system built into a PS1 file.

What I have not been able to yet master is logging.

Just this morning I have found the ‘start-transcript’ command, which appears to be exactly what I want.

However, inside my application I suppress the errors so the user has a more elegant experience.

My thinking here is that a transcript, is essentially going to capture whatever is output to the console, and if i have
$ErrorAction = "SilentlyContinue" the errors wont be there to be transcripted.

What I would like to do is run a transcript but also capture the errors, whilst not displaying the errors out to the console.

Is that possible? Hopefully that makes sense?

Many thanks in advance if you are able to help out!

September 20, 2013 at 1:44 am

I don't think you can.

using the transcript cmdlets toggles the transcript functionality pretty much on or off
-Erroraction silentlycontinue will pretty much suppress your errors and you won't see anything

I think you'll have to log information separately – either using write-eventlog to add it to an event log – I'd recommend creating your own
or use add-content to write the data to a text file

I think you'll end up with something like

try {
do-something -erroraction stop
log success
}
catch
{
log error
}

September 20, 2013 at 3:11 am

If you have some control over how your script is called, you could also use a redirection operator to send the Error stream to a file. It would have to be a separate file from the rest of your logging, though (to avoid "file in use by another process" errors):

.\SomeScript.ps1 2>> .\SomeScriptErrorLog.txt

September 20, 2013 at 3:21 am

Ok so that sounds doable.

I have a shortcut on the desktop of a server that launches Powershell.exe -command xxxx

What do i need to do within the script to get the errors output to a seperate file?

Rob.

September 20, 2013 at 3:24 am

You could add that same redirection operator to the shortcut. PowerShell.exe -command xxxx 2>> .\SomeErrorLog.txt

September 20, 2013 at 3:34 am

2>> .\SomeErrorLog.txt

What exactly does this tell Powershell to do? (i understand the file name bit ;o) )

September 20, 2013 at 6:02 am

It redirects the Error stream to a file (appending to the file if it already exists). "Get-Help about_Redirection" has the details how those operators all work.

September 20, 2013 at 6:37 am

OK i will check it out, many thanks.