0

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.

4
  • This is a standard regex operation... You need to read the file via get-content (or one of the .net alterantives) and then check line by line the regex and if its fullfilled, remove the match. I personally prefer the .net regex operations, but the powershell regex operations should be fine too Commented Oct 22, 2021 at 9:55
  • Hi Farbkreis, thanks for the speedy response, I have been banging my head against the wall for a couple of days trying to get this to work, could you possibly provide an example. Commented Oct 22, 2021 at 10:02
  • BLAH BLAH BLAH is 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) Commented Oct 22, 2021 at 11:13
  • Hi Farbkreis , my first sentence in my question asks "remove all lines that start with a space, "Bag Attributes", "issuer" and "subject" that all that was required. Commented Oct 22, 2021 at 11:34

1 Answer 1

1

If you have that in a file, just use a switch with a very simple regex:

$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"

Output:

-----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-----

Regex details:

^                 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.)
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Theo, I really appreciate your time for answering this for me, it has worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.