How do I combine 2 Error outputs into one

Welcome Forums General PowerShell Q&A How do I combine 2 Error outputs into one

This topic contains 1 reply, has 2 voices, and was last updated by

2 months, 3 weeks ago.

  • Author
  • #162869

    Topics: 3
    Replies: 5
    Points: 44
    Rank: Member

    Below enters 2 different lines into my Log upon Error:

    ## The location/filename where the Logs will be stored
    $varfullpath = "C:\Users\Simon.Evans\Documents\ReferenceData__logfile.txt"
    ## The location/filename of the Source to copy from 
    $sourceDirectory = "C:\Users\Simon.Evans\Documents\Source Data\LNAT\Code_Maping.txt"
    ## The location/filename of the Destination to copy to 
    $destinationDirectory = "I:\Dev\BI\Projects\Powershell\Test Area\Source Data\LNAT\Code_Mapping.txt"
    ## Attempts to copy a file fron Source to Destination and record the event, if the Copy-item fails the script is halted and the error messages are captured
    ## Possibly only 1 error is needed and or applicable, so remove as necessary.
    Copy-item -Force -Verbose $sourceDirectory -Destination $destinationDirectory -ErrorAction Stop 
    Write-log -Message "Copy from $sourceDirectory to $destinationDirectory suceeded" -path $varfullpath 
    $Error[0] | Write-Log -path $varfullpath 
    Write-log -Message "Copy from $sourceDirectory to $destinationDirectory Failed" -Level Error -path $varfullpath 

    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 lines.




  • #162872

    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

You must be logged in to reply to this topic.