From 0bc50328ee8ee1dd85c1c837749fd64e36ce3b0c Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Mon, 4 Jan 2021 21:47:26 +0100 Subject: [PATCH] solves issue when gpLink is empty --- Public/Repair-GPOZaurrBrokenLink.ps1 | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Public/Repair-GPOZaurrBrokenLink.ps1 b/Public/Repair-GPOZaurrBrokenLink.ps1 index b647bb6..13c99ce 100644 --- a/Public/Repair-GPOZaurrBrokenLink.ps1 +++ b/Public/Repair-GPOZaurrBrokenLink.ps1 @@ -71,11 +71,20 @@ function Repair-GPOZaurrBrokenLink { } if ($Found) { $NewGpLink = $($FixedLinks -join '') - try { - Write-Verbose "Repair-GPOZaurrBrokenLink - setting gpLink to $Key - $NewGPLink" - Set-ADObject -Identity $Key -Server $Server -Replace @{ gPLink = $NewGpLink } -ErrorAction Stop - } catch { - Write-Warning "Repair-GPOZaurrBrokenLink - couldn't replace gpLink at $Key with $NewGpLink" + if ($NewGpLink) { + try { + Write-Verbose "Repair-GPOZaurrBrokenLink - setting gpLink to $Key - $NewGPLink" + Set-ADObject -Identity $Key -Server $Server -Replace @{ gPLink = $NewGpLink } -ErrorAction Stop + } catch { + Write-Warning "Repair-GPOZaurrBrokenLink - setting gpLink to $Key - $NewGpLink failed! Error $($_.Exception.Message)" + } + } else { + try { + Write-Verbose "Repair-GPOZaurrBrokenLink - clearing gpLink for $Key (no other links)" + Set-ADObject -Identity $Key -Server $Server -Clear gPLink -ErrorAction Stop + } catch { + Write-Warning "Repair-GPOZaurrBrokenLink - clearing gpLink for $Key failed! Error $($_.Exception.Message)" + } } if ($LimitProcessing -eq $Count) { break