Skip to content

Instantly share code, notes, and snippets.

@tothi
Created February 10, 2019 20:17
Show Gist options
  • Save tothi/addf01e516bb3a54f73bde45cfd7db74 to your computer and use it in GitHub Desktop.
Save tothi/addf01e516bb3a54f73bde45cfd7db74 to your computer and use it in GitHub Desktop.
interactive remote powershell (winrm) with client certificate auth
#!/usr/bin/ruby
#
# https://github.com/WinRb/WinRM
#
require 'winrm'
opts = {
endpoint: 'https://xxx.xxx.xxx.xxx:5986/wsman',
transport: :ssl,
client_cert: 'certs/MyClient.cer',
client_key: 'certs/MyClient.key',
no_ssl_peer_verification: true
}
conn = WinRM::Connection.new(opts)
conn.shell(:powershell) do |shell|
output = shell.run('$PSVersionTable') do |stdout, stderr|
STDOUT.print stdout
STDERR.print stderr
end
command = ""
until command == "exit\n" do
print "PS > "
command = gets
output = shell.run(command) do |stdout, stderr|
STDOUT.print stdout
STDERR.print stderr
end
end
puts "The script exited with exit code #{output.exitcode}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment