在PowerShell中运行openssl命令

Dom*_*tti 6 windows powershell command

我在PowerShell中执行以下命令:

Invoke-Expression "openssl pkcs12 -in $certCN.pfx -nocerts -nodes -out $certCN.key -password pass:1111"
Run Code Online (Sandbox Code Playgroud)

它工作正常,但输出openssl导致丑陋的控制台错误:

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
Run Code Online (Sandbox Code Playgroud)

执行此OpenSSL命令并忽略输出或至少不将其解释为命令的最佳方法是什么?

Bil*_*art 7

您不需要Invoke-Expression(事实上​​,除特殊情况外,不建议使用,因为它易于注射).只需运行命令并引用需要变量字符串扩展的参数:

openssl pkcs12 -in "$certCN.pfx" -nocerts -nodes -out "$certCN.key" -password pass:1111
Run Code Online (Sandbox Code Playgroud)

要忽略命令的输出,一种常见的技术是管道Out-Null.

  • 如果您不想从源代码构建,您可以从哪里安全地获取“openssl”二进制文件? (2认同)
  • @user3613932 如果你安装了 Git,那么你已经在 C:\Program Files\Git\usr\bin\openssl.exe 中找到了它 (2认同)