plugins/commands/powershell/command.rb in vagrant-unbundled-2.2.2.0 vs plugins/commands/powershell/command.rb in vagrant-unbundled-2.2.3.0
- old
+ new
@@ -24,10 +24,14 @@
o.separator ""
o.on("-c", "--command COMMAND", "Execute a powershell command directly") do |c|
options[:command] = c
end
+
+ o.on("-e", "--elevated", "Execute a powershell command with elevated permissions") do |c|
+ options[:elevated] = true
+ end
end
# Parse out the extra args to send to the ps session, which
# is everything after the "--"
split_index = @argv.index("--")
@@ -38,33 +42,36 @@
# Parse the options and return if we don't have any target.
argv = parse_options(opts)
return if !argv
- # Check if the host even supports ps remoting
- raise Errors::HostUnsupported if !@env.host.capability?(:ps_client)
+ # Elevated option enabled means we can only execute commands
+ raise Errors::ElevatedNoCommand if !options[:command] && options[:elevated]
# Execute ps session if we can
with_target_vms(argv, single_target: true) do |machine|
if !machine.communicate.ready?
raise Vagrant::Errors::VMNotCreatedError
end
- if machine.config.vm.communicator != :winrm
- raise VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady
- end
+ if options[:command]
+ if machine.config.vm.communicator != :winrm
+ raise VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady
+ end
- if !options[:command].nil?
- out_code = machine.communicate.execute(options[:command].dup) do |type,data|
+ out_code = machine.communicate.execute(options[:command].dup, elevated: options[:elevated]) do |type,data|
machine.ui.detail(data) if type == :stdout
end
if out_code == 0
machine.ui.success("Command: #{options[:command]} executed successfully with output code #{out_code}.")
end
next
end
+ # Check if the host even supports ps remoting
+ raise Errors::HostUnsupported if !@env.host.capability?(:ps_client)
+
ps_info = VagrantPlugins::CommunicatorWinRM::Helper.winrm_info(machine)
ps_info[:username] = machine.config.winrm.username
ps_info[:password] = machine.config.winrm.password
# Extra arguments if we have any
ps_info[:extra_args] = options[:extra_args]
@@ -76,10 +83,10 @@
machine.ui.detail("Username: #{ps_info[:username]}")
begin
@env.host.capability(:ps_client, ps_info)
ensure
- if !result["PreviousTrustedHosts"].nil?
+ if result["PreviousTrustedHosts"]
reset_ps_remoting_for(machine, ps_info)
end
end
end
end