我正在研究一个脚本,该脚本将.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
}
答案 0 :(得分:0)
$_.FullName
扩展到文件的完整路径,并带有扩展名。同样,完全覆盖PATH也是不当行为,必须用分号分隔路径。也不需要通过$ 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
}