11

I'm executing the following command in PowerShell:

Invoke-Expression "openssl pkcs12 -in $certCN.pfx -nocerts -nodes -out $certCN.key -password pass:1111"

It works fine, however the output from openssl is causing ugly console errors:

openssl : MAC verified OK
At line:1 char:1
+ openssl pkcs12 -in www.mywebsite.com.pfx -nocerts -node ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (MAC verified OK:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

What's the best way to execute this OpenSSL command and have the output ignored or at least not interpreted as a command?

1
  • 8
    How is trying to get a powershell script to run without errors NOT a programming question again? Commented Nov 13, 2017 at 16:02

1 Answer 1

13

You don't need Invoke-Expression (in fact, it's not recommended except in specific cases because it is susceptible to injection). Just run the command and quote the parameters where you need variable string expansion:

openssl pkcs12 -in "$certCN.pfx" -nocerts -nodes -out "$certCN.key" -password pass:1111

To ignore the output of the command, one common technique is to pipe to Out-Null.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you - Not very familiar with powershell. I was planning on reading up tomorrow on invoke-expression vs. invoke-command. I'll give this a go.
Thanks. I've read up on them and see how they are pointless and risky. Also, FYI - I tried piping these commands to Out-Null and it didn't work. However appending the command with 2>&1 did. Thank you again.
Out-Null doesn't redirect stderr, IIRC.
Where do you get openssl binaries from safely in case you don't want to build from source?
@user3613932 If you have Git installed, you already have it at C:\Program Files\Git\usr\bin\openssl.exe
|

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.