我正在編寫一些自動化腳本,需要使用Ruby在遠程計算機上運行PowerShell命令。在Ruby中我有以下代碼:在Ruby中獲取PowerShell輸出
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顯然會引發錯誤(比如無法訪問機器),但輸出始終爲真。
有沒有辦法知道命令是否成功運行?
PowerShell執行後檢查'$?'自動變量。 http://technet.microsoft.com/en-us/library/hh847768.aspx – mudasobwa 2014-10-01 07:00:26