Get-GPOBackupInfoFromXml.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
function Get-GPOBackupInfoFromXml {
  <#
      .SYNOPSIS
      Gets Information about GPO-Backups from a GPO Backup Directory.
 
      .DESCRIPTION
      If you want to find out which backups are in a dedicated Group Policy Backup Folder
      and when they were taken, this functions returns all the relevant Information.
 
      .EXAMPLE
      Get-GPOBackupInfoFromXml -backuppath c:\gpobackup
      Parses all backup.xml-files in the directory and returns Infomation about the Backups.
  #>


  [CmdletBinding()]
  param(
    # The Path cotaining the GPO-Backups
    [parameter(mandatory=$true,
               position=0,
               ValueFromPipelineByPropertyName=$true)]
    [ValidateScript({ Test-path -Path $_ -PathType Container })]
    [Alias('Fullname')]
    [string]$backuppath
  )

  process {  $backupfiles = get-childitem -Path $backuppath\backup.xml -Recurse

    Foreach ( $BackupFileItem in $backupfiles )
    {  
      [xml]$backupinfo = get-content -Path $BackupFileItem.Fullname -ReadCount 0 -Encoding UTF8
      $comment = Get-Content -Path ( join-path -Path $BackupFileItem.Directory.FullName -ChildPath 'DomainSysvol\gpo\GPO.cmt' ) -ErrorAction SilentlyContinue -Encoding Unicode
      $gpoProperties = [ordered]@{
        BackupTime = ( get-date -Date $BackupFileItem.CreationTime -Format g )
        name = $backupinfo.GroupPolicyBackupScheme.GroupPolicyObject.GroupPolicyCoreSettings.DisplayName.InnerText
        GPOGUID = $backupinfo.GroupPolicyBackupScheme.GroupPolicyObject.GroupPolicyCoreSettings.ID.InnerText
        BackupGUID = $BackupFileItem.Directory.Name
        Options = $backupinfo.GroupPolicyBackupScheme.GroupPolicyObject.GroupPolicyCoreSettings.Options.InnerText
        UserVersion = (ConvertFrom-GphGpVersion -VersionNumber ( $backupinfo.GroupPolicyBackupScheme.GroupPolicyObject.GroupPolicyCoreSettings.UserVersionNumber.InnerText.ToString())).UserVersion
        MachineVersion = (ConvertFrom-GphGpVersion -VersionNumber ( $backupinfo.GroupPolicyBackupScheme.GroupPolicyObject.GroupPolicyCoreSettings.MachineVersionNumber.InnerText.ToString())).ComputerVersion
        Comment = $comment
      }
      $GPO = New-Object -TypeName PSCustomObject -Property $GPOProperties
      $GPO
    }
  } 
}