# Re: Executing a shell command from within a ruby script?

**URL:** <https://www.ruby-forum.com/t/re-executing-a-shell-command-from-within-a-ruby-script/225910>\
**Category:** Ruby\
**Created:** [May 27, 2006, 1:53am UTC](https://www.ruby-forum.com/t/re-executing-a-shell-command-from-within-a-ruby-script/225910 "2006-05-27T01:53:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zbrod](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/278dde/32.png) [@zbrod](https://www.ruby-forum.com/u/zbrod)\
**Post date:** [May 27, 2006, 1:53am UTC](https://www.ruby-forum.com/t/re-executing-a-shell-command-from-within-a-ruby-script/225910/1 "2006-05-27T01:53:09Z")

</div>

I recently used this to capture stdout and stderr after running  
Schtasks.exe on many windows servers SOX audit of scheduled jobs.

I then called it like this  
@taskScheduler = Win32Command.new(@@win32Command,@commandArgs)

P @taskScheduler.\_stdout

# This class executes a Microsoft Windows DOS command

# and holds the results of stdin and stdout in

# object attributes.

# 

require “win32/open3”  
$stdout.flush  
$stderr.flush

class Win32Command  
attr\_reader :\_stdout, :\_stderr  
def initialize(command, arguments)  
@@command = command  
@arguments = arguments  
@commandLine = @@command \<\< " " \<\< @arguments  
@\_stdin, @\_stdout, @\_stderr = Open3.popen3(@commandLine)  
@\_stderr.sync = true  
@\_stdin.close  
end  
end
