plugins/communicators/winrm/shell.rb in vagrant-unbundled-2.2.2.0 vs plugins/communicators/winrm/shell.rb in vagrant-unbundled-2.2.3.0

- old
+ new

@@ -15,10 +15,14 @@ module VagrantPlugins module CommunicatorWinRM class WinRMShell include Vagrant::Util::Retryable + # Exit code generated when user is invalid. Can occur + # after a hostname update + INVALID_USERID_EXITCODE = -196608 + # These are the exceptions that we retry because they represent # errors that are generally fixed from a retry and don't # necessarily represent immediate failure cases. @@exceptions_to_retry_on = [ HTTPClient::KeepAliveDisconnected, @@ -69,17 +73,26 @@ end def elevated(command, opts = {}, &block) connection.shell(:elevated) do |shell| shell.interactive_logon = opts[:interactive] || false - uname = shell.username - begin - shell.username = elevated_username - execute_with_rescue(shell, command, &block) - ensure - shell.username = uname + result = execute_with_rescue(shell, command, &block) + if result.exitcode == INVALID_USERID_EXITCODE && result.stderr.include?(":UserId:") + uname = shell.username + ename = elevated_username + if uname != ename + @logger.warn("elevated command failed due to username error") + @logger.warn("retrying command using machine prefixed username - #{ename}") + begin + shell.username = ename + result = execute_with_rescue(shell, command, &block) + ensure + shell.username = uname + end + end end + result end end def wql(query, opts = {}, &block) retryable(tries: @config.max_tries, on: @@exceptions_to_retry_on, sleep: @config.retry_delay) do @@ -222,24 +235,21 @@ retry_delay: @config.retry_delay, retry_limit: @config.max_tries } end def elevated_username - if @elevated_username - return @elevated_username - end if username.include?("\\") - return @elevated_username = username + return username end computername = "" powershell("Write-Output $env:computername") do |type, data| computername << data if type == :stdout end computername.strip! if computername.empty? - return @elevated_username = username + return username end - @elevated_username = "#{computername}\\#{username}" + "#{computername}\\#{username}" end end #WinShell class end end