|
3 | 3 | require File.join(File.dirname(__FILE__), '../../../puppet_x/puppetlabs/powershell/powershell_manager') |
4 | 4 |
|
5 | 5 | Puppet::Type.type(:exec).provide :powershell, :parent => Puppet::Provider::Exec do |
| 6 | + confine :operatingsystem => :windows |
| 7 | + |
6 | 8 | commands :powershell => |
7 | 9 | if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe") |
8 | 10 | "#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe" |
9 | 11 | elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe") |
10 | 12 | "#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe" |
11 | | - elsif !Puppet::Util::Platform.windows? |
12 | | - 'pwsh' |
13 | 13 | else |
14 | 14 | 'powershell.exe' |
15 | 15 | end |
@@ -68,33 +68,21 @@ def ps_manager |
68 | 68 | end |
69 | 69 |
|
70 | 70 | def run(command, check = false) |
71 | | - if Puppet::Util::Platform.windows? |
72 | | - if !PuppetX::PowerShell::PowerShellManager.supported? |
73 | | - self.class.upgrade_message |
74 | | - write_script(command) do |native_path| |
75 | | - # Ideally, we could keep a handle open on the temp file in this |
76 | | - # process (to prevent TOCTOU attacks), and execute powershell |
77 | | - # with -File <path>. But powershell complains that it can't open |
78 | | - # the file for exclusive access. If we close the handle, then an |
79 | | - # attacker could modify the file before we invoke powershell. So |
80 | | - # we redirect powershell's stdin to read from the file. Current |
81 | | - # versions of Windows use per-user temp directories with strong |
82 | | - # permissions, but I'd rather not make (poor) assumptions. |
83 | | - return super("cmd.exe /c \"\"#{native_path(command(:powershell))}\" #{legacy_args} -Command - < \"#{native_path}\"\"", check) |
84 | | - end |
85 | | - else |
86 | | - return ps_manager.execute_resource(command, resource) |
87 | | - end |
88 | | - else |
| 71 | + if !PuppetX::PowerShell::PowerShellManager.supported? |
| 72 | + self.class.upgrade_message |
89 | 73 | write_script(command) do |native_path| |
90 | 74 | # Ideally, we could keep a handle open on the temp file in this |
91 | 75 | # process (to prevent TOCTOU attacks), and execute powershell |
92 | 76 | # with -File <path>. But powershell complains that it can't open |
93 | 77 | # the file for exclusive access. If we close the handle, then an |
94 | 78 | # attacker could modify the file before we invoke powershell. So |
95 | | - # we redirect powershell's stdin to read from the file. |
96 | | - return super("sh -c \"#{native_path(command(:powershell))} #{posix_args} -Command - < #{native_path}\"", check) |
| 79 | + # we redirect powershell's stdin to read from the file. Current |
| 80 | + # versions of Windows use per-user temp directories with strong |
| 81 | + # permissions, but I'd rather not make (poor) assumptions. |
| 82 | + return super("cmd.exe /c \"\"#{native_path(command(:powershell))}\" #{legacy_args} -Command - < \"#{native_path}\"\"", check) |
97 | 83 | end |
| 84 | + else |
| 85 | + return ps_manager.execute_resource(command, resource) |
98 | 86 | end |
99 | 87 | end |
100 | 88 |
|
@@ -126,10 +114,4 @@ def native_path(path) |
126 | 114 | def legacy_args |
127 | 115 | '-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass' |
128 | 116 | end |
129 | | - |
130 | | - def posix_args |
131 | | - # Note - using -ExecutionPolicy causes PowerShell to abort |
132 | | - # https://github.com/PowerShell/PowerShell/issues/2742 |
133 | | - '-NoProfile -NonInteractive -NoLogo' |
134 | | - end |
135 | 117 | end |
0 commit comments