Skip to content

Commit 6c11344

Browse files
authored
Merge pull request #8753 from Deland-Han/cmpy-branch-ci5406
AB#5406: Scripts: Script to check the AGPM archive for inconsisencies
2 parents a042abe + ef5bad3 commit 6c11344

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Scripts to Check and Clean the AGPM Archive That Causes GPO Operation Issues
3+
description: Introduces a script to check and clean up the AGPM archive that might lead to GPO operation issues.
4+
ms.date: 04/25/2025
5+
manager: dcscontentpm
6+
audience: itpro
7+
ms.topic: troubleshooting
8+
ms.reviewer: garymu
9+
ms.custom:
10+
- sap:group policy\group policy management (gpmc or gpedit)
11+
- pcy:WinComm Directory Services
12+
---
13+
# Scripts: Check and clean up the AGPM archive that might lead to GPO operation issues
14+
15+
_Applies to:_   Advanced Group Policy Manager 4.0 SP3
16+
17+
The sample script included in this article can assist with archive inconsistencies that lead to errors in managing Group Policy Objects (GPOs) within the tool.
18+
19+
Examples of errors that can occur if there's inconsistent information in the **gpostate.xml** file:
20+
21+
```output
22+
Create GPO: Test...Failed.
23+
[Error] The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
24+
--------------------------------------------------------------------------------------------------------
25+
1 actions failed.
26+
```
27+
28+
## Script
29+
30+
Here's what the script does:
31+
32+
1. Stops the Advanced Group Policy Management (AGPM) service.
33+
2. Scans the **gpostate.xml** file and removes references (if any) to archive nonexistent or incomplete GUID folders.
34+
3. Renames the **gpostate.xml** file with a timestamp if any changes are detected and saves a new **gpostate.xml** file.
35+
4. Starts the AGPM service.
36+
37+
[!INCLUDE [Script disclaimer](../../includes/script-disclaimer.md)]
38+
39+
```powershell
40+
Write-Host "Stopping AGPM Service"
41+
Stop-Service "AGPM Service" -ErrorAction Stop
42+
$AGPMArchivePath = Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Microsoft\AGPM" -Name "ArchivePath" -ErrorAction Stop
43+
$AGPMFile =$AGPMArchivePath + "gpostate.xml"
44+
[xml]$AGPMArchive = Get-Content -Path $AGPMFile -ErrorAction Stop
45+
$bChangesMade = $false
46+
foreach( $GPODomain in $AGPMArchive.Archive.GPODomain )
47+
{
48+
Write-Host "Processing archive information for domain: $($GPODomain.domain)"
49+
$ArchiveGPO = if( $GPODomain.GPO -is [array] ){ $GPODomain.GPO[0] } else { $GPODomain.GPO }
50+
While( $ArchiveGPO -ne $null )
51+
{
52+
$TempGPONext = $ArchiveGPO.NextSibling
53+
Write-Host "Checking GPO $($ArchiveGPO.id)"
54+
if( $ArchiveGPO.state.archiveId -ne $null ){
55+
$TestArchivePath = $AGPMArchivePath + $ArchiveGPO.state.archiveId
56+
if( -not (Test-Path $TestArchivePath ) )
57+
{
58+
Write-Host "$($ArchiveGPO.state.archiveId) is not in archive - Removing"
59+
$ArchiveGPO.ParentNode.RemoveChild($ArchiveGPO) > $null
60+
$bChangesMade = $true
61+
}
62+
}
63+
else
64+
{
65+
$ArchiveGPOHistoryItem = $ArchiveGPO.History.FirstChild
66+
While( $ArchiveGPOHistoryItem -ne $null )
67+
{
68+
$TempNext = $ArchiveGPOHistoryItem.NextSibling
69+
$TestArchivePath = $AGPMArchivePath + $ArchiveGPOHistoryItem.archiveId
70+
if( -not (Test-Path -Path $TestArchivePath) )
71+
{
72+
Write-Host "History '$($ArchiveGPOHistoryItem.archiveId)' for State '$($ArchiveGPOHistoryItem.state)' on '$($ArchiveGPOHistoryItem.time)' is not in archive - Removing"
73+
$ArchiveGPOHistoryItem.ParentNode.RemoveChild($ArchiveGPOHistoryItem) > $null
74+
$bChangesMade = $true
75+
}
76+
elseif( -not (Test-Path -Path ($TestArchivePath + "\bkupinfo.xml") ) )
77+
{
78+
Write-Host "'$($ArchiveGPOHistoryItem.archiveId)' does not have bkupinfo.xml - Removing"
79+
$ArchiveGPOHistoryItem.ParentNode.RemoveChild($ArchiveGPOHistoryItem) > $null
80+
$bChangesMade = $true
81+
}
82+
$ArchiveGPOHistoryItem = $TempNext
83+
}
84+
if( -not $ArchiveGPO.History.HasChildNodes )
85+
{
86+
Write-Host "GPO $($ArchiveGPO.id) has no History removing."
87+
$ArchiveGPO.ParentNode.RemoveChild($ArchiveGPO) > $null
88+
$bChangesMade = $true
89+
}
90+
}
91+
$ArchiveGPO = $TempGPONext
92+
}
93+
}
94+
if( $bChangesMade )
95+
{
96+
$BackupFileName = "gpostate\_bak\_$((Get-Date).ToString('yyyymmdd-hhmmss')).xml"
97+
Write-Host "Backing up gpostate.xml file to $BackupFileName"
98+
Move-Item -Path $AGPMFile -Destination ($AGPMArchivePath + $BackupFileName) -Force -ErrorAction Stop
99+
Write-Host "Saving updates"
100+
$AGPMArchive.Save($AGPMArchivePath + "gpostate.xml")
101+
}
102+
else
103+
{
104+
Write-Host "No Changes made."
105+
}
106+
Write-Host "Starting AGPM service."
107+
Start-Service "AGPM Service"
108+
```

support/windows-server/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,6 +3145,8 @@ items:
31453145
href: ./support-tools/scripts-retrieve-profile-age-delete-aged-copies.md
31463146
- name: Scripts to view the certificate information in the msDS-KeyCredentialLink attribute
31473147
href: ./support-tools/script-to-view-msds-keycredentiallink-attribute-value.md
3148+
- name: 'Scripts to check and clean up the AGPM archive'
3149+
href: support-tools/scripts-check-and-cleanup-the-agpm-archive.md
31483150
- name: TroubleShootingScript toolset (TSS)
31493151
items:
31503152
- name: Introduction to TroubleShootingScript toolset (TSS)

0 commit comments

Comments
 (0)