我正在尝试使用PowerShell中的CertEnroll和CertLib COM库续订证书。这是我的代码:
[CmdletBinding()]
param(
[parameter (mandatory=$true)]
[string]$ServerName
)
$credential = Get-Credential
Invoke-Command -ComputerName $ServerName -Credential $credential -ScriptBlock{
$location=[System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
$store =New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $location
$store.Open(1)
$cert = $store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindByIssuerName,"<CA-Name>",$false)[0]
#Write-Output $cert.IssuerName
$store.Close()
$request = New-Object -ComObject X509Enrollment.CX509CertificateRequestPkcs7
try{
$request.InitializeFromCertificate(0x2,$true,[System.Convert]::ToBase64String($cert.RawData),0x1,3 -band 1024)
$enroll = New-Object -ComObject X509Enrollment.CX509Enrollment
$enroll.InitializeFromRequest($request)
$message = $enroll.CreateRequest(1)
$certReq = New-Object -ComObject CertificateAuthority.Request
$val=$certReq.Submit(0x1 -bor 0,$message,[string]::Empty,"<CA-Server>\"+ $cert.Issuer.Split('=')[1])
if($val -eq 3)
{
Write-Host "Certificate request accepted"
}
$reqid=$certReq.GetRequestId()
#$reqid=[System.Convert]::ToInt32($id)
$objcertRequest = New-Object -ComObject CertificateAuthority.Request
$pending = $objcertRequest.RetrievePending($reqid,"<CA-Server>"+$cert.Issuer.Split('=')[1])
if($pending -eq 3)
{
$certificate = $objcertRequest.GetCertificate(0x1 -bor 0x100);
$objEnroll = New-Object -ComObject X509Enrollment.CX509Enrollment
$objEnroll.Initialize(1);
$objEnroll.InstallResponse(4,$certificate,1,$null);
Write-Host "Installed Successfully"
}
else
{
Write-Error 'error'
}
}
catch
{
Write-Output $_.Exception.Message
Write-Output $_.Exception.ItemName
}
}
我已为CA服务器和CA名称指定了正确的值。我可以在本地计算机上续订证书,但是当我尝试在另一台计算机上为证书执行此操作时,我收到错误。 这是我得到的错误:
Exception calling "InitializeFromCertificate" with "5" argument (s): "CertEnroll::CX509CertificateRequestPkcs7::InitializeFromCertificate: The operation being requested was not performed because the user has not been authenticated. 0x800704dc (WIN32: 1244)"
任何人都可以告诉我为什么我会收到身份验证错误。我提供了正确的凭证。
答案 0 :(得分:1)
您提供了在远程主机上进行身份验证的凭据,但这些凭据不用于在CA服务器上进行身份验证。你要么: