@@ -19,7 +19,7 @@ class PowerShell
19
19
# Number of seconds to wait while attempting to get powershell version
20
20
DEFAULT_VERSION_DETECTION_TIMEOUT = 30
21
21
# Names of the powershell executable
22
- POWERSHELL_NAMES = [ "powershell " , "pwsh " ] . map ( &:freeze ) . freeze
22
+ POWERSHELL_NAMES = [ "pwsh " , "powershell " ] . map ( &:freeze ) . freeze
23
23
# Paths to powershell executable
24
24
POWERSHELL_PATHS = [
25
25
"%SYSTEMROOT%/System32/WindowsPowerShell/v1.0" ,
@@ -33,11 +33,29 @@ class PowerShell
33
33
# @return [String|nil] a powershell executable, depending on environment
34
34
def self . executable
35
35
if !defined? ( @_powershell_executable )
36
+ prefer_name = ENV [ "VAGRANT_PREFERRED_POWERSHELL" ] . to_s . sub ( ".exe" , "" )
37
+ if !POWERSHELL_NAMES . include? ( prefer_name )
38
+ prefer_name = POWERSHELL_NAMES . first
39
+ end
40
+
41
+ LOGGER . debug ( "preferred powershell executable name: #{ prefer_name } " )
42
+
36
43
# First start with detecting executable on configured path
37
- POWERSHELL_NAMES . detect do |psh |
38
- return @_powershell_executable = psh if Which . which ( psh )
39
- psh += ".exe"
40
- return @_powershell_executable = psh if Which . which ( psh )
44
+ found_shells = Hash . new . tap do |found |
45
+ POWERSHELL_NAMES . each do |psh |
46
+ psh_path = Which . which ( psh )
47
+ psh_path = Which . which ( psh + ".exe" ) if !psh_path
48
+ next if !psh_path
49
+
50
+ LOGGER . debug ( "detected powershell for #{ psh . inspect } - #{ psh_path } " )
51
+ found [ psh ] = psh_path
52
+ end
53
+ end
54
+
55
+ # Done if preferred shell was found
56
+ if found_shells . key? ( prefer_name )
57
+ LOGGER . debug ( "using preferred powershell #{ prefer_name . inspect } - #{ found_shells [ prefer_name ] } " )
58
+ return @_powershell_executable = found_shells [ prefer_name ]
41
59
end
42
60
43
61
# Now attempt with paths
@@ -48,16 +66,28 @@ def self.executable
48
66
49
67
paths . each do |psh_path |
50
68
POWERSHELL_NAMES . each do |psh |
69
+ next if found_shells . key? ( psh )
70
+
51
71
path = File . join ( psh_path , psh )
52
- return @_powershell_executable = path if Which . which ( path )
72
+ [ path , "#{ path } .exe" , path . sub ( /^([A-Za-z]):/ , "/mnt/\\ 1" ) ] . each do |full_path |
73
+ if File . executable? ( full_path )
74
+ found_shells [ psh ] = full_path
75
+ break
76
+ end
77
+ end
78
+ end
79
+ end
53
80
54
- path += ".exe"
55
- return @_powershell_executable = path if Which . which ( path )
81
+ # Done if preferred shell was found
82
+ if found_shells . key? ( prefer_name )
83
+ LOGGER . debug ( "using preferred powershell #{ prefer_name . inspect } - #{ found_shells [ prefer_name ] } " )
84
+ return @_powershell_executable = found_shells [ prefer_name ]
85
+ end
56
86
57
- # Finally test the msys2 style path
58
- path = path . sub ( /^([A-Za-z]):/ , "/mnt/ \\ 1" )
59
- return @_powershell_executable = path if Which . which ( path )
60
- end
87
+ # Iterate names and return first found
88
+ POWERSHELL_NAMES . each do | psh |
89
+ LOGGER . debug ( "using powershell #{ prefer_name . inspect } - #{ found_shells [ prefer_name ] } " )
90
+ return @_powershell_executable = found_shells [ psh ] if found_shells . key? ( psh )
61
91
end
62
92
end
63
93
@_powershell_executable
@@ -94,6 +124,7 @@ def self.execute(path, *args, **opts, &block)
94
124
"-NoProfile" ,
95
125
"-NonInteractive" ,
96
126
"-ExecutionPolicy" , "Bypass" ,
127
+ "-Command" ,
97
128
"#{ env } &('#{ path } ')" ,
98
129
args
99
130
] . flatten
0 commit comments