W
W
WindBridges2021-08-27 20:47:56
PowerShell
WindBridges, 2021-08-27 20:47:56

How to throw an exception in PowerShell if the output of the previous command does not match the regular expression?

For example, I want to make sure that the PHP version is exactly what I need and continue executing the script, otherwise throw an exception. I check like this:

Invoke-Expression "php -v" | Select-String -Pattern "PHP 7.4"
Further, as I understand it, there should be another pipe and a command that, if an empty string is received, will throw an exception, but I can’t find anything like it. Tell me how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GaryManshow, 2021-08-30
@GaryManshow

Invoke Expression "php -v" | Where-Object {$_ -like "PHP 7.4"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question