签署脚本

使用 Set-AuthenticodeSignature-cmdlet 和代码签名证书签署脚本。

#Get the first available personal code-signing certificate for the logged on user
$cert = @(Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert)[0]
    
#Sign script using certificate
Set-AuthenticodeSignature -Certificate $cert -FilePath c:\MyScript.ps1

你还可以使用以下方法从 .pfx 文件中读取证书:

$cert = Get-PfxCertificate -FilePath "C:\MyCodeSigningCert.pfx"

该脚本将在有效期到期之前有效。如果在签名期间使用时间戳服务器,则证书过期后脚本将继续有效。添加证书的信任链(包括根权限)以帮助大多数计算机信任用于签署脚本的证书也很有用。

Set-AuthenticodeSignature -Certificate $cert -FilePath c:\MyScript.ps1 -IncludeChain All -TimeStampServer "http://timestamp.verisign.com/scripts/timstamp.dll"

建议使用来自 Verisign,Comodo,Thawte 等受信任证书提供商的时间戳服务器。