Powershell script: cannot read return value of executed program

I use PowerShell to run a script that runs wgetto retrieve a web page (simple import of the script database) and parses its output (error message or "OK").

I am using the code from the answer to this previous question .

$a = c:\path_to_wget\wget.exe --quiet -O - "http://www.example.com/import_db"
$rc = $a.CompareTo("OK")
exit $rc

When the result of the wget operation is 404 - and wget probably returns an error level of 1 or 127 - I get the following error message from PowerShell:

You cannot call a method on a null-valued expression.

this obviously applies to my function call CompareTo().

However, wget launches and outputs something.

I suspect that wget output to the error console in this case, and this does not fall into my $ a operation.

How can I redirect the error output so that it gets into my script?

, , PowerShell !:)

+3
1

# This will hold the last executed EXE return code
$LastExitCode
# For console apps, where 0 is true, else is false, this will hold either True or False
$?

STDERR, , script

$a = c:\path_to_wget\wget.exe --quiet -O - "http://www.example.com/import_db" 2>&1
+4

Source: https://habr.com/ru/post/1728078/


All Articles