20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/vagrant-winrmcli/command.rb', line 20
def execute
silence_warnings do
require "winrm"
end
mode = parse_args
with_target_vms(nil, single_target: true) do |vm|
username = vm.config.winrm.username
password = vm.config.winrm.password
port = vm.config.winrm.port
host = vm.ssh_info[:host]
endpoint = 'https://' + host.to_s + ':' + port.to_s + '/wsman'
winrm = WinRM::WinRMWebService.new(endpoint, :ssl, :user => username, :pass => password, :basic_auth_only => true)
winrm.instance_variable_get('@xfer').instance_variable_get('@httpcli').ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
shell_id = nil
silence_warnings do
shell_id = winrm.open_shell()
end
while true do
cmd = @env.ui.ask mode == 'ps' ? "PS> " : "CMD> "
cmd = cmd.strip
if cmd.empty?
next
end
if mode == 'ps'
cmd = '$tcd=[Environment]::GetEnvironmentVariable("TuskCd","User");if($tcd){cd $tcd;};function Set-Location {$r=iex ("Microsoft.PowerShell.Management\Set-Location " + $args -join " ");[Environment]::SetEnvironmentVariable("TuskCd",(gl),"User");$r} ' + cmd
encoded_script = cmd.encode('UTF-16LE', 'UTF-8')
script = Base64.strict_encode64(encoded_script)
cmd = "powershell -encodedCommand #{script}"
end
command_id = nil
silence_warnings do
command_id = winrm.run_command(shell_id, cmd)
end
isclixml = false
winrm.get_command_output(shell_id, command_id) do |stdout, stderr|
STDOUT.print stdout
if !stderr.nil? and (isclixml or stderr.include? 'CLIXML')
isclixml = true
stderr.scan(/<S S="Error">(.*?)<\/S>/).each { |x|
STDERR.print x[0].gsub('_x000D__x000A_', "\n")
}
else
STDERR.print stderr
end
end
winrm.cleanup_command(shell_id, command_id)
end
winrm.close_shell(shell_id)
end
end
|