# Remediate-ChromeLocalNetworkAccessAllowedForUrls.ps1 $RequiredUrls = @( "https://companya-my.sharepoint.com" "https://companya.sharepoint.com" "https://companyb.sharepoint.com" ) $RegPath = "HKLM:\SOFTWARE\Policies\Google\Chrome\LocalNetworkAccessAllowedForUrls" try { # Ensure key exists if (-not (Test-Path $RegPath)) { New-Item -Path $RegPath -Force | Out-Null } # Read existing numeric values $item = Get-ItemProperty -Path $RegPath -ErrorAction SilentlyContinue $numericMap = @{} foreach ($p in $item.PSObject.Properties) { if ($p.Name -match '^\d+$') { $numericMap[[int]$p.Name] = ($p.Value -as [string]) } } # Build list of existing URLs $existingUrls = @() foreach ($v in $numericMap.Values) { if ($null -ne $v -and $v.Trim().Length -gt 0) { $existingUrls += $v.Trim() } } # Determine next free index ONCE, then increment $nextIndex = 1 while ($numericMap.ContainsKey($nextIndex)) { $nextIndex++ } $added = @() foreach ($url in $RequiredUrls) { if ($existingUrls -contains $url) { continue } New-ItemProperty ` -Path $RegPath ` -Name $nextIndex.ToString() ` -PropertyType String ` -Value $url ` -Force | Out-Null # Reserve index immediately $numericMap[$nextIndex] = $url $existingUrls += $url $added += "$nextIndex=$url" # Increment for the next addition $nextIndex++ while ($numericMap.ContainsKey($nextIndex)) { $nextIndex++ } } if ($added.Count -gt 0) { Write-Output ("Remediated: added " + ($added -join ", ")) } else { Write-Output "No remediation needed" } # exit 0 } catch { Write-Output ("Remediation failed: " + $_.Exception.Message) #exit 1 }