3

I was wondering if this Bash command could be converted to PowerShell:

openssl s_client -showcerts -connect host:port </dev/null 2>/dev/null | openssl x509 -noout -dates

The current script (not working):

.\openssl s_client -showcerts -connect host:port 2>$null | .\openssl x509 -noout -dates

The missing </dev/null makes it wait infinitely for input. Is there a PowerShell alternative for that syntax?

<$null doesn't work since < is reserved for future use.



Any suggestions?

3
  • I don't have experience with powershell, but the "normal" command shell uses NUL for the bitbucket. Did you try <NUL with the Powershell? Commented Sep 21, 2021 at 13:15
  • 1
    Pipe an empty string to openssl s_client: '' |.\openssl s_client .... Or a NUL byte: [char]0 |.\openssl s_client ... Commented Sep 21, 2021 at 13:20
  • @MathiasR.Jessen, only @() | ... truly provides no input. Commented Sep 21, 2021 at 16:38

1 Answer 1

5

The equivalent of </dev/null is @() | ..., as the following simplified example shows:

# Equivalent of `cat -n </dev/null` in Bash, for instance.
@() | cat -n
  • PowerShell doesn't support the < operator at all, as of PowerShell 7.2;[1] to send data that an external program sees via its stdin stream, it must be piped (|) to it, as shown above.

  • Piping an empty array (@()) effectively provides no stdin input,[2] as </dev/null does in POSIX-compatible shells.

Note that piping '' or "`0" / [char] 0 does not work, because PowerShell invariably appends a newline to the data, which it invariably sends as text.

However, there may be an additional problem:

Your specific command joins two external programs with a PowerShell pipeline, which runs the risk of inadvertent modification of the data being streamed, because this invariably involves decoding as text of the first process' output and re-encoding as text before sending it to the second process - see this answer for more information.

The workaround is to call the platform-native shell's CLI, whose | operator streams data unmodified; the platform-native shell also allow use of <:

On Windows, use cmd /c and NUL in lieu of /dev/null:

cmd /c 'openssl s_client -showcerts -connect host:port <NUL 2>NUL | openssl x509 -noout -dates'

On Unix-like platforms, use sh -c (/dev/null can be used as-is):

sh -c 'openssl s_client -showcerts -connect host:port </dev/null 2>/dev/null | openssl x509 -noout -dates'

[1] The error message when trying to use < mentions "reserved for future use", but there is no telling if this operator will ever be implemented, and given that the pipeline covers its functionality I'd say that chances are slim.

[2] The reason is that PowerShell enumerates collections (such as arrays) in the pipeline, i.e. it sends the elements one by one. An empty collection by definition has no elements, so there is nothing to send.

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

Comments

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.