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 has 1 reply, 2 voices, and was last updated 1 year ago by
Viewing 1 reply thread
  • 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: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP
Viewing 1 reply thread
  • The topic ‘How do I combine 2 Error outputs into one’ is closed to new replies.