<# MoonChromeStore 環境設定 1. ExtensionInstallSources:允許從 Store 網址安裝擴充功能。 2. ExtensionInstallAllowlist:把 Store 上每個工具的 Extension ID 加入白名單。 Windows 上不在 Web Store 的擴充功能,必須被政策「明確允許」,否則安裝後會被 Chrome 以「無法驗證來源」停用。ID 從 Store 的 catalog.json 讀取。 需要以系統管理員身分執行。Store 上架新工具後,再執行一次即可把新 ID 加入白名單。 用法:powershell -ExecutionPolicy Bypass -File install-policy.ps1 [-Origin http://localhost:8787] #> param( [string]$Origin = 'http://localhost:8787' ) $ErrorActionPreference = 'Stop' $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host '請以「系統管理員身分」開啟 PowerShell 後再執行這個腳本。' -ForegroundColor Red exit 1 } $Origin = $Origin.TrimEnd('/') $pattern = "$Origin/*" # 記錄由 MoonChromeStore 加入的 ID,移除時只移除這些,不動 IT 原有的白名單 $trackKey = 'HKLM:\SOFTWARE\MoonChromeStore' # 清單型政策:值名稱必須是 1、2、3… 連續編號。找第一個空號寫入,已存在就略過。 function Add-ListPolicyValue([string]$Key, [string]$Value) { if (-not (Test-Path $Key)) { New-Item -Path $Key -Force | Out-Null } $props = @((Get-ItemProperty $Key).PSObject.Properties | Where-Object { $_.Name -match '^\d+$' }) if ($props.Value -contains $Value) { return $false } $i = 1 while ($props.Name -contains "$i") { $i++ } New-ItemProperty -Path $Key -Name "$i" -Value $Value -PropertyType String -Force | Out-Null return $true } Write-Host "Store:$Origin" # 前置檢查:電腦必須加入網域 if (-not (Get-CimInstance Win32_ComputerSystem).PartOfDomain) { Write-Host '警告:這台電腦沒有加入 AD 網域,Chrome 可能會停用從 Store 安裝的擴充功能。' -ForegroundColor Yellow } # 讀取 Store 目前的工具清單 $ids = @() try { $catalog = Invoke-RestMethod -Uri "$Origin/catalog.json" -UseBasicParsing -TimeoutSec 15 $ids = @($catalog.extensions | ForEach-Object { $_.id } | Where-Object { $_ -match '^[a-p]{32}$' }) Write-Host "Store 上共有 $($ids.Count) 個工具。" } catch { Write-Host "警告:無法讀取 $Origin/catalog.json($($_.Exception.Message)),這次只設定安裝來源,不更新白名單。" -ForegroundColor Yellow } $browsers = @( @{ Name = 'Chrome'; Key = 'HKLM:\SOFTWARE\Policies\Google\Chrome' }, @{ Name = 'Edge'; Key = 'HKLM:\SOFTWARE\Policies\Microsoft\Edge' } ) foreach ($b in $browsers) { # 全域黑名單提示 foreach ($root in @($b.Key, $b.Key.Replace('HKLM:', 'HKCU:'))) { $bl = Join-Path $root 'ExtensionInstallBlocklist' if ((Test-Path $bl) -and (((Get-ItemProperty $bl).PSObject.Properties | Where-Object { $_.Name -match '^\d+$' }).Value -contains '*')) { Write-Host "提示:$($b.Name) 有全域黑名單($bl = *),白名單裡的工具仍可安裝,其他擴充功能會被擋。" -ForegroundColor Yellow } } if (Add-ListPolicyValue (Join-Path $b.Key 'ExtensionInstallSources') $pattern) { Write-Host "$($b.Name):已加入安裝來源 $pattern" -ForegroundColor Green } else { Write-Host "$($b.Name):安裝來源已設定過。" } $added = 0 foreach ($id in $ids) { if (Add-ListPolicyValue (Join-Path $b.Key 'ExtensionInstallAllowlist') $id) { $added++ } } if ($ids.Count) { Write-Host "$($b.Name):白名單新增 $added 個 ID(已存在的略過)。" -ForegroundColor Green } } # 記錄這次管理的 ID 與 Store 網址,供 remove-policy.ps1 使用 if ($ids.Count -or -not (Test-Path $trackKey)) { if (-not (Test-Path $trackKey)) { New-Item -Path $trackKey -Force | Out-Null } $prev = @((Get-ItemProperty $trackKey -ErrorAction SilentlyContinue).ManagedIds) $all = @($prev + $ids | Where-Object { $_ } | Sort-Object -Unique) New-ItemProperty -Path $trackKey -Name 'ManagedIds' -Value $all -PropertyType MultiString -Force | Out-Null New-ItemProperty -Path $trackKey -Name 'Origin' -Value $Origin -PropertyType String -Force | Out-Null } Write-Host '' Write-Host '設定完成!請完全關閉並重新開啟 Chrome / Edge。' -ForegroundColor Green Write-Host '可在 chrome://policy 確認 ExtensionInstallSources 與 ExtensionInstallAllowlist 是否生效。'