Powershell Write File To UTF-8

Just a simple program that re-writes files in a folder to UTF-8. A production server I work on had an old version of Powershell running on it and a file needed to be in UTF-8 for it to be read by a Golang-script, so this was created.
This can be run from the terminal by calling the script and passing the logPath and pathToFiles parameters to the Script.
Doesn't write a BOM (Byte Order Mark) on the file, due to the Golang Script reading the file.
This can also be found on my github
## Running this twice on a already utf-8 file causes it to scramble the text
## MAKE SURE THAT THE FILE YOU ARE USING THIS ON ISN'T UTF-8 ALREADY
## Current powershell version doesn't support checking file-encoding quickly without a Byte Order Mark
param
(
$logPath,
$pathToFiles
)
$logDate = Get-Date -Format "yyyy_MM_dd_hh_mm"
$finalLogPath = $logPath + $logDate + "_utf_write_log.txt"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
function somethingWentWrong ($errormessage){
$_ | Out-File $finalLogPath -Append
$sendmailto = ""
$sendmailfrom = ""
$smtp = ""
$subject = "Rewrite failure"
$mailMessage ="An error occurred during file rewrite. Check logs `n" + $finalLogPath + "`n" + $errormessage + "`n"
Send-MailMessage -From $sendmailfrom -To $sendmailto -SmtpServer $smtp -Subject $subject -Body $mailMessage
Exit 1
}
try{
$filesToTransfer | ForEach-Object{
[System.IO.File]::WriteAllLines((Resolve-Path $pathTofiles*$_*),(Get-Content $pathTofiles*$_*), $Utf8NoBomEncoding)
Write-Output "File $_ rewritten as utf-8 successfully" >> $finalLogPath
}
}catch{
$errormessage = $_
Write-Output $errormessage >> $finalLogPath
somethingWentWrong($errormessage);
}
- Log in to post comments