Powershell 导入PfxCertificate返回FileNotFoundException

Powershell 导入PfxCertificate返回FileNotFoundException,powershell,certificate,Powershell,Certificate,我正在尝试将一个自签名证书分发到多个服务器,并将其分发到一组服务帐户的个人证书存储中。此证书将用于解密只有服务帐户才能读取的敏感文件 证书的创建方式如下: 新的自签名证书-DnsName CredentialVault` -CertStoreLocation“证书:\CurrentUser\My”` -密钥使用密钥加密,数据加密-类型DocumentEncryptionCert` -密钥算法RSA-密钥长度2048 $computer='SERVER123.EXAMPLE.COM' $cred

我正在尝试将一个自签名证书分发到多个服务器,并将其分发到一组服务帐户的个人证书存储中。此证书将用于解密只有服务帐户才能读取的敏感文件

证书的创建方式如下:

新的自签名证书-DnsName CredentialVault`
-CertStoreLocation“证书:\CurrentUser\My”`
-密钥使用密钥加密,数据加密-类型DocumentEncryptionCert`
-密钥算法RSA-密钥长度2048
$computer='SERVER123.EXAMPLE.COM'
$cred=新对象PSCredential“MYDOMAIN\USER213”`
$(转换为SecureString-String'P4$$w0Rd'-AsPlainText-Force)
$scriptBlock={
导入PfxCertificate-文件路径“C:\temp\CredentialVault.pfx”`
-密码$(转换为SecureString-String'1234'-AsPlainText-Force)`
-CertStoreLocation“证书:\CurrentUser\My”
}
调用命令-ComputerName$computer-Credential$cred-ScriptBlock$ScriptBlock
我在服务器
SERVER123.EXAMPLE.COM
(但使用不同的帐户)上执行代码,只是为了在尝试远程服务器之前看看它是否可以工作。这就是文件路径指向本地文件的原因

但是,即使文件可由
MYDOMAIN\USER213
帐户访问(我首先在同一台服务器上测试了
Import PfxCertificate
命令,PowerShell仍会返回
System.IO.FileNotFoundException
,如下所示

未能找到PFX文件。 +CategoryInfo:NotSpecified:(:)[Import PfxCertificate],FileNotFoundException +FullyQualifiedErrorId:System.IO.FileNotFoundException,Microsoft.CertificateServices.Commands.ImportFXCertificate +PSComputerName:SERVER123.EXAMPLE.COM 我不知道问题是什么。欢迎提出建议

版本信息:

  • Windows Server 2016
  • PowerShell 5.1.14393.3471

可能您必须尝试使用path变量
$env:HOMEDRIVE\temp\CredentialVault.pfx
我首先通过
PSSession
将文件复制到远程系统来解决此问题

仍然不知道为什么这是有效的,而原始代码没有。¯\_(ツ)_/“”

$certFile='C:\temp\CredentialVault.pfx'
$remoteFile=[System.IO.Path]::合并('C:\Temp',[System.IO.Path]::GetFileName($certFile))
$certPassword=$(转换为SecureString-String'1234'-AsPlainText-Force)
$cred=获取凭证
$sess=新PSSession-ComputerName$server-Credential$cred
试一试{
复制项目-路径$certFile-ToSession$sess-目标$remoteFile
调用命令-会话$sess-脚本块{
导入PfxCertificate-文件路径$remoteFile`
-密码$certPassword`
-CertStoreLocation“证书:\CurrentUser\My”
删除项$remoteFile
}
}
最后{
删除PSSession$sess
}