Last active
December 22, 2018 19:55
-
-
Save guitarrapc/3ee289271e9a1f1590fef36dd70c2efe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Utf8BomHeaderPS { | |
[CmdletBinding(DefaultParameterSetName = "File")] | |
[OutputType([bool])] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "File")] | |
[System.IO.FileInfo]$File, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Path")] | |
[Alias("LiteralPath", "PSPath")] | |
[string]$Path | |
) | |
process { | |
if ($PSBoundParameters.ContainsKey("File")) { | |
[System.IO.FileStream]$stream | |
try { | |
$stream = $File.OpenRead() | |
$header = (1..3 | ForEach-Object {$stream.ReadByte().ToString("X2")}) -join "" | |
Write-Output ($header -eq $bomHex) | |
} | |
finally { | |
$stream.Dispose(); | |
} | |
} | |
else { | |
[System.IO.FileStream]$stream | |
try { | |
$stream = [System.IO.FileStream]::New($Path, [System.IO.FileMode]::Open) | |
$header = (1..3 | ForEach-Object {$stream.ReadByte().ToString("X2")}) -join "" | |
Write-Output ($header -eq $bomHex) | |
} | |
finally { | |
$stream.Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment