【发布时间】:2014-10-01 05:42:56
【问题描述】:
我正在编写一些自动化脚本,需要使用 Ruby 在远程机器上运行 PowerShell 命令。在 Ruby 中,我有以下代码:
def run_powershell(powershell_command)
puts %Q-Executing powershell #{powershell_command}-
output = system("powershell.exe #{powershell_command}")
puts "Executed powershell output #{output}"
end
我可以传入基于 Invoke-Command 的 ps1 文件,一切都按预期工作。当我运行命令时,我可以在控制台中看到输出。
唯一的问题是没有办法知道命令运行是否成功;有时 PowerShell 显然会抛出错误(例如无法访问机器),但输出始终为 true。
有没有办法知道命令是否运行成功?
【问题讨论】:
-
在 powershell 执行后检查
$?自动变量。 technet.microsoft.com/en-us/library/hh847768.aspx
标签: ruby powershell windows2012