ConvertFrom-GphGpVersion.ps1

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
#requires -Version 2.0
function ConvertFrom-GphGpVersion
{
  <#
      .SYNOPSIS
      Splits the Combined Group Policy Version Number into its User- and Computer-Part
 
      .Description
      Each time a Group Policy is changed, the Group Policy Version Number is also changed. The Number ist a 32-Bit Value, consisting
      of two parts. The Lower 16 Bits code is the Computer Version, the upper 16 Bits are the User Version of the GPO.
      A good description of the Group Policy Version Numbers can be found at
      https://blogs.technet.microsoft.com/grouppolicy/2007/12/14/understanding-the-gpo-version-number/.
      ConvertFrom-GphGpVersion returns the split version number.
 
      .EXAMPLE
      ConvertFrom-GPVersion -VersionNumber 65537
      converts the 32-Bit Represenation of the Version-Number into a 16-Bit Value
 
      .NOTES
      Group-Policys Version-Number for Computer- and Userversion are represented in one 32-Bit Value. This Function converts
      the number back into a 16-Bit Value.
  #>



  
  [CmdletBinding()]
  param(
    # The Group Policy Version Number as it is written to gpt.ini, the Group Policy History in the Registry or the Active Directory
    [Parameter(Mandatory=$true,
        ValueFromPipelinebyPropertyName=$true,
    ValueFromPipeline=$true)]
    [ValidateRange(0,4294967295)]
    [int]$VersionNumber,

    [switch]$Passthru
  )
  
  Begin{
    [regex]$regex = '(\d{16})?'
  }

  process
  {
    # Konvertierung ins Binärsystem mit ToString(Zahl,Zahlsystem)
    $VersionBinary = ([convert]::ToString($VersionNumber,2)).padleft(32,'0')
    Write-Verbose -Message $VersionBinary
    $Versions = $regex.Matches($VersionBinary)
    $versionList = @{
        ComputerVersion = [Convert]::ToUInt64($Versions[1],2) 
        UserVersion = [Convert]::ToUInt64($Versions[0],2)
    }
    If ( $Passthru )
    { add-member -InputObject }
    New-Object -TypeName PSCustomObject -Property $versionList 
  }
}