Powershell 脚本:无法读取执行程序的返回值 [英] Powershell script: Can't read return value of executed program

查看:32
本文介绍了Powershell 脚本:无法读取执行程序的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PowerShell 运行一个脚本,该脚本执行 wget 以获取网页(一个简单的数据库导入脚本)并分析其输出(错误消息或OK").

I am using PowerShell to run a script that executes wget to fetch a web page (a simple database import script) and analyzes its output (Error message or "OK").

我正在使用 我之前的问题.

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

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

当 wget 操作的结果是 404 并且 wget 可能返回错误级别 1 或 127 时,我从 PowerShell 收到以下错误消息:

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

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

这显然是指我调用了 CompareTo() 函数.

this obviously refers to my calling the CompareTo() function.

然而,wget 被执行并输出一些东西.

However, wget gets executed and outputs something.

在这种情况下,我怀疑 wget 会输出到错误控制台,而我的 $a 操作不会捕获到这一点.

I am suspecting that wget outputs to the error console in this case, and this does not get caught by my $a operation.

如何重定向错误输出以使其被我的脚本捕获?

男孩,我肯定会成为本月 PowerShell 标签中的问题之王!:)

Boy, I'm sure going to be question king in the PowerShell tag this month! :)

推荐答案

开始

# 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,我想最快的方法是使用流重定向运行脚本

As for reading the STDERR, i guess the quickest way will be to run the script with stream redirection

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

这篇关于Powershell 脚本:无法读取执行程序的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆