Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions GPRegistryPolicyParser.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
###########################################################
###########################################################
#
# Group Policy - Registry Policy parser module
#
Expand All @@ -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}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used the same version detection logic merged in earlier commits, but instead of using that to call a function we just define the correct parameter as a hashtable.


$script:REGFILE_SIGNATURE = 0x67655250 # PRef
$script:REGISTRY_FILE_VERSION = 0x00000001 #Initially defined as 1, then incremented each time the file format is changed.
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can remove the logic from mid-function and instead simply splat the hashtable


# 4 bytes are the signature PReg
$signature = [System.Text.Encoding]::ASCII.GetString($policyContents[0..3])
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows us to easily make all calls to add/get-content compatible

}

<#
Expand Down