none
Format Write-Error

    Question

  • Hi

    i have a Error message like this :

    Write-Error  -Message "Backup failed for Datasource $($DSPSN) :: $($DSDP)" -ErrorId $Global:ErrorCode 

    where the output look like this:

    JobStatus : Backup failed for Datasource rumsk01ww99 :: RUMSK01WW99\RACRM
    At F:\Scripts\DPMMgmt.ps1:141 char:25
    +                         JobStatus $Job $ds.DisplayPath $ds.ProductionServerName
    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : 4,JobStatus

    is it possible to get the output to look like this:

    JobStatus : Backup failed for Datasource rumsk01ww99 :: RUMSK01WW99\RACRM
    ErrorId : 4

    Wednesday, September 18, 2013 1:37 PM

Answers

  • the program only starts my script from a CMD promt like this "powershell PathToMyScript" and then capture the Command window for output but how its filter about its a error message or a write-host i dont know.

    Sounds like it's probably doing something like "powershell.exe -File .\YourScript.ps1 > LogFile 2> ErrorLogFile"

    If that's the case, I'm not sure if there's anything you can do about it.  Write-Error sends ErrorRecord objects down the Error stream, and that PowerShell command line just sends the Error stream straight to a file (which will apply the default formatting).

    • Marked as answer by jolesen92 Thursday, September 19, 2013 12:31 PM
    Thursday, September 19, 2013 11:51 AM

All replies

  • You don't have much control over how PowerShell displays an ErrorRecord if you allow it to go all the way back up to the host without capturing it to a variable.  In fact, Write-Error is just for creating an ErrorRecord object and sending it down the Error stream, for a caller to handle later.

    If you want to display that information (and don't need to indicate the error to any calling code), you can just use Write-Host instead:

    Write-Host -ForegroundColor Red "JobStatus : Backup failed for Datasource $($DSPSN) :: $($DSDP)"
    Write-Host -ForegroundColor Red "ErrorId : $Global:ErrorCode"

    Wednesday, September 18, 2013 1:57 PM
  • yeah i typically just check if error contains certain words, then i write-host my custom error message
    Wednesday, September 18, 2013 2:11 PM
  • I cannot use the Write-Hoste because the script is running from some Scheduling software some have to files a standard out file and a Error out file, and text from the write-host is past to the standard out file and the Write-error is past to the Error out file.

    and i need the fail message in the Error out file because its is been used to create a case to the system administrator.

    Wednesday, September 18, 2013 2:11 PM
  • OK, just use Add-Content instead of Write-Host.
    Wednesday, September 18, 2013 2:19 PM
  • i dont have the path to the error out file.
    Wednesday, September 18, 2013 2:23 PM
  • So if I'm understanding this correctly, you're writing code that generates errors, and it's the responsibility of some other code to take those errors and put them into a file.  If that's the case, your code has no control over the formatting; the code that puts the errors into a file has to handle that.  They can do something like this:

    Add-Content -Path $pathToErrorFile -Value "JobStatus : $($errorRecord.Exception.Message)"
    Add-Content -Path $pathToErrorFile -Value "ErrorId : $($errorRecord.FullyQualifiedErrorId)"

    Wednesday, September 18, 2013 2:28 PM
  • the program only starts my script from a CMD promt like this "powershell PathToMyScript" and then capture the Command window for output but how its filter about its a error message or a write-host i dont know.
    Thursday, September 19, 2013 7:22 AM
  • the program only starts my script from a CMD promt like this "powershell PathToMyScript" and then capture the Command window for output but how its filter about its a error message or a write-host i dont know.

    if you cannot change the script then there is nothing you can do about this.  Try contacting the author of the script to make changes.

    ¯\_(ツ)_/¯

    Thursday, September 19, 2013 7:24 AM
  • the program only starts my script from a CMD promt like this "powershell PathToMyScript" and then capture the Command window for output but how its filter about its a error message or a write-host i dont know.

    Sounds like it's probably doing something like "powershell.exe -File .\YourScript.ps1 > LogFile 2> ErrorLogFile"

    If that's the case, I'm not sure if there's anything you can do about it.  Write-Error sends ErrorRecord objects down the Error stream, and that PowerShell command line just sends the Error stream straight to a file (which will apply the default formatting).

    • Marked as answer by jolesen92 Thursday, September 19, 2013 12:31 PM
    Thursday, September 19, 2013 11:51 AM
  • Okay thanks for the help :)
    Thursday, September 19, 2013 12:30 PM