Improve Remove-GPOZaurrBroken

This commit is contained in:
Przemyslaw Klys
2020-11-18 18:42:43 +01:00
parent 5fb48b994b
commit 9cd28b11ea
+16 -15
View File
@@ -33,42 +33,43 @@
} }
} }
} | Select-Object | Select-Object -First $LimitProcessing | ForEach-Object { } | Select-Object | Select-Object -First $LimitProcessing | ForEach-Object {
if ($_.Status -eq 'Not available in AD') { $GPO = $_
Write-Verbose "Remove-GPOZaurrBroken - Processing $($_.Path)" if ($GPO.Status -eq 'Not available in AD') {
Write-Verbose "Remove-GPOZaurrBroken - Processing $($GPO.Path)"
if ($BackupFinalPath) { if ($BackupFinalPath) {
Try { Try {
Write-Verbose "Remove-GPOZaurrBroken - Backing up $($_.Path)" Write-Verbose "Remove-GPOZaurrBroken - Backing up $($GPO.Path)"
Copy-Item -LiteralPath $_.Path -Recurse -Destination $BackupFinalPath -ErrorAction Stop Copy-Item -LiteralPath $GPO.Path -Recurse -Destination $BackupFinalPath -ErrorAction Stop
$BackupWorked = $true $BackupWorked = $true
} catch { } catch {
Write-Warning "Remove-GPOZaurrBroken - Error backing up error: $($_.Exception.Message)" Write-Warning "Remove-GPOZaurrBroken - Error backing up $($GPO.Path) error: $($_.Exception.Message)"
$BackupWorked = $false $BackupWorked = $false
} }
} }
if ($BackupWorked -or $BackupFinalPath -eq '') { if ($BackupWorked -or $BackupFinalPath -eq '') {
Write-Verbose "Remove-GPOZaurrBroken - Deleting $($_.Path)" Write-Verbose "Remove-GPOZaurrBroken - Deleting $($GPO.Path)"
try { try {
Remove-Item -Recurse -Force -LiteralPath $_.Path Remove-Item -Recurse -Force -LiteralPath $GPO.Path -ErrorAction Stop
} catch { } catch {
Write-Warning "Remove-GPOZaurrBroken - Failed to remove file $($_.Path): $($_.Exception.Message)." Write-Warning "Remove-GPOZaurrBroken - Failed to remove file $($GPO.Path): $($_.Exception.Message)."
} }
} }
} elseif ($_.Status -eq 'Not available on SYSVOL') { } elseif ($GPO.Status -eq 'Not available on SYSVOL') {
try { try {
$ExistingObject = Get-ADObject -Identity $_.DistinguishedName -Server $_.DomainName -ErrorAction Stop $ExistingObject = Get-ADObject -Identity $GPO.DistinguishedName -Server $GPO.DomainName -ErrorAction Stop
} catch { } catch {
Write-Warning "Remove-GPOZaurrBroken - Error getting $($_.DistinguishedName) from AD error: $($_.Exception.Message)" Write-Warning "Remove-GPOZaurrBroken - Error getting $($GPO.DistinguishedName) from AD error: $($_.Exception.Message)"
$ExistingObject = $null $ExistingObject = $null
} }
if ($ExistingObject -and $ExistingObject.ObjectClass -eq 'groupPolicyContainer') { if ($ExistingObject -and $ExistingObject.ObjectClass -eq 'groupPolicyContainer') {
Write-Verbose "Remove-GPOZaurrBroken - Removing DN: $($_.DistinguishedName) / ObjectClass: $($ExistingObject.ObjectClass)" Write-Verbose "Remove-GPOZaurrBroken - Removing DN: $($GPO.DistinguishedName) / ObjectClass: $($ExistingObject.ObjectClass)"
try { try {
Remove-ADObject -Server $_.DomainName -Identity $_.DistinguishedName -Recursive -Confirm:$false Remove-ADObject -Server $GPO.DomainName -Identity $GPO.DistinguishedName -Recursive -Confirm:$false -ErrorAction Stop
} catch { } catch {
Write-Warning "Remove-GPOZaurrBroken - Failed to remove $($_.DistinguishedName) from AD error: $($_.Exception.Message)" Write-Warning "Remove-GPOZaurrBroken - Failed to remove $($GPO.DistinguishedName) from AD error: $($_.Exception.Message)"
} }
} else { } else {
Write-Warning "Remove-GPOZaurrBroken - DistinguishedName $($_.DistinguishedName) not found or ObjectClass is not groupPolicyContainer ($($ExistingObject.ObjectClass))" Write-Warning "Remove-GPOZaurrBroken - DistinguishedName $($GPO.DistinguishedName) not found or ObjectClass is not groupPolicyContainer ($($ExistingObject.ObjectClass))"
} }
} }
} }