From b0a41098459431e5924e094fb5ac1194751faee7 Mon Sep 17 00:00:00 2001 From: lyderX05 <69924216+lyderX05@users.noreply.github.com> Date: Thu, 1 Oct 2020 16:11:10 +0530 Subject: [PATCH 1/3] Fixes the issue of Vagrant is unable to execute Get-WindowsOptionalFeature Command. Fixes #11932 --- lib/vagrant/util/platform.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index c8658e185..b160f1bd9 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -139,8 +139,13 @@ module Vagrant return @_windows_hyperv_enabled if defined?(@_windows_hyperv_enabled) @_windows_hyperv_enabled = -> { - ["Get-WindowsOptionalFeature", "Get-WindowsFeature"].each do |cmd_name| - ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor).State" + {:"Get-WindowsOptionalFeature" => ["-Online"], :"Get-WindowsFeature" => []}.each do |cmd_name, arguments| + parameters = arguments.join " " + if cmd_name == "Get-WindowsFeature" + ps_cmd = "if (Get-Command #{cmd_name} -ErrorAction SilentlyContinue){ $(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State } else { 'Disabled' }" + else + ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State" + end begin output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd) return true if output == "Enabled" From 88c5e17c5ce301451712dca5195ce7a32ac0ead5 Mon Sep 17 00:00:00 2001 From: lyderX05 <69924216+lyderX05@users.noreply.github.com> Date: Thu, 1 Oct 2020 16:14:48 +0530 Subject: [PATCH 2/3] Fixes the issue of Vagrant is unable to execute Get-WindowsOptionalFeature Command changes 2 Fixes #11932 --- lib/vagrant/util/platform.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index b160f1bd9..b213add0e 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -141,10 +141,9 @@ module Vagrant @_windows_hyperv_enabled = -> { {:"Get-WindowsOptionalFeature" => ["-Online"], :"Get-WindowsFeature" => []}.each do |cmd_name, arguments| parameters = arguments.join " " + ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State" if cmd_name == "Get-WindowsFeature" ps_cmd = "if (Get-Command #{cmd_name} -ErrorAction SilentlyContinue){ $(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State } else { 'Disabled' }" - else - ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State" end begin output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd) From a7d87498ed4fb0101522f59ef97b06fc367e1692 Mon Sep 17 00:00:00 2001 From: lyderX05 <69924216+lyderX05@users.noreply.github.com> Date: Wed, 14 Oct 2020 00:49:48 +0530 Subject: [PATCH 3/3] Requested Changes Updated on Fixes Fixes the issue of Vagrant is unable to execute Get-WindowsOptionalFeature Command changes 2 Fixes #11932 --- lib/vagrant/util/platform.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index b213add0e..6091d84dd 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -139,18 +139,20 @@ module Vagrant return @_windows_hyperv_enabled if defined?(@_windows_hyperv_enabled) @_windows_hyperv_enabled = -> { - {:"Get-WindowsOptionalFeature" => ["-Online"], :"Get-WindowsFeature" => []}.each do |cmd_name, arguments| - parameters = arguments.join " " - ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State" - if cmd_name == "Get-WindowsFeature" - ps_cmd = "if (Get-Command #{cmd_name} -ErrorAction SilentlyContinue){ $(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor #{parameters}).State } else { 'Disabled' }" - end + check_commands = Array.new.tap do |c| + c << "(Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-Hypervisor -Online).State" + c << "(Get-WindowsFeature -FeatureName Microsoft-Hyper-V-Hypervisor).State" + end + check_commands.each do |ps_cmd| begin output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd) return true if output == "Enabled" rescue Errors::PowerShellInvalidVersion logger.warn("Invalid PowerShell version detected during Hyper-V enable check") return false + rescue Errors::PowerShellError + logger.warn("Powershell command not found or error on execution of command") + return false end end return false