我正在编写一个脚本,该脚本将把文件夹中的.key文件通过OpenSSL转换为.der格式。我尝试将当前文件名保存为变量,这样转换后的证书将保持相同的名称,只是扩展名会改变。下面是我到目前为止尝试过的代码:
$env:path="C:\Source\RFK\RFK Hardening\OpenSSL v1.1.1e\x64\bin"
$env:path="C:\Source\RFK\RFK Hardening\OpenSSL v1.1.1e\x86\bin"
$Keypath = "C:\Source\RFK\RFK Hardening\2. PKI Certificates\RFK PKI Files"
$Location = "C:\Source\RFK\RFK Hardening\2. PKI Certificates\PKI Files\RFK CSR Files"
$Keyfiles = Get-ChildItem -path $Location -Recurse -Include "*local.key"
Get-ChildItem -Path $Keypath -Recurse -Include "*.key" |
ForEach-Object {
$KeyName = Get-Content $_.fullName
$KeyName | openssl rsa -in $_.FullName.key -out $_.FullName.der -outform DER
}发布于 2020-01-22 23:42:57
$_.FullName将扩展到文件的完整路径,扩展名也是如此。另外,完全覆盖路径也是错误的,路径必须用分号分隔。此外,通过$KeyName获取内容也是不必要的。$_从管道获取值,所以放入$KeyName会扩展内容的完整路径,这是无效的。
$env:path +=";C:\Source\RFK\RFK Hardening\OpenSSL v1.1.1e\x64\bin;"
$env:path +="C:\Source\RFK\RFK Hardening\OpenSSL v1.1.1e\x86\bin;"
$Keypath = "C:\Source\RFK\RFK Hardening\2. PKI Certificates\RFK PKI Files"
$Location = "C:\Source\RFK\RFK Hardening\2. PKI Certificates\PKI Files\RFK CSR Files"
$Keyfiles = Get-ChildItem -path $Location -Recurse -Include "*local.key"
Get-ChildItem $Keypath -Recurse -Include "*.key" | ForEach {
$OutFile = $_.Fullname.ToString().Replace(".key", ".dar")
# Note you can use Regex operator with Regex '\.key$', '\.dar$'
openssl rsa -in $_.FullName -out $Outfile -outform DER
}https://stackoverflow.com/questions/59862957
复制相似问题