powershell - Import-PfxCertificate 返回 FileNotFoundException
问题描述
我正在尝试将单个自签名证书分发到多个服务器,并分发到一组服务帐户的个人证书存储中。此证书将用于解密只有服务帐户才能读取的敏感文件。
证书创建如下:
New-SelfSignedCertificate -DnsName CredentialVault `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyUsage KeyEncipherment,DataEncipherment -Type DocumentEncryptionCert `
-KeyAlgorithm RSA -KeyLength 2048
$computer = 'SERVER123.EXAMPLE.COM'
$cred = New-Object PSCredential 'MYDOMAIN\USER213', `
$(ConvertTo-SecureString -String 'P4$$w0Rd' -AsPlainText -Force)
$scriptBlock = {
Import-PfxCertificate -FilePath 'C:\temp\CredentialVault.pfx' `
-Password $(ConvertTo-SecureString -String '1234' -AsPlainText -Force) `
-CertStoreLocation "Cert:\CurrentUser\My"
}
Invoke-Command -ComputerName $computer -Credential $cred -ScriptBlock $scriptBlock
我在服务器上执行代码SERVER123.EXAMPLE.COM
(但使用不同的帐户),只是想在尝试远程服务器之前看看它是否可以工作。这就是文件路径引用本地文件的原因。
但是,即使该MYDOMAIN\USER213
帐户可以访问该文件(我Import-PfxCertificate
首先与该用户在同一台服务器上直接测试了该命令),PowerShell 仍会返回 a System.IO.FileNotFoundException
,如下所示。
The PFX file could not be found.
+ CategoryInfo : NotSpecified: (:) [Import-PfxCertificate], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.CertificateServices.Commands.ImportPfxCertificate
+ PSComputerName : SERVER123.EXAMPLE.COM
我不知道问题是什么。欢迎提出建议!
版本信息:
- 视窗服务器 2016
- PowerShell 5.1.14393.3471
解决方案
我最终通过首先将文件复制到远程系统来解决它,通过PSSession
.
仍然不知道为什么会这样,而原始代码却没有。¯\_(ツ)_/¯
$certFile = 'C:\temp\CredentialVault.pfx'
$remoteFile = [System.IO.Path]::Combine('C:\Temp', [System.IO.Path]::GetFileName($certFile))
$certPassword = $(ConvertTo-SecureString -String '1234' -AsPlainText -Force)
$cred = Get-Credential
$sess = New-PSSession -ComputerName $server -Credential $cred
try {
Copy-Item -Path $certFile -ToSession $sess -Destination $remoteFile
Invoke-Command -Session $sess -ScriptBlock {
Import-PfxCertificate -FilePath $remoteFile `
-Password $certPassword `
-CertStoreLocation "Cert:\CurrentUser\My"
Remove-Item $remoteFile
}
}
finally {
Remove-PSSession $sess
}
推荐阅读
- c++ - 传递给主函数的命令行参数是否有例外?
- ffmpeg - 如何在ffmpeg中添加多个过滤器?
- php - 如何按类别名称在 ACF 中自动添加新行
- python - 如何使用 Bio.Entrez 提取 PMC 文章标题和摘要的完整列表?
- android - 发布 GitHub 包返回 422 错误
- python - Python管道重复检查器,使用“raise DropItem”,但我们如何让它向下管道?
- html - HTML - 如何将我的自定义单选按钮存储到我的 localStorage 中?
- apache - RewriteRule - 或 document_root 到 subdurectory 的别名(不是重定向)导致“错误请求”或“内部服务器错误”
- opengl - 在OpenGL中保存旋转矩阵
- azure - 使用 Terraform 创建 Azure Synapse 池时出错