Skip to content

Instantly share code, notes, and snippets.

@RamblingCookieMonster RamblingCookieMonster/ps-winrb.rb Secret
Last active Feb 23, 2016

What would you like to do?
# Point this to your ruby binary as desired...
require 'winrm'
require 'optparse'
require 'io/console'
require 'logger'
# Default values
params = Hash[
:password => '*',
:username => 'REDACTED\wframe',
:uri => 'http://ts1:5985/wsman'
# Who knows if this is the right route for verbose output
logg =
logg.level = Logger::WARN
logg.datetime_format = "%Y-%m-%d %H:%M:%S"
# Parse ze args
# We should make -c take remaining positional arguments, but this is quick and dirty... do |opts|
opts.banner = "Usage: winrm.rb [options]"
opts.on('-a', '--uri ADDRESS', String, 'Connection URI') { |v| params[:uri] = v }
opts.on('-u', '--username USERNAME', String, 'User name') { |v| params[:username] = v }
opts.on('-p', '--password PASSWORD', String, 'Password. Prompt if not specified') { |v| params[:password] = v }
opts.on('-v', '--verbose', 'Run verbosely') { logg.level = Logger::INFO }
opts.on('-c', '--command POWERSHELLCODE', 'Run arbitrary PowerShell code. Beware string parsing \\$') { |v| params[:command] = v }
# No user/PW? get them
unless params.key?(:username)
print "Enter Username: "
params[:username] = STDIN.gets.chomp
unless params.key?(:password) && params[:password] != '*'
print "Enter Password:\n"
params[:password] = STDIN.noecho(&:gets).chomp
# Output the params. Maybe don't show the password. Probably a more appropiate way to do this. {
cleaned = params.clone
# WinRM fun
winrm =[:uri],
:user => params[:username],
:pass => params[:password])
winrm.create_executor do |executor|
executor.run_powershell_script(params[:command]) do |stdout, stderr|
STDOUT.print stdout
STDERR.print stderr
# Side note...
# I ran into issues where I would get a "Safe handle has been closed" error, among other issues.
# Couldn't find any config issues, WinRM from the local box worked, and other common misconfigurations were not in play...
# Ultimately, got fed up, restarted, and it everything started playing nicely.
# I don't like that...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.