{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/vagrant/issues/7557","repository_url":"https://api.github.com/repos/hashicorp/vagrant","labels_url":"https://api.github.com/repos/hashicorp/vagrant/issues/7557/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/vagrant/issues/7557/comments","events_url":"https://api.github.com/repos/hashicorp/vagrant/issues/7557/events","html_url":"https://github.com/hashicorp/vagrant/issues/7557","id":164526274,"number":7557,"title":"Changing hostname doesn't work for joined domain computers","user":{"login":"martijnburgers","id":906501,"avatar_url":"https://avatars1.githubusercontent.com/u/906501?v=4","gravatar_id":"","url":"https://api.github.com/users/martijnburgers","html_url":"https://github.com/martijnburgers","followers_url":"https://api.github.com/users/martijnburgers/followers","following_url":"https://api.github.com/users/martijnburgers/following{/other_user}","gists_url":"https://api.github.com/users/martijnburgers/gists{/gist_id}","starred_url":"https://api.github.com/users/martijnburgers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martijnburgers/subscriptions","organizations_url":"https://api.github.com/users/martijnburgers/orgs","repos_url":"https://api.github.com/users/martijnburgers/repos","events_url":"https://api.github.com/users/martijnburgers/events{/privacy}","received_events_url":"https://api.github.com/users/martijnburgers/received_events","type":"User","site_admin":false},"labels":[{"id":25816,"url":"https://api.github.com/repos/hashicorp/vagrant/labels/bug","name":"bug","color":"e11d21","default":true},{"id":218287562,"url":"https://api.github.com/repos/hashicorp/vagrant/labels/guest/windows","name":"guest/windows","color":"5319e7","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/hashicorp/vagrant/milestones/20","html_url":"https://github.com/hashicorp/vagrant/milestone/20","labels_url":"https://api.github.com/repos/hashicorp/vagrant/milestones/20/labels","id":2033220,"number":20,"title":"Secondary","description":"Secondary issues to pull from for point releases. Main composition is non-critical bug reports and requests.","creator":{"login":"chrisroberts","id":266674,"avatar_url":"https://avatars1.githubusercontent.com/u/266674?v=4","gravatar_id":"","url":"https://api.github.com/users/chrisroberts","html_url":"https://github.com/chrisroberts","followers_url":"https://api.github.com/users/chrisroberts/followers","following_url":"https://api.github.com/users/chrisroberts/following{/other_user}","gists_url":"https://api.github.com/users/chrisroberts/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisroberts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisroberts/subscriptions","organizations_url":"https://api.github.com/users/chrisroberts/orgs","repos_url":"https://api.github.com/users/chrisroberts/repos","events_url":"https://api.github.com/users/chrisroberts/events{/privacy}","received_events_url":"https://api.github.com/users/chrisroberts/received_events","type":"User","site_admin":false},"open_issues":79,"closed_issues":151,"state":"open","created_at":"2016-09-27T21:31:38Z","updated_at":"2017-09-06T18:41:00Z","due_on":null,"closed_at":null},"comments":0,"created_at":"2016-07-08T13:03:35Z","updated_at":"2017-09-30T01:19:29Z","closed_at":null,"author_association":"NONE","body":"I came acros this issue while diagnosing the problem described in [GH-7556](https://github.com/mitchellh/vagrant/issues/7556).\n\nThe code file for changing the hostname is: [plugins\\guests\\windows\\cap\\change_host_name.rb](https://github.com/mitchellh/vagrant/blob/master/plugins/communicators/winrm/communicator.rb)\n\n``` ruby\nmodule VagrantPlugins\n  module GuestWindows\n    module Cap\n      module ChangeHostName\n\n        def self.change_host_name(machine, name)\n          change_host_name_and_wait(machine, name, machine.config.vm.graceful_halt_timeout)\n        end\n\n        def self.change_host_name_and_wait(machine, name, sleep_timeout)\n\n          # If the configured name matches the current name, then bail\n          # We cannot use %ComputerName% because it truncates at 15 chars\n          return if machine.communicate.test(\"if ([System.Net.Dns]::GetHostName() -eq '#{name}') { exit 0 } exit 1\")\n\n          # Rename and reboot host if rename succeeded\n          script = <<-EOH\n            $computer = Get-WmiObject -Class Win32_ComputerSystem\n            $retval = $computer.rename(\"#{name}\").returnvalue\n            if ($retval -eq 0) {\n              shutdown /r /t 5 /f /d p:4:1 /c \"Vagrant Rename Computer\"\n            }\n            exit $retval\n          EOH\n\n          machine.communicate.execute(\n            script,\n            error_class: Errors::RenameComputerFailed,\n            error_key: :rename_computer_failed)\n\n          # Don't continue until the machine has shutdown and rebooted\n          sleep(sleep_timeout)\n        end\n      end\n    end\n  end\nend\n```\n\nThe WinRM command checks if the hostname of the machine is equal to the hostname in the Vagrantfile. If it's not, a new WinRM command is send which renames the computer and performs a reboot depending on the return value of the `rename` method.\n\nThe [WMI rename method](https://msdn.microsoft.com/en-us/library/aa393056%28v=vs.85%29.aspx) returns a 0 (zero) if successful. A nonzero return value indicates an error. The return value is zero in the case of renaming a `WORKGROUP` computer but not for joined domain computers. For renaming a DC I see a error code of 1355 and for _regular_ joined computers a error code of 5. No reboot is forced and the `RenameComputerFailed` error is raised and Vagrant stops further processing.\n\nThe meaning of the error codes are:\n\n[5 - Access is denied](https://msdn.microsoft.com/en-us/library/aa392154%28v=vs.85%29.aspx)\n[1355 - The specified domain either does not exist or could not be contacted.](https://msdn.microsoft.com/en-us/library/ms681385%28v=vs.85%29.aspx)\n\nSo we have to decide whether we are going to support renaming joined domain computers or not. If not we should raise a beter describing error because `Errors::RenameComputerFailed` is not correct here. \n\nIf yes - I think we need to run the WMI command with elevated permissions. If I run the [rename method](https://msdn.microsoft.com/en-us/library/aa393056%28v=vs.85%29.aspx) manually in a elevated shell on both the DC and a regular joined computer the return value is 0 (zero). I don't know if this will break other things because the WMI command will be packed as a scheduled job (or are there other ways?)\n### Debug output\n- [Gist Vagrant Log - Regular Joined Computer](https://gist.github.com/martijnburgers/9cfac68e723a9ff210375a71929d9484#file-change_hostname_joined_domain_computer_regular-log-L10510-L10574)\n- [Gist Vagrant Log - DC](https://gist.github.com/martijnburgers/239e12a7061746126d90246e145bcdba#file-change_hostname_joined_domain_computer_dc-log-L13977-L14040)\n### References\n- [GH-7556 (Hostname is always changed on an `vagrant reload` or `vagrant up`)](https://github.com/mitchellh/vagrant/issues/7556)\n"},"comment":{"url":"https://api.github.com/repos/hashicorp/vagrant/issues/comments/333271951","html_url":"https://github.com/hashicorp/vagrant/issues/7557#issuecomment-333271951","issue_url":"https://api.github.com/repos/hashicorp/vagrant/issues/7557","id":333271951,"user":{"login":"benh57","id":847794,"avatar_url":"https://avatars0.githubusercontent.com/u/847794?v=4","gravatar_id":"","url":"https://api.github.com/users/benh57","html_url":"https://github.com/benh57","followers_url":"https://api.github.com/users/benh57/followers","following_url":"https://api.github.com/users/benh57/following{/other_user}","gists_url":"https://api.github.com/users/benh57/gists{/gist_id}","starred_url":"https://api.github.com/users/benh57/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benh57/subscriptions","organizations_url":"https://api.github.com/users/benh57/orgs","repos_url":"https://api.github.com/users/benh57/repos","events_url":"https://api.github.com/users/benh57/events{/privacy}","received_events_url":"https://api.github.com/users/benh57/received_events","type":"User","site_admin":false},"created_at":"2017-09-30T01:19:29Z","updated_at":"2017-09-30T01:19:29Z","author_association":"CONTRIBUTOR","body":"Just encountered this when setting up a new Windows 10 VM.\r\nI feel like this used to work back when i was on 2008.\r\n\r\nI think a normal workflow is to start with an unbound image, have vagrant rename the computer to the short hostname,, then have a provisioner which joins it to the domain. So i'd be fine if rename just stopped working once the PC's on the domain -- that's how it used to work. "}}
{"actor":{"display_login":"benh57"}}