我希望有人可以幫助我。我已經從 PFX 中提取了證書,并希望洗掉所有以空格、“Bag Attributes”、“issuer”和“subject”開頭的行。我的輸入檔案看起來像這樣:
Bag Attributes
friendlyName: BLAH BLAH
BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH
issuer=C = BLAH BLAH BLAH
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
Bag Attributes
friendlyName: BLAH BLAH
BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH
issuer=C = BLAH BLAH BLAH
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
Bag Attributes
friendlyName: BLAH BLAH
BLAH BLAH BLAH
subject=C = BLAH BLAH BLAH
issuer=C = BLAH BLAH BLAH
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
輸出應如下所示:
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
我嘗試使用以下方法洗掉以空格開頭的行,但是無法成功洗掉以“Bag Attrubutes”、“subject”和“issuer”開頭的行,并帶有 { $_ -notmatch "^ ","^subject ","^issuer","^Bag 屬性" }
Get-Content "C:\ScriptRepository\Certs\CA-Chain.pem" |
Where { $_ -notmatch "^ " } |
Set-Content "C:\ScriptRepository\Certs\CA-chain2.pem"
任何幫助將不勝感激。
uj5u.com熱心網友回復:
如果你在一個檔案中有它,只需使用switch一個非常簡單的正則運算式:
$result = switch -Regex -File 'X:\InputFile.pfx' {
'^(Bag|subject|issuer|\s)' { <# skip these lines #> }
default { $_ }
}
$result | Set-Content -Path "C:\ScriptRepository\Certs\CA-chain2.pem"
輸出:
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
BLAH BLAH BLAH
BLAH BLAH BLAH
-----END CERTIFICATE-----
正則運算式詳細資訊:
^ Assert position at the beginning of the string
( Match the regular expression below and capture its match into backreference number 1
Match either the regular expression below (attempting the next alternative only if this one fails)
Bag Match the characters “Bag” literally
| Or match regular expression number 2 below (attempting the next alternative only if this one fails)
subject Match the characters “subject” literally
| Or match regular expression number 3 below (attempting the next alternative only if this one fails)
issuer Match the characters “issuer” literally
| Or match regular expression number 4 below (the entire group fails if this one fails to match)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331883.html
標籤:电源外壳
