#Requires -Version 5.1 <# 把自签名代码签名证书导入当前用户的「受信任的根证书颁发机构」与「受信任的发布者」。 只影响当前用户、不需要管理员:导入后本机运行我们签名过的 exe 不再提示「未知发布者」。 换一台机器要再导一次;-Remove 可撤销。 用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1 [-CerPath <证书>] [-Remove] 为什么用 certutil 而不是 Import-Certificate:后者向 Root 存储导入要求交互式 UI 确认, 在脚本里直接报 "UI is not allowed in this operation";certutil -user -addstore 是静默的。 #> [CmdletBinding()] param( [string]$CerPath, [switch]$Remove ) $ErrorActionPreference = 'Stop' if (-not $CerPath) { $CerPath = Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) 'certs\ZeroOrigin-CodeSign.cer' } if (-not (Test-Path -LiteralPath $CerPath)) { "找不到证书文件:$CerPath" exit 1 } $full = (Resolve-Path -LiteralPath $CerPath).Path $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($full) $thumb = $cert.Thumbprint foreach ($store in 'Root', 'TrustedPublisher') { if ($Remove) { $proc = Start-Process certutil -ArgumentList @('-user', '-delstore', $store, $thumb) -PassThru -NoNewWindow -Wait } else { $proc = Start-Process certutil -ArgumentList @('-user', '-addstore', $store, $full) -PassThru -NoNewWindow -Wait } if ($proc.ExitCode -ne 0) { "操作失败:$store(certutil 退出码 $($proc.ExitCode))" exit 1 } } if ($Remove) { "已移除:$($cert.Subject)($thumb)" } else { "已信任:$($cert.Subject)($thumb)" "到期:$($cert.NotAfter.ToString('yyyy-MM-dd'))" } exit 0