From b3cb39afc17338e21e143641e6aadc5f4876e662 Mon Sep 17 00:00:00 2001 From: Fabian Niesen Date: Wed, 25 Jan 2023 16:14:45 +0100 Subject: [PATCH 1/3] Include UTF8BOM (Microsoft Flavor) --- Functions/Get-FileEncoding.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Functions/Get-FileEncoding.ps1 b/Functions/Get-FileEncoding.ps1 index 607e4c5..eacaba0 100644 --- a/Functions/Get-FileEncoding.ps1 +++ b/Functions/Get-FileEncoding.ps1 @@ -68,12 +68,16 @@ function Get-FileEncoding { } elseif ($byte[0] -eq 0x84 -and $byte[1] -eq 0x31 -and $byte[2] -eq 0x95 -and $byte[3] -eq 0x33) { # 84 31 95 33 (GB-18030) Write-Output -InputObject 'GB-18030' - } else { + } elseif ($byte[0] -eq 0x23 -and $byte[1] -eq 0x72 -and $byte[2] -eq 0x65 -and $byte[3] -eq 0x71) { + Write-Output -InputObject 'UTF8BOM' + } + else { Write-Output -InputObject 'ASCII' } } else { Write-Error -Message "The file [$Path] does not exist." } + Write-Verbose "$byte" } end { From 886b625ec52f4c5834c5c1962767e74c193754e4 Mon Sep 17 00:00:00 2001 From: Fabian Niesen Date: Wed, 25 Jan 2023 16:28:14 +0100 Subject: [PATCH 2/3] Twisted UTF8 and UTF8BOM --- Functions/Get-FileEncoding.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Functions/Get-FileEncoding.ps1 b/Functions/Get-FileEncoding.ps1 index eacaba0..5ee49ae 100644 --- a/Functions/Get-FileEncoding.ps1 +++ b/Functions/Get-FileEncoding.ps1 @@ -35,7 +35,7 @@ function Get-FileEncoding { if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) { # EF BB BF (UTF8) - Write-Output -InputObject 'UTF8' + Write-Output -InputObject 'UTF8BOM' } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { # FE FF (UTF-16 Big-Endian) Write-Output -InputObject 'Unicode UTF-16 Big-Endian' @@ -69,7 +69,7 @@ function Get-FileEncoding { # 84 31 95 33 (GB-18030) Write-Output -InputObject 'GB-18030' } elseif ($byte[0] -eq 0x23 -and $byte[1] -eq 0x72 -and $byte[2] -eq 0x65 -and $byte[3] -eq 0x71) { - Write-Output -InputObject 'UTF8BOM' + Write-Output -InputObject 'UTF8' } else { Write-Output -InputObject 'ASCII' @@ -77,7 +77,7 @@ function Get-FileEncoding { } else { Write-Error -Message "The file [$Path] does not exist." } - Write-Verbose "$byte" + Write-Verbose "$byte $Path" } end { From 0a61278f1b5a6428b721f12e17208a887ed76270 Mon Sep 17 00:00:00 2001 From: Fabian Niesen Date: Thu, 26 Jan 2023 08:23:56 +0100 Subject: [PATCH 3/3] Insert catch for get-Content errors --- Functions/Get-FileEncoding.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Functions/Get-FileEncoding.ps1 b/Functions/Get-FileEncoding.ps1 index 5ee49ae..ca95049 100644 --- a/Functions/Get-FileEncoding.ps1 +++ b/Functions/Get-FileEncoding.ps1 @@ -30,7 +30,13 @@ function Get-FileEncoding { process { if (Test-Path -Path $Path) { - [byte[]] $byte = Get-Content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path + Try { [byte[]] $byte = Get-Content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path } + Catch + { + Write-Warning "Get-Content is not working. Be aware PowerShell 7.2 does not support byte encoding anymore" + Write-Output -InputObject 'Unkown' + break + } #Write-Host Bytes: $byte[0] $byte[1] $byte[2] $byte[3] if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) {