I am hoping someone can help me. I have extracted certificates from a PFX and would like to remove all lines that start with a space, "Bag Attributes", "issuer" and "subject". My input file would look something like this:
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-----
And the output should look like the following:
-----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-----
I have tried using the following which does remove lines beginning with spaces, however have not been able to successfully remove lines starting with "Bag Attrubutes","subject" and "issuer" with { $_ -notmatch "^ ","^subject","^issuer","^Bag Attributes" }
Get-Content "C:\ScriptRepository\Certs\CA-Chain.pem" |
Where { $_ -notmatch "^ " } |
Set-Content "C:\ScriptRepository\Certs\CA-chain2.pem"
Any help would be greatly appreciated.
BLAH BLAH BLAHis a bad representation of a certificate which per definition of your question shouldn't have spaces. So, the question is what DO you want to match ? (and not what you do not want to match). e.g. all valid certificate characters (excluding spaces) that start from the beginning to the end of the line:-Match '^[A-Z,a-z,0-9,=]+$'(or a specific length-Match '^[A-Z,a-z,0-9,=]{8,40}$'or-Match '^[A-Z,a-z,0-9,=]+\s*$'if you want to accept tailing spaces)