我正在编写一个创建自签名证书的 Powershell 脚本,该证书作为管理员运行良好,但作为管理员失败:
当我调用 Create 方法时失败的一段代码:
$PrivateKey = New-Object -ComObject X509Enrollment.CX509PrivateKey
$PrivateKey.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
$PrivateKey.KeySpec = 0x1
$PrivateKey.Length = 2048
# set security descriptor
$PrivateKey.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"
# key will be stored in local machine certificate store
$PrivateKey.MachineContext = 0x1
# export will be allowed
$PrivateKey.ExportPolicy = 0x1
$PrivateKey.Create()
它抛出这个错误:
Exception calling "Create" with "0" argument(s): "CertEnroll::CX509PrivateKey::Create: Access is denied. 0x80070005
(WIN32: 5)"
At C:\dev\LinoaSSC.ps1:106 char:1
+ $PrivateKey.Create()
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
我应该如何排除或解决此错误?多谢你们!