Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When running the script extract below it used different encoding when running from command shell or as scheduled task!

The relevant command is 'Add-Content', why does it be have different when running this directly wihtin a command window then running as scheduled task. Why do they use different encoding (which messes up the logfile).

# Function to print-out messages, including <date> and <time> information.
$scriptName = $MyInvocation.MyCommand.Name
$logFile = "$env:ProgramData\Siemens\LMS\Logs\CleanUp.log"  
function Log-Message
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string]$LogMessage
    )

	$logEntry = "[$scriptName/$scriptVersion] $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $LogMessage"  
    Write-Output $logEntry
	Add-Content -Path $logFile -Value $logEntry -Encoding ASCII 
}


What I have tried:

Tried with and without '-Encoding ASCII'
Posted
Comments
[no name] 8-Nov-23 13:29pm    
"Terminal input" is typically ASCII, but most text "files" these days are UNICODE so it can hold all the "special characters" beyond 127.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900