0

Below enters 2 different lines into my Log:

  1. "2019-06-25 16:08:23" "ERROR: " "Copy from C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Maping.txt to I:\Dev\BI\Projects\Powershell\Test Area\Source Data\LNAT\Code_Mapping.txt Failed"

  2. "2019-06-25 16:08:23" "ERROR: " "Cannot find path 'C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Maping.txt' because it does not exist."

Is it possible to combine the 2 or use both in just one line/entry?

catch{
    $Error[0] | Write-Log -path $varfullpath                                                                            
    Write-log -Message "Copy from $sourceDirectory to $destinationDirectory Failed"  -Level Error -path $varfullpath     
} 
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
Simon
  • 391
  • 4
  • 16
  • 1
    You can use a string concatenation by adding a new line with carriage return and can dump it in a single line . If you can share the piece of lines from where the two errors are coming, then we can help you out in catch the errors and collating them – Ranadip Dutta Jun 26 '19 at 07:41
  • 1
    For the point of catchimg them both (all), this is a possible duplicate of [Catching Cascading Errors in PowerShell](https://stackoverflow.com/questions/44441228/catching-cascading-errors-in-powershell) – iRon Jun 26 '19 at 07:46
  • I though I had posted both the output of both line above, essentially I couldn't decide which error to use the system error or the custom error as both provide different information, so wondered if they could be concatinated into just one entry rather than two – Simon Jun 26 '19 at 08:18
  • This question may already have an answer here: - I have looked at that post and cannot find what relates to this one, any ideas? – Simon Jun 26 '19 at 09:46

1 Answers1

0

PowerShell save all the errors in the $Error object.
Each new error is added to the beginning of the list.
If the list gets longer then 256 ($MaximumErrorCount), the last error in the list will drop off.
This means that you might simply concatenate the first two errors in the list (as commented by Ranadip Dutta):

($Error[1].ToString() + [System.Environment]::NewLine + $Error[0].ToString()) |
    Write-Log -path $varfullpath

(Note that this will probably fail if there is only 1 error caught.)

iRon
  • 20,463
  • 10
  • 53
  • 79