#Requires -Version 5.1 <# 用本机自签名证书给一个 PE 文件做 Authenticode 签名。 mygo.json 的 windows.signCommand 指向本脚本:mygo build 会给主 exe、资源里的 watchdog.exe、安装包和卸载器逐个调用它(%1 是文件路径,已带引号)。 自签名证书没有 CA 时间戳服务,所以不加 RFC 3161 时间戳:证书到期后旧签名会失效, 到那时重新发证并重签即可(自用场景够)。 用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\sign-file.ps1 <文件> #> [CmdletBinding()] param( [Parameter(Mandatory = $true, Position = 0)][string]$Path, [string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN' ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Path)) { "签名失败:文件不存在 $Path" exit 1 } $cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey } | Sort-Object NotAfter -Descending | Select-Object -First 1 if (-not $cert) { "签名失败:找不到证书「$Subject」,先运行 scripts\codesign\new-cert.ps1" exit 1 } $leaf = Split-Path -Leaf $Path $existing = Get-AuthenticodeSignature -LiteralPath $Path if ($existing.SignerCertificate) { "已签名,跳过:$leaf" exit 0 } try { $sig = Set-AuthenticodeSignature -LiteralPath $Path -Certificate $cert -HashAlgorithm SHA256 } catch { "签名失败:$leaf — $($_.Exception.Message)" exit 1 } if (-not $sig.SignerCertificate) { "签名失败:$leaf — $($sig.StatusMessage)" exit 1 } "已签名:$leaf($($sig.Status))" exit 0