#!/usr/bin/ruby require 'ostruct' require 'optparse' require 'winrm' PROGRAM_NAME = $0 PROGRAM_VERSION = 1.0 REALM = 'MYREALM' options = OpenStruct.new optparse = OptionParser.new do |opts| opts.banner = < BANNER opts.on('-h', '--help', 'Show this message') do puts opts exit end opts.on_tail('--version', 'Show version') do puts "Version: #{PROGRAM_VERSION}" exit end end optparse.parse! options.script = ARGV[0] unless ARGV[0].nil? ARGV.shift options.servers = Array.new until ARGV[0].nil? do options.servers << ARGV[0] ARGV.shift end raise "Script file not specified" if options.script.nil? raise "Servers to run script on, not specified" if options.servers.size == 0 begin fileio = File.new(options.script, "r") rescue Exception => e puts e exit 1 end raise "Invalid Filename specified" unless fileio.kind_of?(IO) options.servers.each do |server| begin fileio.rewind endpoint = "http://#{server}:5985/wsman" winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm => REALM) output = winrm.run_powershell_script(fileio) rescue GSSAPI::GssApiError => gae system 'kinit' retry rescue Exception => e puts "#{e.class}" puts "#{e.message}" exit end puts "Executed command on #{server}, output =" puts "-"*78 output[:data].each do |line| STDOUT.print "#{line[:stdout]}" if line.key?(:stdout) STDERR.print "ERROR: #{line[:stderr]}" if line.key?(:stderr) end puts "-"*78 puts end