OCram85/DroneHelper
OCram85
/
DroneHelper
Archived
1
0
Fork 0
This repository has been archived on 2023-10-10. You can view files and clone it, but cannot push or open issues or pull requests.
DroneHelper/src/FileLinter/Test-FileBOM.ps1

62 lines
1.3 KiB
PowerShell

function Test-FileBOM {
<#
.SYNOPSIS
Tests given file if native utf8 w/o BOM is used. Returns false if BOM is present.
.DESCRIPTION
This function is used to test for a valid encoding without BOM.
.PARAMETER Path
Full or relative path to existing file.
.INPUTS
[None]
.OUTPUTS
[bool]
.EXAMPLE
Test-FileBOM -Path './Testfile.txt'
.NOTES
#>
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory = $true)]
[ValidateScript(
{
Test-Path -Path $_
}
)]
[string]$Path
)
begin {
}
process {
try {
$contents = [byte[]]::new(3)
$stream = [System.IO.File]::OpenRead($Path)
$stream.Read($contents, 0, 3) | Out-Null
$stream.Close()
}
catch {
Write-Error -Message 'Could not read the given file!' -ErrorAction 'Stop'
}
Write-Debug -Message ('BOM Content was: {0}' -f ([System.BitConverter]::ToString($contents)))
if ( $contents[0] -eq 0xEF -and $contents[1] -eq 0xBB -and $contents -eq 0xBF ) {
Write-Output $false
}
else {
Write-Output $true
}
}
end {
}
}