From 2fbebf9b9c8a814595be5e47a50fb3e431911814 Mon Sep 17 00:00:00 2001 From: BreakwaterLabs <9223149+ll102981@users.noreply.github.com> Date: Fri, 18 Aug 2023 13:36:14 -0400 Subject: [PATCH] Add full pwsh7 compatibility Powershell 7 does not support -encoding byte on add/get content. Instead it uses the 'asbytestream' switch. This uses parameter splatting to convert all references to add- and get-content to be compatible regardless of powershell version. --- GPRegistryPolicyParser.psm1 | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/GPRegistryPolicyParser.psm1 b/GPRegistryPolicyParser.psm1 index 06baf05..f2da10d 100644 --- a/GPRegistryPolicyParser.psm1 +++ b/GPRegistryPolicyParser.psm1 @@ -1,4 +1,4 @@ -########################################################### +########################################################### # # Group Policy - Registry Policy parser module # @@ -22,6 +22,14 @@ data LocalizedData } Import-LocalizedData LocalizedData -filename GPRegistryPolicyParser.Strings.psd1 +# Add compatibility for Powershell Core which does not support 'encoding byte' via splatting +if ( $PSVersionTable.PSVersion.Major -le "5") + { + $byteParam = @{Encoding = "Byte"} + } elseif ($PSVersionTable.PSVersion.Major -gt "5") + { + $byteParam = @{AsByteStream = $True} +} $script:REGFILE_SIGNATURE = 0x67655250 # PRef $script:REGISTRY_FILE_VERSION = 0x00000001 #Initially defined as 1, then incremented each time the file format is changed. @@ -185,14 +193,7 @@ Function Read-PolFile $index = 0 [string] $policyContents = Get-Content $Path -Raw - if ( $PSVersionTable.PSVersion.Major -le "5") - { - [byte[]] $policyContentInBytes = Get-Content $Path -Raw -Encoding Byte - } - elseif ($PSVersionTable.PSVersion.Major -gt "5") - { - [byte[]] $policyContentInBytes= get-content $path -AsByteStream -Raw - } + [byte[]] $policyContentInBytes = Get-Content $Path -Raw @byteParam # 4 bytes are the signature PReg $signature = [System.Text.Encoding]::ASCII.GetString($policyContents[0..3]) @@ -554,7 +555,7 @@ Function Add-RegistryPolicies foreach ($rp in $RegistryPolicies) { [Byte[]] $Entry =New-RegistrySettingsEntry -RegistryPolicy $rp - $Entry | Add-Content -Path $Path -Encoding Byte + $Entry | Add-Content -Path $Path @byteParam } } @@ -610,8 +611,8 @@ Function New-GPRegistryPolicyFile New-Item -Path $Path -Force -Verbose -ErrorAction Stop | Out-Null - [System.BitConverter]::GetBytes($script:REGFILE_SIGNATURE) | Add-Content -Path $Path -Encoding Byte - [System.BitConverter]::GetBytes($script:REGISTRY_FILE_VERSION) | Add-Content -Path $Path -Encoding Byte + [System.BitConverter]::GetBytes($script:REGFILE_SIGNATURE) | Add-Content -Path $Path @byteParam + [System.BitConverter]::GetBytes($script:REGISTRY_FILE_VERSION) | Add-Content -Path $Path @byteParam } <#