{"action":"created","comment":{"url":"https://api.github.com/repos/mitchellh/vagrant/pulls/comments/31864219","id":31864219,"diff_hunk":"@@ -0,0 +1,118 @@\n+require \"optparse\"\n+\n+require_relative \"../../communicators/winrm/helper\"\n+\n+module VagrantPlugins\n+  module CommandPS\n+    class Command < Vagrant.plugin(\"2\", :command)\n+      def self.synopsis\n+        \"connects to machine via powershell remoting\"\n+      end\n+\n+      def execute\n+        options = {}\n+\n+        opts = OptionParser.new do |o|\n+          o.banner = \"Usage: vagrant ps [-- extra ps args]\"\n+\n+          o.separator \"\"\n+          o.separator \"Options:\"\n+          o.separator \"\"\n+\n+          o.on(\"-c\", \"--command COMMAND\", \"Execute a powershell command directly\") do |c|\n+            options[:command] = c\n+          end\n+        end\n+\n+        # Parse out the extra args to send to the ps session, which\n+        # is everything after the \"--\"\n+        split_index = @argv.index(\"--\")\n+        if split_index\n+          options[:extra_args] = @argv.drop(split_index + 1)\n+          @argv                = @argv.take(split_index)\n+        end\n+\n+        # Parse the options and return if we don't have any target.\n+        argv = parse_options(opts)\n+        return if !argv\n+\n+        # Check if the host even supports ps remoting\n+        raise Errors::HostUnsupported if !@env.host.capability?(:ps_client)\n+\n+        # Execute ps session if we can\n+        with_target_vms(argv, single_target: true) do |machine|\n+          if !machine.communicate.ready?\n+            raise Vagrant::Errors::VMNotCreatedError\n+          end\n+\n+          if machine.config.vm.communicator != :winrm\n+            raise VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady\n+          end\n+\n+          if !options[:command].nil?\n+            out_code = machine.communicate.execute(options[:command].dup) do |type,data|\n+              machine.ui.detail(data) if type == :stdout\n+            end\n+            if out_code == 0\n+              machine.ui.success(\"Command: #{options[:command]} executed succesfully with output code #{out_code}.\")\n+            end\n+            next\n+          end\n+\n+          ps_info = VagrantPlugins::CommunicatorWinRM::Helper.winrm_info(machine)\n+          ps_info[:username] = machine.config.winrm.username\n+          ps_info[:password] = machine.config.winrm.password\n+          # Extra arguments if we have any\n+          ps_info[:extra_args] = options[:extra_args]\n+\n+          result = ready_ps_remoting_for(machine, ps_info)\n+\n+          machine.ui.detail(\n+            \"Creating powershell session to #{ps_info[:host]}:#{ps_info[:port]}\")\n+          machine.ui.detail(\"Username: #{ps_info[:username]}\")\n+\n+          begin\n+            @env.host.capability(:ps_client, ps_info)\n+          ensure\n+            if !result[\"PreviousTrustedHosts\"].nil?\n+              reset_ps_remoting_for(machine, ps_info)\n+            end\n+          end\n+        end\n+      end\n+\n+      def ready_ps_remoting_for(machine, ps_info)\n+        machine.ui.output(I18n.t(\"vagrant_ps.detecting\"))\n+        script_path = File.expand_path(\"../scripts/enable_psremoting.ps1\", __FILE__)\n+        args = []\n+        args << \"-hostname\" << ps_info[:host]\n+        args << \"-port\" << ps_info[:port].to_s\n+        args << \"-username\" << ps_info[:username]\n+        args << \"-password\" << ps_info[:password]\n+        result = Vagrant::Util::PowerShell.execute(script_path, *args)\n+        if result.exit_code != 0\n+          raise Errors::PowershellError,","path":"plugins/commands/ps/command.rb","position":94,"original_position":94,"commit_id":"740877065a8fc0b777cc7aa51b4fb074307affbd","original_commit_id":"740877065a8fc0b777cc7aa51b4fb074307affbd","user":{"login":"sneal","id":197023,"avatar_url":"https://avatars.githubusercontent.com/u/197023?v=3","gravatar_id":"","url":"https://api.github.com/users/sneal","html_url":"https://github.com/sneal","followers_url":"https://api.github.com/users/sneal/followers","following_url":"https://api.github.com/users/sneal/following{/other_user}","gists_url":"https://api.github.com/users/sneal/gists{/gist_id}","starred_url":"https://api.github.com/users/sneal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sneal/subscriptions","organizations_url":"https://api.github.com/users/sneal/orgs","repos_url":"https://api.github.com/users/sneal/repos","events_url":"https://api.github.com/users/sneal/events{/privacy}","received_events_url":"https://api.github.com/users/sneal/received_events","type":"User","site_admin":false},"body":"Is `PowershellError` available in this context?","created_at":"2015-06-06T03:48:47Z","updated_at":"2015-06-06T03:48:47Z","html_url":"https://github.com/mitchellh/vagrant/pull/4400#discussion_r31864219","pull_request_url":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400","_links":{"self":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/comments/31864219"},"html":{"href":"https://github.com/mitchellh/vagrant/pull/4400#discussion_r31864219"},"pull_request":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400"}}},"pull_request":{"url":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400","id":20395915,"html_url":"https://github.com/mitchellh/vagrant/pull/4400","diff_url":"https://github.com/mitchellh/vagrant/pull/4400.diff","patch_url":"https://github.com/mitchellh/vagrant/pull/4400.patch","issue_url":"https://api.github.com/repos/mitchellh/vagrant/issues/4400","number":4400,"state":"open","locked":false,"title":"Add a ps command to vagrant that drops the user into a remote powershell shell","user":{"login":"mwrock","id":655165,"avatar_url":"https://avatars.githubusercontent.com/u/655165?v=3","gravatar_id":"","url":"https://api.github.com/users/mwrock","html_url":"https://github.com/mwrock","followers_url":"https://api.github.com/users/mwrock/followers","following_url":"https://api.github.com/users/mwrock/following{/other_user}","gists_url":"https://api.github.com/users/mwrock/gists{/gist_id}","starred_url":"https://api.github.com/users/mwrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwrock/subscriptions","organizations_url":"https://api.github.com/users/mwrock/orgs","repos_url":"https://api.github.com/users/mwrock/repos","events_url":"https://api.github.com/users/mwrock/events{/privacy}","received_events_url":"https://api.github.com/users/mwrock/received_events","type":"User","site_admin":false},"body":"This adds a new vagrant command:\r\n```\r\nvagrant ps\r\n```\r\nExample:\r\n```\r\nC:\\dev\\vagrant\\win8.1x64> vagrant ps\r\n    default: Creating powershell session to 192.168.1.14:5985\r\n    default: Username: vagrant\r\n[192.168.1.14]: PS C:\\Users\\vagrant\\Documents>\r\n```\r\nThe command is very similar to the ssh command but drops the user into a remote powershell session. The command is currently only supported on windows hosts with a guest running via the winrm communicator. The command, similar to `vagrant ssh` also supports a `--command` argument that can execute any powershell command over winrm.\r\n\r\nWould be great to add @sneal 's repl to add support for linux hosts.\r\n\r\n*note:* this PR is a squashed and final version of #4330 \r\n\r\nFixes https://github.com/mitchellh/vagrant/issues/5725","created_at":"2014-08-27T19:24:45Z","updated_at":"2015-06-06T03:48:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7e22c0900050cf141f6d06f8abd2bde0ee18429e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400/commits","review_comments_url":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400/comments","review_comment_url":"https://api.github.com/repos/mitchellh/vagrant/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mitchellh/vagrant/issues/4400/comments","statuses_url":"https://api.github.com/repos/mitchellh/vagrant/statuses/740877065a8fc0b777cc7aa51b4fb074307affbd","head":{"label":"mwrock:ps-cmd","ref":"ps-cmd","sha":"740877065a8fc0b777cc7aa51b4fb074307affbd","user":{"login":"mwrock","id":655165,"avatar_url":"https://avatars.githubusercontent.com/u/655165?v=3","gravatar_id":"","url":"https://api.github.com/users/mwrock","html_url":"https://github.com/mwrock","followers_url":"https://api.github.com/users/mwrock/followers","following_url":"https://api.github.com/users/mwrock/following{/other_user}","gists_url":"https://api.github.com/users/mwrock/gists{/gist_id}","starred_url":"https://api.github.com/users/mwrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwrock/subscriptions","organizations_url":"https://api.github.com/users/mwrock/orgs","repos_url":"https://api.github.com/users/mwrock/repos","events_url":"https://api.github.com/users/mwrock/events{/privacy}","received_events_url":"https://api.github.com/users/mwrock/received_events","type":"User","site_admin":false},"repo":{"id":21641701,"name":"vagrant","full_name":"mwrock/vagrant","owner":{"login":"mwrock","id":655165,"avatar_url":"https://avatars.githubusercontent.com/u/655165?v=3","gravatar_id":"","url":"https://api.github.com/users/mwrock","html_url":"https://github.com/mwrock","followers_url":"https://api.github.com/users/mwrock/followers","following_url":"https://api.github.com/users/mwrock/following{/other_user}","gists_url":"https://api.github.com/users/mwrock/gists{/gist_id}","starred_url":"https://api.github.com/users/mwrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mwrock/subscriptions","organizations_url":"https://api.github.com/users/mwrock/orgs","repos_url":"https://api.github.com/users/mwrock/repos","events_url":"https://api.github.com/users/mwrock/events{/privacy}","received_events_url":"https://api.github.com/users/mwrock/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mwrock/vagrant","description":"Vagrant is a tool for building and distributing development environments.","fork":true,"url":"https://api.github.com/repos/mwrock/vagrant","forks_url":"https://api.github.com/repos/mwrock/vagrant/forks","keys_url":"https://api.github.com/repos/mwrock/vagrant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mwrock/vagrant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mwrock/vagrant/teams","hooks_url":"https://api.github.com/repos/mwrock/vagrant/hooks","issue_events_url":"https://api.github.com/repos/mwrock/vagrant/issues/events{/number}","events_url":"https://api.github.com/repos/mwrock/vagrant/events","assignees_url":"https://api.github.com/repos/mwrock/vagrant/assignees{/user}","branches_url":"https://api.github.com/repos/mwrock/vagrant/branches{/branch}","tags_url":"https://api.github.com/repos/mwrock/vagrant/tags","blobs_url":"https://api.github.com/repos/mwrock/vagrant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mwrock/vagrant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mwrock/vagrant/git/refs{/sha}","trees_url":"https://api.github.com/repos/mwrock/vagrant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mwrock/vagrant/statuses/{sha}","languages_url":"https://api.github.com/repos/mwrock/vagrant/languages","stargazers_url":"https://api.github.com/repos/mwrock/vagrant/stargazers","contributors_url":"https://api.github.com/repos/mwrock/vagrant/contributors","subscribers_url":"https://api.github.com/repos/mwrock/vagrant/subscribers","subscription_url":"https://api.github.com/repos/mwrock/vagrant/subscription","commits_url":"https://api.github.com/repos/mwrock/vagrant/commits{/sha}","git_commits_url":"https://api.github.com/repos/mwrock/vagrant/git/commits{/sha}","comments_url":"https://api.github.com/repos/mwrock/vagrant/comments{/number}","issue_comment_url":"https://api.github.com/repos/mwrock/vagrant/issues/comments{/number}","contents_url":"https://api.github.com/repos/mwrock/vagrant/contents/{+path}","compare_url":"https://api.github.com/repos/mwrock/vagrant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mwrock/vagrant/merges","archive_url":"https://api.github.com/repos/mwrock/vagrant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mwrock/vagrant/downloads","issues_url":"https://api.github.com/repos/mwrock/vagrant/issues{/number}","pulls_url":"https://api.github.com/repos/mwrock/vagrant/pulls{/number}","milestones_url":"https://api.github.com/repos/mwrock/vagrant/milestones{/number}","notifications_url":"https://api.github.com/repos/mwrock/vagrant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mwrock/vagrant/labels{/name}","releases_url":"https://api.github.com/repos/mwrock/vagrant/releases{/id}","created_at":"2014-07-09T06:23:46Z","updated_at":"2015-01-17T19:39:56Z","pushed_at":"2015-06-05T13:33:45Z","git_url":"git://github.com/mwrock/vagrant.git","ssh_url":"git@github.com:mwrock/vagrant.git","clone_url":"https://github.com/mwrock/vagrant.git","svn_url":"https://github.com/mwrock/vagrant","homepage":"http://www.vagrantup.com","size":18641,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mitchellh:master","ref":"master","sha":"b420da067812be9feb3cfab07680ab4dcff66bed","user":{"login":"mitchellh","id":1299,"avatar_url":"https://avatars.githubusercontent.com/u/1299?v=3","gravatar_id":"","url":"https://api.github.com/users/mitchellh","html_url":"https://github.com/mitchellh","followers_url":"https://api.github.com/users/mitchellh/followers","following_url":"https://api.github.com/users/mitchellh/following{/other_user}","gists_url":"https://api.github.com/users/mitchellh/gists{/gist_id}","starred_url":"https://api.github.com/users/mitchellh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mitchellh/subscriptions","organizations_url":"https://api.github.com/users/mitchellh/orgs","repos_url":"https://api.github.com/users/mitchellh/repos","events_url":"https://api.github.com/users/mitchellh/events{/privacy}","received_events_url":"https://api.github.com/users/mitchellh/received_events","type":"User","site_admin":false},"repo":{"id":481872,"name":"vagrant","full_name":"mitchellh/vagrant","owner":{"login":"mitchellh","id":1299,"avatar_url":"https://avatars.githubusercontent.com/u/1299?v=3","gravatar_id":"","url":"https://api.github.com/users/mitchellh","html_url":"https://github.com/mitchellh","followers_url":"https://api.github.com/users/mitchellh/followers","following_url":"https://api.github.com/users/mitchellh/following{/other_user}","gists_url":"https://api.github.com/users/mitchellh/gists{/gist_id}","starred_url":"https://api.github.com/users/mitchellh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mitchellh/subscriptions","organizations_url":"https://api.github.com/users/mitchellh/orgs","repos_url":"https://api.github.com/users/mitchellh/repos","events_url":"https://api.github.com/users/mitchellh/events{/privacy}","received_events_url":"https://api.github.com/users/mitchellh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mitchellh/vagrant","description":"Vagrant is a tool for building and distributing development environments.","fork":false,"url":"https://api.github.com/repos/mitchellh/vagrant","forks_url":"https://api.github.com/repos/mitchellh/vagrant/forks","keys_url":"https://api.github.com/repos/mitchellh/vagrant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mitchellh/vagrant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mitchellh/vagrant/teams","hooks_url":"https://api.github.com/repos/mitchellh/vagrant/hooks","issue_events_url":"https://api.github.com/repos/mitchellh/vagrant/issues/events{/number}","events_url":"https://api.github.com/repos/mitchellh/vagrant/events","assignees_url":"https://api.github.com/repos/mitchellh/vagrant/assignees{/user}","branches_url":"https://api.github.com/repos/mitchellh/vagrant/branches{/branch}","tags_url":"https://api.github.com/repos/mitchellh/vagrant/tags","blobs_url":"https://api.github.com/repos/mitchellh/vagrant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mitchellh/vagrant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mitchellh/vagrant/git/refs{/sha}","trees_url":"https://api.github.com/repos/mitchellh/vagrant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mitchellh/vagrant/statuses/{sha}","languages_url":"https://api.github.com/repos/mitchellh/vagrant/languages","stargazers_url":"https://api.github.com/repos/mitchellh/vagrant/stargazers","contributors_url":"https://api.github.com/repos/mitchellh/vagrant/contributors","subscribers_url":"https://api.github.com/repos/mitchellh/vagrant/subscribers","subscription_url":"https://api.github.com/repos/mitchellh/vagrant/subscription","commits_url":"https://api.github.com/repos/mitchellh/vagrant/commits{/sha}","git_commits_url":"https://api.github.com/repos/mitchellh/vagrant/git/commits{/sha}","comments_url":"https://api.github.com/repos/mitchellh/vagrant/comments{/number}","issue_comment_url":"https://api.github.com/repos/mitchellh/vagrant/issues/comments{/number}","contents_url":"https://api.github.com/repos/mitchellh/vagrant/contents/{+path}","compare_url":"https://api.github.com/repos/mitchellh/vagrant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mitchellh/vagrant/merges","archive_url":"https://api.github.com/repos/mitchellh/vagrant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mitchellh/vagrant/downloads","issues_url":"https://api.github.com/repos/mitchellh/vagrant/issues{/number}","pulls_url":"https://api.github.com/repos/mitchellh/vagrant/pulls{/number}","milestones_url":"https://api.github.com/repos/mitchellh/vagrant/milestones{/number}","notifications_url":"https://api.github.com/repos/mitchellh/vagrant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mitchellh/vagrant/labels{/name}","releases_url":"https://api.github.com/repos/mitchellh/vagrant/releases{/id}","created_at":"2010-01-21T08:34:27Z","updated_at":"2015-06-06T00:54:20Z","pushed_at":"2015-06-05T20:36:23Z","git_url":"git://github.com/mitchellh/vagrant.git","ssh_url":"git@github.com:mitchellh/vagrant.git","clone_url":"https://github.com/mitchellh/vagrant.git","svn_url":"https://github.com/mitchellh/vagrant","homepage":"http://www.vagrantup.com","size":43312,"stargazers_count":9423,"watchers_count":9423,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2055,"mirror_url":null,"open_issues_count":503,"forks":2055,"open_issues":503,"watchers":9423,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400"},"html":{"href":"https://github.com/mitchellh/vagrant/pull/4400"},"issue":{"href":"https://api.github.com/repos/mitchellh/vagrant/issues/4400"},"comments":{"href":"https://api.github.com/repos/mitchellh/vagrant/issues/4400/comments"},"review_comments":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400/comments"},"review_comment":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mitchellh/vagrant/pulls/4400/commits"},"statuses":{"href":"https://api.github.com/repos/mitchellh/vagrant/statuses/740877065a8fc0b777cc7aa51b4fb074307affbd"}}}}