1

I have a catch clock that works like this:

catch  [System.InvalidOperationException]
{
     Process-Exception($_.Exception,1111) 
}

And then I want to process the exception like this:

# Write exceptions to eventlog and reset socket
function Process-Exception { param ($exception, $eventID)

    $dateTime = Get-Date
    #Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `

    #for now: write-host [string] instead of -Message [string]
    write-host "Date and time:`t" + $dateTime + "`r`n`" 
    write-host "Exception Source:`t" + $exception.Source   
    write-host "Error Code:`t"+ $exception.NativeErrorCode  
    write-host "Exception Message:" + $exception.Message  
    write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`

    $socket = $null
}

Where `t is a tab and the $exception properties needs to be accessed. Unfortunately when I do this the exception is not send to the method, only the error code (this case 1111) is send. THe message is:

 write-host   Stack Trace:   + Cannot find type [[System.InvalidOperationException]]: 
make sure the assembly containing this type is loaded. 9999.StackTrace + 

How can I send the full exception to the method? And if you have improvements to the code let me know!

user2609980
  • 10,264
  • 15
  • 74
  • 143

4 Answers4

3

Here is some code I was just playing with.

function Process-Exception
{
    param([System.InvalidOperationException]$Exception, [int] $EventID)
    $dateTime = Get-Date
    #Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `

    #for now: write-host [string] instead of -Message [string]
    write-host "Date and time:`t" + $dateTime + "`r`n`" 
    write-host "Exception Source:`t" + $exception.Source   
    write-host "Error Code:`t"+ $exception.NativeErrorCode  
    write-host "Exception Message:" + $exception.Message  
    write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`

    $socket = $null

}

function ExceptionTesting
{
    Try 
    {
        throw  [System.InvalidOperationException] "Here is a message"
    }
    catch [System.InvalidOperationException]
    {
        #Write-Host $_.Exception
        Process-Exception $_.Exception  1111
    }
}

ExceptionTesting
websch01ar
  • 2,103
  • 2
  • 13
  • 22
  • This function suffers from the same issues as the OP's. The biggest issue being the exception properties are not expanded when writing to the host. – dugas Jan 14 '14 at 19:27
  • I see what you are stating here, but the write-host statements were not the core issue impacting the OP. – websch01ar Jan 14 '14 at 19:31
  • I agree, and didn't down vote you - I was just trying to point out all the mistakes I saw to the OP to better help them understand the changes that would be needed to get the desired outcome. – dugas Jan 14 '14 at 19:42
  • To get to the underlying exception details, would you use Trace? I know PowerShell strips out much of the exception details prior to that. However, you could catch that specific error and then create a custom PSObject that contained some details of note. But that seems like a hack to me. – websch01ar Jan 14 '14 at 19:50
  • Can't I make it a generic function for all exceptions? – user2609980 Jan 15 '14 at 08:02
  • Apparently casting with `[Exception]` in parameters does just that. Problem solved! Thanks a lot @webschO1ar and the rest! – user2609980 Jan 15 '14 at 09:42
3

You have syntax errors in your function and you are calling it wrong.

  1. You have extra tildes on the line to write the date time and the line to write the stack trace.
  2. You are missing closing quotes when writing the stack trace.
  3. You are passing multiple parameters to Write-Host instead of the desired concatenated string.
  4. When calling the function, you are passing a single object array instead of the desired two parameters.

Corrected function:

# Write exceptions to eventlog and reset socket
function Process-Exception { param ([Exception]$exception, [int]$eventID)

    $dateTime = Get-Date

    write-host ("Date and time:`t" + $dateTime + "`r`n")
    write-host ("Exception Source:`t" + $exception.Source)
    write-host ("Error Code:`t"+ $exception.NativeErrorCode)
    write-host ("Exception Message:" + $exception.Message)
    write-host ("Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n")

    $socket = $null
}

Example of calling the function in the catch block:

Process-Exception $_.Exception 111

Reference links:

How to concatenate strings and variables in PowerShell?

Parenthesis Powershell functions

Community
  • 1
  • 1
dugas
  • 12,025
  • 3
  • 45
  • 51
  • ` You have extra tildes on the line to write the date time and the line to write the stack trace.` Haha now I see :-) `When calling the function, you are passing a single object array instead of the desired two parameters.` hmm.. How do I get the exception from the catchblock? Is the code of webscholar above working? (On Linux now..) – user2609980 Jan 14 '14 at 19:12
  • I updated the example of calling the function to show you how to get the exception in the catch block. – dugas Jan 14 '14 at 19:14
  • I guess Trevor and I should have indicated what syntax changes we made. – websch01ar Jan 14 '14 at 19:19
  • @websch01ar - I tested your function and commented on the pending issues. – dugas Jan 14 '14 at 19:31
2

This isn't exactly the same situation as yours, but I'm wondering if this works for you. As far as I can tell, your code looks fine. I've made some changes to the function, and its parameter definitions.

function Process-Exception {
    [CmdletBinding()]
    param (
        [System.Exception] $Exception
        , [int] $EventID
    )

    $dateTime = Get-Date
    #Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `

    #for now: write-host [string] instead of -Message [string]
    write-host "Date and time:`t" + $dateTime + "`r`n";
    write-host "Exception Source:`t" + $exception.Source   
    write-host "Error Code:`t"+ $exception.NativeErrorCode  
    write-host "Exception Message:" + $exception.Message  
    write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n"
}

try {
    [invalidtype]
}
catch [System.Exception] {
    Process-Exception -Exception $_.Exception -EventID 1111;
}
0

Correct me if I am wrong, but the error says that the assembly isn't loaded.

Cannot find type [[System.InvalidOperationException]]: make sure the assembly containing this type is loaded

Try loading the assembly first and let me know if you still get the same error:

add-type -AssemblyName mscorlib

More info on: InvalidOperationException Class

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56