具有 foreach 方案的 Export-PfxCertificate
Export-PfxCertificate with foreach scenario
我在尝试将证书从 windows 商店导出为 .pfx 格式时遇到一些问题
以下命令工作正常,
Get-ChildItem -Path Cert:\CurrentUser\My\**** | Export-PfxCertificate -FilePath "D:\a.pfx" -Password $securestring
而这不会
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { Export-PfxCertificate -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
错误:Export-PfxCertificate:无法使用指定的命名参数解析参数集。
连这个都没有
Get-ChildItem -Path Cert:\CurrentUser\My\*********** | Export-PfxCertificate -FilePath "D:\a.pfx" -Password (ConvertTo-SecureString -String $_.FriendlyName -Force -AsPlainText)
错误:
ConvertTo-SecureString:无法将参数绑定到参数 'String',因为它为空。
请说明一下..
提前致谢
您没有在第二个命令中将证书传递给 Export-PfxCertificate
,而是将找到的证书传递给 ForEach-Object
子句,但没有使用 $PSItem
块内。您有两个选择:
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { $_ | Export-PfxCertificate -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
或
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { Export-PfxCertificate -Certificate $_ -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
第一个将迭代的证书对象通过管道传递给 Export-PfxCertificate
,而第二个将证书作为直接参数提供。
为什么你的第三次尝试没有成功
我看到的第一件事是您正在将 Get-ChildItem
传送到 Export-PfxCertificate
,它只需要一个证书作为参数。如果 gci
returns 超过一个证书,它就会爆炸。
其次,您正在尝试使用证书友好名称作为证书密码。不确定这是否是您的意图,但代码出错是因为您试图在上下文之外使用 $PSItem
,并且返回值最终是 $null
作为必需参数 - 您需要在 ForEach-Object
或 Where-Object
脚本块中使用 $PSItem
或其别名 $_
。如果你这样做,它会起作用:
Get-ChildItem -Path Cert:\CurrentUser\My\*********** | ForEach-Object { $_ | Export-PfxCertificate -FilePath "D:\a.pfx" -Password ( ConvertTo-SecureString -String $_.FriendlyName -Force -AsPlainText ) }
请注意,我将命令的后半部分包裹在 ForEach-Object
循环中 - 这解决了两个问题,一次只传递一个证书,您还可以访问 FriendlyName
属性 因为那是当前可迭代的项目,在这种情况下应该是证书。
我在尝试将证书从 windows 商店导出为 .pfx 格式时遇到一些问题
以下命令工作正常,
Get-ChildItem -Path Cert:\CurrentUser\My\**** | Export-PfxCertificate -FilePath "D:\a.pfx" -Password $securestring
而这不会
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { Export-PfxCertificate -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
错误:Export-PfxCertificate:无法使用指定的命名参数解析参数集。
连这个都没有
Get-ChildItem -Path Cert:\CurrentUser\My\*********** | Export-PfxCertificate -FilePath "D:\a.pfx" -Password (ConvertTo-SecureString -String $_.FriendlyName -Force -AsPlainText)
错误: ConvertTo-SecureString:无法将参数绑定到参数 'String',因为它为空。
请说明一下.. 提前致谢
您没有在第二个命令中将证书传递给 Export-PfxCertificate
,而是将找到的证书传递给 ForEach-Object
子句,但没有使用 $PSItem
块内。您有两个选择:
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { $_ | Export-PfxCertificate -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
或
Get-ChildItem -Path Cert:\CurrentUser\My | ? {$_.friendlyname -like "zxy*" } | sort -Property FriendlyName | % { Export-PfxCertificate -Certificate $_ -FilePath "D:$($_.friendlyname).pfx" -Password $securestring }
第一个将迭代的证书对象通过管道传递给 Export-PfxCertificate
,而第二个将证书作为直接参数提供。
为什么你的第三次尝试没有成功
我看到的第一件事是您正在将 Get-ChildItem
传送到 Export-PfxCertificate
,它只需要一个证书作为参数。如果 gci
returns 超过一个证书,它就会爆炸。
其次,您正在尝试使用证书友好名称作为证书密码。不确定这是否是您的意图,但代码出错是因为您试图在上下文之外使用 $PSItem
,并且返回值最终是 $null
作为必需参数 - 您需要在 ForEach-Object
或 Where-Object
脚本块中使用 $PSItem
或其别名 $_
。如果你这样做,它会起作用:
Get-ChildItem -Path Cert:\CurrentUser\My\*********** | ForEach-Object { $_ | Export-PfxCertificate -FilePath "D:\a.pfx" -Password ( ConvertTo-SecureString -String $_.FriendlyName -Force -AsPlainText ) }
请注意,我将命令的后半部分包裹在 ForEach-Object
循环中 - 这解决了两个问题,一次只传递一个证书,您还可以访问 FriendlyName
属性 因为那是当前可迭代的项目,在这种情况下应该是证书。