Parsing error messages

I wanted to reduce the amount of verbiage in an error message. I came up with this.


$ScriptName = $MyInvocation.MyCommand.Name # gets name of the script that is running
$Error.clear()
# Introduce an error for testing
Stop-Process -Name invalidprocess -EA "SilentlyContinue"
If ($Error) {
$E1 = $E2 = $E3 = $E4 = $E5 = " " #Sets variables to null.
$E1 = [string]$Error.Exception #Catches error
$E2 = $E1.Split(":") # divides the string at the :
$E3 = $E2[1] # Selects the part of the string after the :
$E4 = $E3.Split("\.") # divides the result at the period
$E5 = $E4[0] # selects the first part of of the string
# Write-Log -Message "$ScriptName $E5" -Path $LogPath warn # writes the result to the log
# Break # exits the script
}
Write-Host $E5

For some reason the above script has stopped working. It appears that something has changed in PS ver 2 on Windows 7 at least.
If the above does not work try this:

 


Function err {
If ($Error) {
$E1 = $E2 = $E3 = $E4 = " " #Sets variables to null.
[string]$E1 = $Error #Catches error
[string]$E2 = $E1.split(":") # divides the string at the ":"
$E3 = $E2.split(".") # divides the string at the "."
$E4 = $E3[0] # Selects the part of the string before the "."
Write-Host $E4
}
$Error.clear()
}

 

How do I record an Error in Excell


###### Start Posh Script ######## 

<# ==============================================================================================
Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2011
NAME: ErrorView.ps1
AUTHOR: OldDog
DATE  : 5/18/2012
COMMENT: Ping does not return an Error
 Use Test-Connection instead
==============================================================================================
#>

If ($Error) {
               $ErrorView = "CategoryView"
                $y = $error | select Exception
                $e = $y -split (":")
                $x = $e[2]
                $ws1.Cells.Item($row, 3) = $x
                $ws1.rows.Item($row).Font.ColorIndex = "3"
                $row++
                $Error.Clear()
                $range = $ws1.usedRange
                [void]$range.entireColumn.Autofit()
                $ErrorView = $null
}