Forum Discussion

tryllzhuud's avatar
tryllzhuud
Copper Contributor
Mar 19, 2024

Output in CLI different that what is saved in log file ?!

Hi,

I have the below command which shows errors, both in the Shell, and also logs into a log file.

Function errMessage
{
param([Parameter(Mandatory=
$true)][String]$err)
Write-Warning -Message $(
"`n$(getDateTime) $err`n" + $_.Exception.Message + $_.CategoryInfo + "$($_.InvocationInfo.ScriptLineNumber): $($_.InvocationInfo.Line)") *>&1 | Tee-Object $logFilePath -Append
}

The error appears fine in the Powershell window…

WARNING:
WARNING: 19 Mar 2024 10:51:55:57 Adding Task To Scheduler Failed
WARNING: Cannot create a file when that file already exists.
WARNING: ResourceExists: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException525:
WARNING: Register-ScheduledTask VLABADScript -InputObject $taskvlabad -ErrorAction Stop | Out-Null
WARNING:

But saves as below in the log file.

1 9 M a r 2 0 2 4 1 0 : 5 1 : 5 5 : 5 7 A d d i n g T a s k T o S c h e d u l e r F a i l e d
C a n n o t c r e a t e a f i l e w h e n t h a t f i l e a l r e a d y e x i s t s .
R e s o u r c e E x i s t s : ( P S _ S c h e d u l e d T a s k : R o o t / M i c r o s o f t / . . . S _ S c h e d u l e d T a s k ) [ R e g i s t e r - S c h e d u l e d T a s k ] , C i m E x c e p t i o n 5 2 5 :
R e g i s t e r - S c h e d u l e d T a s k V L A B A D S c r i p t - I n p u t O b j e c t $ t a s k v l a b a d - E r r o r A c t i o n S t o p | O u t - N u l l

Not sure what is going on, any thoughts ?

Thank You

4 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    tryllzhuud 

     

    Hi, Touqueer.

     

    Superficially, it looks like an encoding issue with the file, as that's what a Unicode stream output to an ANSI file tends to appear like.

     

    Separately, Tee-Object is not technically positioned correctly.

     

    The idea behind Tee-Object is that it splits one input channel into two destinations: a file and then the standard pipeline. Pipeline redirection is not required in this scenario.

     

    With that in mind, simply:

     

    1. Construct the string;
    2. Pipe it to Tee-Object first;
    3. Let Tee-Object work as intended, where it sends the input to a file as well as the standard output;
    4. Leave Write-Warning last, which takes the standard pipeline input and writes it to the warning pipeline.

     

    Example

    "Boo!" | Tee-Object -FilePath "D:\Data\Temp\Forum\warning.log" | Write-Warning;

     

    Output (screen)

     

    Output (file)

     

    Cheers,

    Lain

    • tryllzhuud's avatar
      tryllzhuud
      Copper Contributor

      LainRobertson

       

      I tried this and it works fine but the output saved in the file did not change and remains the same which bring in Encoding.

       

      I came to know 2 things.

       

      1) There is a $PSDefaultParameterValues = @{ '*:Encoding' = 'UTF8' }. The questio is if this sets Encoding of the whole script from the start (added to the start of the script), or does it change the Encoding of which ever fils is created durin gscript runtime ?

       

      2) Add-Content has an -Encoding switch but I'm not sure how this would work with Tee-Object since Add-Content adds to the file, and Tee-Object adds to the file and displays in the CLI as well ?!

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        tryllzhuud 

         

        Hi, Touqueer.

         

        You can remove the value from PSDefaultParameters as shown below - which you would need to do since Tee-Object offers no control over encoding.

         

        Reference

         

        Examples

         

        Once you've removed the relevant encoding entry from $PSDefaultParameterValues, try your command again - the file output should now be fine.

         

        Cheers,

        Lain

Resources