DSCResources/MSFT_RegistryPolicyFile/MSFT_RegistryPolicyFile.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using module ..\..\Modules\GPRegistryPolicyFileParser
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'GPRegistryPolicyDsc.Common'
$script:GPRegistryPolicyFileParserModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'GPRegistryPolicyFileParser'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'GPRegistryPolicyDsc.Common.psm1')
Import-Module -Name (Join-Path -Path $script:GPRegistryPolicyFileParserModulePath -ChildPath 'GPRegistryPolicyFileParser.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_RegistryPolicyFile'

<#
    .SYNOPSIS
        Returns the current state of the registry policy file.
 
    .PARAMETER Key
        Indicates the path of the registry key for which you want to ensure a specific state. This path must include the hive.
 
    .PARAMETER ValueName
        Indicates the name of the registry value.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the .pol file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
 
    .PARAMETER AccountName
        Specifies the name of the account for an user specific pol file to be managed.
#>

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Key,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ValueName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType,

        [Parameter()]
        [AllowNull()]
        [System.String]
        $AccountName
    )

    Write-Verbose -Message ($script:localizedData.RetrievingCurrentState -f $Key, $ValueName)
    # determine pol file path
    $polFilePath = Get-RegistryPolicyFilePath -TargetType $TargetType -AccountName $AccountName
    $assertPolFile = Test-Path -Path $polFilePath

    # read the pol file
    if ($assertPolFile -eq $true)
    {
        $polFileContents = Read-GPRegistryPolicyFile -Path $polFilePath
        $currentResults  = $polFileContents | Where-Object -FilterScript {$PSItem.Key -eq $Key -and $PSItem.ValueName -eq $ValueName}
    }

    # determine if the key is present or not
    if ($null -eq $currentResults.ValueName)
    {
        $ensureResult = 'Absent'
    }
    else
    {
        $ensureResult = 'Present'
        $valueTypeResult = $currentResults.GetRegTypeString()
    }

    # resolve account name
    $polFilePathArray = $polFilePath -split '\\'
    $system32Index = $polFilePathArray.IndexOf('System32')
    $accountNameFromPath = $polFilePathArray[$system32Index+2]

    if ($accountNameFromPath -match '^S-1-')
    {
        $accountNameResult = ConvertTo-NTAccountName -SecurityIdentifier $accountNameFromPath
    }
    else
    {
        $accountNameResult = $accountNameFromPath
    }

    # return the results
    $getTargetResourceResult = @{
        Key         = $Key
        ValueName   = $ValueName
        ValueData   = [System.String[]] $currentResults.ValueData
        ValueType   = $valueTypeResult
        TargetType  = $TargetType
        Ensure      = $ensureResult
        Path        = $polFilePath
        AccountName = $accountNameResult
    }

    return $getTargetResourceResult
}

<#
    .SYNOPSIS
        Adds or removes the policy key in the pol file.
 
    .PARAMETER Key
        Indicates the path of the registry key for which you want to ensure a specific state. This path must include the hive.
 
    .PARAMETER ValueName
        Indicates the name of the registry value.
 
    .PARAMETER ValueData
        The data for the registry value.
 
    .PARAMETER ValueType
        Indicates the type of the value.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the .pol file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
 
    .PARAMETER AccountName
        Specifies the name of the account for an user specific pol file to be managed.
 
    .PARAMETER Ensure
        Specifies the desired state of the registry policy. When set to 'Present', the registry policy will be created. When set to 'Absent', the registry policy will be removed. Default value is 'Present'.
#>

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Key,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ValueName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType,

        [Parameter()]
        [System.String[]]
        $ValueData,

        [Parameter()]
        [ValidateSet('Binary','Dword','ExpandString','MultiString','Qword','String','None')]
        [System.String]
        $ValueType,

        [Parameter()]
        [System.String]
        $AccountName,

        [Parameter()]
        [ValidateSet('Present', 'Absent')]
        [System.String]
        $Ensure = 'Present'
    )

    $getTargetResourceParameters = @{
        Key         = $Key
        TargetType  = $TargetType
        ValueName   = $ValueName
        AccountName = $AccountName
    }

    $getTargetResourceResult = Get-TargetResource @getTargetResourceParameters
    $polFilePath = Get-RegistryPolicyFilePath -TargetType $TargetType -AccountName $AccountName
    $gpRegistryEntry = New-GPRegistryPolicy -Key $Key -ValueName $ValueName -ValueData $ValueData -ValueType ([GPRegistryPolicy]::GetRegTypeFromString($ValueType))

    if ($Ensure -eq 'Present')
    {
        if ($getTargetResourceResult.Ensure -eq 'Absent')
        {
            $assertPolFile = Test-Path -Path $polFilePath

            if ($assertPolFile -eq $false)
            {
                # create the pol file
                New-GPRegistryPolicyFile -Path $polFilePath
            }
        }
        # write the desired value
        Write-Verbose -Message ($script:localizedData.AddPolicyToFile -f $Key, $ValueName, $ValueData, $ValueType)
        Set-GPRegistryPolicyFileEntry -Path $polFilePath -RegistryPolicy $gpRegistryEntry
    }
    else
    {
        if ($getTargetResourceResult.Ensure -eq 'Present')
        {
            Write-Verbose -Message ($script:localizedData.RemovePolicyFromFile -f $Key, $ValueName)
            Remove-GPRegistryPolicyFileEntry -Path $polFilePath -RegistryPolicy $gpRegistryEntry
        }
    }

    # write the gpt.ini update
    $setGptIniFileParams = @{
        TargetType = $TargetType
    }
    if ($PSBoundParameters.ContainsKey('AccountName'))
    {
        $setGptIniFileParams.AccountName = $AccountName
    }

    Set-GptIniFile @setGptIniFileParams
    Set-RefreshRegistryKey
}

<#
    .SYNOPSIS
        Tests for the desired state of the policy key in the pol file.
 
    .PARAMETER Key
        Indicates the path of the registry key for which you want to ensure a specific state. This path must include the hive.
 
    .PARAMETER ValueName
        Indicates the name of the registry value.
 
    .PARAMETER ValueData
        The data for the registry value.
 
    .PARAMETER ValueType
        Indicates the type of the value.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the .pol file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
 
    .PARAMETER AccountName
        Specifies the name of the account for an user specific pol file to be managed.
 
    .PARAMETER Ensure
        Specifies the desired state of the registry policy. When set to 'Present', the registry policy will be created. When set to 'Absent', the registry policy will be removed. Default value is 'Present'.
#>

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Key,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ValueName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType,

        [Parameter()]
        [System.String[]]
        $ValueData,

        [Parameter()]
        [ValidateSet('Binary','Dword','ExpandString','MultiString','Qword','String','None')]
        [System.String]
        $ValueType,

        [Parameter()]
        [System.String]
        $AccountName,

        [Parameter()]
        [ValidateSet('Present', 'Absent')]
        [System.String]
        $Ensure = 'Present'
    )

    $getTargetResourceParameters = @{
        Key         = $Key
        TargetType  = $TargetType
        ValueName   = $ValueName
        AccountName = $AccountName
    }

    $testTargetResourceResult = $false

    $getTargetResourceResult = Get-TargetResource @getTargetResourceParameters

    if ($Ensure -eq 'Present')
    {
        $valuesToCheck = @(
            'Key'
            'ValueName'
            'TargetType'
            'ValueData'
            'ValueType'
            'Ensure'
        )

        $testTargetResourceResult = Test-DscParameterState -CurrentValues $getTargetResourceResult -DesiredValues $PSBoundParameters -ValuesToCheck $valuesToCheck
    }
    else
    {
        if ($Ensure -eq $getTargetResourceResult.Ensure)
        {
            Write-Verbose -Message ($script:localizedData.InDesiredState)
            $testTargetResourceResult = $true
        }
    }

    return $testTargetResourceResult
}

<#
    .SYNOPSIS
        Retrieves the path to the pol file.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the .pol file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
 
    .PARAMETER AccountName
        Specifies the name of the account for an user specific pol file to be managed.
#>

function Get-RegistryPolicyFilePath
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType,

        [Parameter()]
        [System.String]
        $AccountName
    )

    switch ($TargetType)
    {
        'ComputerConfiguration'
        {
            $childPath = 'System32\GroupPolicy\Machine\registry.pol'
        }
        'UserConfiguration'
        {
            $childPath = 'System32\GroupPolicy\User\registry.pol'
        }
        'Administrators'
        {
            $childPath = 'System32\GroupPolicyUsers\S-1-5-32-544\User\registry.pol'
        }
        'NonAdministrators'
        {
            $childPath = 'System32\GroupPolicyUsers\S-1-5-32-545\User\registry.pol'
        }
        'Account'
        {
            if ([System.String]::IsNullOrEmpty($AccountName))
            {
                throw $script:localizedData.AccountNameNull
            }

            $sid = ConvertTo-SecurityIdentifier -AccountName $AccountName
            $childPath = "System32\GroupPolicyUsers\$sid\User\registry.pol"
        }
    }

    return (Join-Path -Path $env:SystemRoot -ChildPath $childPath)
}

<#
    .SYNOPSIS
        Converts an identity to a SID to verify it's a valid account.
 
    .PARAMETER AccountName
        Specifies the identity to convert.
#>

function ConvertTo-SecurityIdentifier
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $AccountName
    )

    Write-Verbose -Message ($script:localizedData.TranslatingNameToSid -f $AccountName)
    $id = [System.Security.Principal.NTAccount] $AccountName

    return $id.Translate([System.Security.Principal.SecurityIdentifier]).Value
}

<#
    .SYNOPSIS
        Converts a SID to an NTAccount name.
 
    .PARAMETER SecurityIdentifier
        Specifies SID of the identity to convert.
#>

function ConvertTo-NTAccountName
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Security.Principal.SecurityIdentifier]
        $SecurityIdentifier
    )

    $identiy = [System.Security.Principal.SecurityIdentifier] $SecurityIdentifier

    return $identiy.Translate([System.Security.Principal.NTAccount]).Value
}

<#
    .SYNOPSIS
        Writes a registry key indicating a group policy refresh is required.
 
    .PARAMETER Path
        Specifies the value of the registry path that will contain the properties pertaining to requiring a refresh.
 
    .PARAMETER PropertyName
        Specifies a name for the new property.
 
    .PARAMETER Value
        Specifies the property value.
#>

function Set-RefreshRegistryKey
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [System.String]
        $Path = 'HKLM:\SOFTWARE\Microsoft\GPRegistryPolicy',

        [Parameter()]
        [System.String]
        $PropertyName = 'RefreshRequired',

        [Parameter()]
        [System.Object]
        $Value = 1
    )

    New-Item -Path $Path -Force
    New-ItemProperty -Path $Path -Name $PropertyName -Value $Value -Force
}

<#
    .SYNOPSIS
        Sets the gpt.ini file according to user/computer policy changes.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the gpt.ini file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
 
    .PARAMETER AccountName
        Specifies the name of the account for an user specific gpt.ini file to be managed.
#>

function Set-GptIniFile
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType,

        [Parameter()]
        [System.String]
        $AccountName
    )

    $registryPolicyPath = Split-Path -Path (Get-RegistryPolicyFilePath @PSBoundParameters) -Parent | Split-Path -Parent
    $gptIniPath = Join-Path -Path $registryPolicyPath -ChildPath 'gpt.ini'

    $extensionNamesPattern = '{35378EAC-683F-11D2-A89A-00C04FBBCFA2}.*{D02B1F7[2|3]-3407-48AE-BA88-E8213C6761F1}'
    $extensionHashtable = @{
        gPCMachineExtensionNames = '35378EAC-683F-11D2-A89A-00C04FBBCFA2', 'D02B1F72-3407-48AE-BA88-E8213C6761F1'
        gPCUserExtensionNames    = '35378EAC-683F-11D2-A89A-00C04FBBCFA2', 'D02B1F73-3407-48AE-BA88-E8213C6761F1'
    }

    # Detect gPCMachineExtensionNames/gPCUserExtensionNames presence and value
    foreach ($gPCItem in $extensionHashtable.Keys)
    {
        $gptEntry = Get-PrivateProfileString -AppName 'General' -KeyName $gPCItem -GptIniPath $gptIniPath
        if (-not ($gptEntry -match $extensionNamesPattern))
        {
            if ($gptEntry -ne [String]::Empty)
            {
                $gPCExistingValue = $gptEntry -replace '\[{|}]' -split '}{'
                $gPCNewValue = $gPCExistingValue + $extensionHashtable[$gPCItem] | Select-Object -Unique | Sort-Object
            }
            else
            {
                $gPCNewValue = $extensionHashtable[$gPCItem]
            }

            $formattedgPCNewValue = '[{{{0}}}]' -f $($gPCNewValue -join '}{')
            Write-Verbose -Message ($script:localizedData.GptIniCseUpdate -f $gPCItem, $gptEntry, $formattedgPCNewValue)
            Write-PrivateProfileString -AppName 'General' -KeyName $gPCItem -KeyValue $formattedgPCNewValue -GptIniPath $gptIniPath
        }

        <#
            To ensure consistent gpt.ini file structure, querying Version and setting Version so the structure will be:
            gPC[User|Machine]ExtensionName = [{guids}]
            Version = 11111
            gPC[User|Machine]ExtensionName = [{guids}]
        #>

        $gptVersion = Get-PrivateProfileString -AppName 'General' -KeyName 'Version' -Default 0 -GptIniPath $gptIniPath
        Write-PrivateProfileString -AppName 'General' -KeyName 'Version' -KeyValue $gptVersion -GptIniPath $gptIniPath
    }

    # Determine incremented version number
    $newGptVersion = Get-IncrementedGptVersion -TargetType $TargetType -Version $gptVersion

    # Write incremented version to GPT
    Write-Verbose -Message ($script:localizedData.GptIniVersionUpdate -f $TargetType, $gptVersion, $newGptVersion)
    Write-PrivateProfileString -AppName 'General' -KeyName 'Version' -KeyValue $newGptVersion -GptIniPath $gptIniPath
}

<#
    .SYNOPSIS
        Queries an ini file for specific information.
 
    .PARAMETER AppName
        The name of the section containing the key name in an ini file, also known as 'Section'.
 
    .PARAMETER KeyName
        The name of the key whose associated string is to be retrieved.
 
    .PARAMETER Default
        If the KeyName key cannot be found in the initialization file, GetPrivateProfileString
        copies the default string to the ReturnedString buffer.
 
    .PARAMETER GptIniPath
        Path to the gpt.ini file to be queried.
#>

function Get-PrivateProfileString
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter()]
        [System.String]
        $AppName = 'General',

        [Parameter(Mandatory = $true)]
        [System.String]
        $KeyName,

        [Parameter()]
        [System.String]
        $Default,

        [Parameter(Mandatory = $true)]
        [System.String]
        $GptIniPath
    )

    # The GetPrivateProfileString method requires a FileSystem path, meaning no PSDrive paths
    $fullyQualifiedFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($GptIniPath)

    $stringBuilder = [System.Text.StringBuilder]::new(65535)

    [void][GPRegistryPolicyDsc.IniUtility]::GetPrivateProfileString(
        $AppName,
        $KeyName,
        $Default,
        $stringBuilder,
        $stringBuilder.Capacity,
        $fullyQualifiedFilePath
    )

    return $stringBuilder.ToString()
}

<#
    .SYNOPSIS
        Writes information to an ini file.
 
    .PARAMETER AppName
        The name of the section containing the key name in an ini file, also known as 'Section'.
 
    .PARAMETER KeyName
        The name of the key whose associated KeyValue string is to be written/modified.
 
    .PARAMETER KeyValue
        A null-terminated string to be written to the file.
 
    .PARAMETER GptIniPath
        Path to the gpt.ini file to be written/modified.
#>

function Write-PrivateProfileString
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter()]
        [System.String]
        $AppName = 'General',

        [Parameter(Mandatory = $true)]
        [System.String]
        $KeyName,

        [Parameter(Mandatory = $true)]
        [System.String]
        $KeyValue,

        [Parameter(Mandatory = $true)]
        [System.String]
        $GptIniPath
    )

    # The WritePrivateProfileString method requires a FileSystem path, meaning no PSDrive paths
    $fullyQualifiedFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($GptIniPath)

    [void][GPRegistryPolicyDsc.IniUtility]::WritePrivateProfileString(
        $AppName,
        $KeyName,
        $KeyValue,
        $fullyQualifiedFilePath
    )
}

<#
    .SYNOPSIS
        Determines the incremented version number from the specified gpt.ini file.
 
    .PARAMETER Version
        The current gpt.ini version number which will be incremented based on TargetType.
 
    .PARAMETER TargetType
        Indicates the target type. This is needed to determine the gpt.ini file path. Supported values are LocalMachine, User, Administrators, NonAdministrators, Account.
#>

function Get-IncrementedGptVersion
{
    [CmdletBinding()]
    [OutputType([System.Int32])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Version,

        [Parameter(Mandatory = $true)]
        [ValidateSet('ComputerConfiguration','UserConfiguration','Administrators','NonAdministrators','Account')]
        [System.String]
        $TargetType
    )

    <#
        Reference: https://docs.microsoft.com/en-us/archive/blogs/grouppolicy/understanding-the-gpo-version-number
        The version integer value in the GPT.ini has the following structure:
            Version = [user version number top 16 bits] [computer version number lower 16 bits]
        Below is a simple way to split the version number into the user and computer version number:
            * First, recognize that the version number is in decimal. Before we can split the number into the two version numbers,
              we first convert the decimal value to hex. The easiest way to perform this conversion is to use the calculator in windows
              in scientific mode. Enter the decimal value and then click the hex button to convert the number. You should see a value of 15002F.
            * If you are using the calculator, it will not display the leading zeros of the number. In hexadecimal, four hexadecimal characters
              are equal to 16 bits. When you split the number into two parts you'll need to add two leading zeros to show the full version number
              in hexadecimal. For our case, I would write this number out as 0015002F. (When written on paper, a 0x is added to the beginning of
              the number to clarify the number is hexadecimal, 0x0015002F.)
            * Input the lower 4 hex characters (002F) into the calculator while in hex mode. Then convert this value to decimal by clicking the
              decimal button. You should see a computer version number of 47 decimal.
            * Input the upper 4 hex characters (0015) into the calculator while in hex mode. Then convert this value to decimal by clicking the
              decimal button. You should see a user version number of 21 decimal.
    #>


    # Increment gpt.ini version number based on user or computer policy change.
    $versionBytes = [System.BitConverter]::GetBytes([int]$Version)
    $loVersion    = [System.BitConverter]::ToUInt16($versionBytes, 0)
    $hiVersion    = [System.BitConverter]::ToUInt16($versionBytes, 2)

    if ($TargetType -eq 'ComputerConfiguration')
    {
        if ($loVersion -eq [uint16]::MaxValue)
        {
            # Once the GPT version hits the uint16 max (65535), the incremented number is reset to 1
            $loVersion = 1
        }
        else
        {
            $loVersion++
        }
    }
    else
    {
        if ($hiVersion -eq [uint16]::MaxValue)
        {
            # Once the GPT version hits the uint16 max (65535), the incremented number is reset to 1
            $hiVersion = 1
        }
        else
        {
            $hiVersion++
        }
    }

    # Convert lo/hi to byte array
    $loVersionByte = [System.BitConverter]::GetBytes($loVersion)
    $hiVersionByte = [System.BitConverter]::GetBytes($hiVersion)

    # Create new byte array and convert to int32
    $newGptVersionBytes = [byte[]]::new(4)
    $newGptVersionBytes[0] = $loVersionByte[0]
    $newGptVersionBytes[1] = $loVersionByte[1]
    $newGptVersionBytes[2] = $hiVersionByte[0]
    $newGptVersionBytes[3] = $hiVersionByte[1]

    return [System.BitConverter]::ToInt32($newGptVersionBytes, 0)
}