我正在尝试签署一份.ps1
使用自签名证书(用例是我自己在私人开发站上编写的脚本,因此无需使用 - 或付费 -
真正的 CA)。但是,无论我阅读多少关于证书生成和数字签名主题的指南,我似乎都无法使其正常工作。
这是我到目前为止所取得的成就:
# Create a certificate to use as trusted root of the signing chain
$root = New-SelfSignedCertificate `
-Subject "CN=PowerShell Trusted Authority" `
-FriendlyName "PowerShell Trusted Authority" `
-KeyUsageProperty Sign `
-KeyUsage CertSign, CRLSign, DigitalSignature `
-CertStoreLocation Cert:\LocalMachine\My\ `
-NotAfter (Get-Date).AddYears(10)
# Create a certificate to use for signing powershell scripts
New-SelfSignedCertificate `
-Signer $root `
-Subject "CN=PowerShell Code Signing" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-Type CodeSigningCert `
-CertStoreLocation Cert:\LocalMachine\My\
# Move the root cert into Trusted Root CAs
Move-Item "Cert:\LocalMachine\My\$($root.Thumbprint)" Cert:\LocalMachine\Root
上述所有操作都是通过管理 powershell 实例完成的。完成后,我可以在管理控制台的预期位置看到两个证书,并且签名证书的证书路径检查为有效。
然后,我打开常规 PS 提示符并尝试签署脚本:
# Obtain a reference to the signing certificate
PS> $cert = Get-ChildItem Cert:\LocalMachine\My\ -CodeSigningCert
# Attempt at signing
PS> Set-AuthenticodeSignature .\Microsoft.PowerShell_profile.ps1 $cert
Directory: C:\Users\tomas\Documents\WindowsPowerShell
SignerCertificate Status Path
----------------- ------ ----
UnknownError Microsoft.PowerShell_profile.ps1
如您所见,实际签名失败。查看 powershell 文件,我发现脚本中没有附加任何签名。
如果我在管理员提示下进行签名,我似乎会更进一步;签名块添加到脚本中,并且签名证书的指纹打印在输出中Set-AuthenticodeSignature
,但状态仍然是UnknownError
并根据AllSigned
政策仍然不允许。
# Output some info about the certificate:
PS> $cert | Format-List
Subject : CN=PowerShell Code Signing
Issuer : CN=PowerShell Trusted Authority
Thumbprint : <omitted>
FriendlyName :
NotBefore : 9/20/2017 10:48:59 PM
NotAfter : 9/20/2018 11:08:59 PM
Extensions : {System.Security.Cryptography.Oid, System.Security.Cryptography.Oid,
System.Security.Cryptography.Oid, System.Security.Cryptography.Oid}
我尝试过多种变体New-SelfSignedCertificate
咒语,特别是生成用于代码签名的证书,但始终具有相同的状态消息(UnknownError
).
我的最终目标是能够拥有Set-ExecutionPolicy AllSigned
并且仍然运行我自己创建的脚本。为了实现这一目标,我在这个过程中缺少什么?