Skip to content

Commit 18757f2

Browse files
committed
support powershellcore
1 parent eaad16b commit 18757f2

File tree

9 files changed

+60
-44
lines changed

9 files changed

+60
-44
lines changed

examples/init.pp

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
package { 'PSExcel':
1919
ensure => latest,
20-
provider => 'psmodule',
20+
provider => 'windowspowershell',
2121
source => 'PSGallery',
2222
}
23+
24+
package { 'Pester':
25+
ensure => latest,
26+
provider => 'powershellcore',
27+
source => 'PSGallery',
28+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'puppet/util/feature'
2+
3+
Puppet.features.add(:powershellgetcore) do
4+
command = '"Import-Module powershellget; $mod = Get-Module powershellget; $mod.Name"'
5+
output = `pwsh -noprofile -executionpolicy bypass -command #{command}`
6+
output.downcase.strip == 'powershellget'
7+
end

lib/puppet/feature/powershellget.rb lib/puppet/feature/powershellgetwindows.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'puppet/util/feature'
22

3-
Puppet.features.add(:powershellget) do
3+
Puppet.features.add(:powershellgetwindows) do
44
command = '"Import-Module powershellget; $mod = Get-Module powershellget; $mod.Name"'
55
output = `powershell.exe -noprofile -executionpolicy bypass -command #{command}`
66
output.downcase.strip == 'powershellget'

lib/puppet/provider/package/psmodule.rb lib/puppet/provider/package/powershellcore.rb

+11-22
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
require 'puppet/provider/package'
22
require 'json'
33

4-
Puppet::Type.type(:package).provide :psmodule, parent: Puppet::Provider::Package do
5-
confine operatingsystem: :windows
6-
confine feature: :powershellget
7-
8-
has_feature :installable
9-
has_feature :uninstallable
10-
has_feature :upgradeable
11-
has_feature :versionable
12-
13-
commands powershell:
14-
if File.exist?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
15-
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
16-
elsif File.exist?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
17-
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
18-
else
19-
'powershell.exe'
20-
end
4+
Puppet::Type.type(:package).provide :powershellcore, parent: Puppet::Provider::Package do
5+
initvars
6+
has_feature :installable, :uninstallable, :upgradeable, :versionable
7+
commands pwsh: 'pwsh'
218

229
def self.invoke_ps_command(command)
23-
result = powershell(['-noprofile', '-executionpolicy', 'bypass', '-command', command])
10+
result = pwsh(['-NoProfile', '-NonInteractive', '-NoLogo', '-Command', "$ProgressPreference = 'SilentlyContinue'; #{command}"])
11+
Puppet.debug result.exitstatus
12+
Puppet.debug result.lines
2413
result.lines
2514
end
2615

@@ -62,29 +51,29 @@ def self.instances_command
6251
[ordered]@{
6352
'name' = $_.Name
6453
'ensure' = @(($_.Group).Version)
65-
'provider' = 'psmodule'
54+
'provider' = '#{name}'
6655
} | ConvertTo-Json -Depth 99 -Compress
6756
}
6857
COMMAND
6958
end
7059

7160
def install_command
72-
command = "Install-Module #{@resource[:name]} -Force"
61+
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
7362
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
7463
command << " -Repository #{@resource[:source]}" if @resource[:source]
7564
command
7665
end
7766

7867
def uninstall_command
79-
"Uninstall-Module #{@resource[:name]} -AllVersions -Force"
68+
"Uninstall-Module #{@resource[:name]} -AllVersions -Scope AllUsers -Force"
8069
end
8170

8271
def latest_command
8372
"$mod = Find-Module #{@resource[:name]}; $mod.Version.ToString()"
8473
end
8574

8675
def update_command
87-
command = "Install-Module #{@resource[:name]} -Force"
76+
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
8877
command << " -Repository #{@resource[:source]}" if @resource[:source]
8978
command
9079
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Puppet::Type.type(:package).provide(:windowspowershell, parent: :powershellcore) do
2+
initvars
3+
confine operatingsystem: :windows
4+
confine feature: :powershellgetwindows
5+
commands powershell: 'powershell'
6+
7+
def self.invoke_ps_command(command)
8+
result = powershell(['-noprofile', '-executionpolicy', 'bypass', '-command', command])
9+
result.lines
10+
end
11+
end

lib/puppet/provider/psrepository/windows.rb lib/puppet/provider/psrepository/powershellcore.rb

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
Puppet::Type.type(:psrepository).provide(:windows) do
2-
confine operatingsystem: :windows
3-
confine feature: :powershellget
4-
5-
commands powershell:
6-
if File.exist?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
7-
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
8-
elsif File.exist?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
9-
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
10-
else
11-
'powershell.exe'
12-
end
13-
1+
Puppet::Type.type(:psrepository).provide(:powershellcore) do
2+
initvars
3+
commands pwsh: 'pwsh'
144
mk_resource_methods
155

166
def initialize(value = {})
@@ -19,7 +9,9 @@ def initialize(value = {})
199
end
2010

2111
def self.invoke_ps_command(command)
22-
result = powershell(['-noprofile', '-executionpolicy', 'bypass', '-command', command])
12+
result = pwsh(['-NoProfile', '-NonInteractive', '-NoLogo', '-Command', "$ProgressPreference = 'SilentlyContinue'; #{command}"])
13+
Puppet.debug result.exitstatus
14+
Puppet.debug result.lines
2315
result.lines
2416
end
2517

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Puppet::Type.type(:psrepository).provide(:windowspowershell, parent: :powershellcore) do
2+
initvars
3+
confine operatingsystem: :windows
4+
confine feature: :powershellgetwindows
5+
commands powershell: 'powershell'
6+
7+
def self.invoke_ps_command(command)
8+
result = powershell(['-noprofile', '-executionpolicy', 'bypass', '-command', command])
9+
result.lines
10+
end
11+
end

spec/unit/puppet/provider/psmodule_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
provider_class = Puppet::Type.type(:package).provider(:psmodule)
3+
provider_class = Puppet::Type.type(:package).provider(:windowspowershell)
44

55
describe provider_class do
66
before(:each) do
@@ -11,14 +11,14 @@
1111
allow(provider_class).to receive(:invoke_ps_command).with(
1212
provider_class.instances_command).and_return(
1313
[
14-
'{"name":"PackageManagement","ensure":["1.1.6.0","1.1.7.0"],"provider":"psmodule"}',
15-
'{"name":"Pester","ensure":["4.0.8"],"provider":"psmodule"}',
16-
'{"name":"PowerShellGet","ensure":["1.5.0.0"],"provider":"psmodule"}'
14+
'{"name":"PackageManagement","ensure":["1.1.6.0","1.1.7.0"],"provider":"windowspowershell"}',
15+
'{"name":"Pester","ensure":["4.0.8"],"provider":"windowspowershell"}',
16+
'{"name":"PowerShellGet","ensure":["1.5.0.0"],"provider":"windowspowershell"}'
1717
]
1818
)
1919
end
2020
describe :instances do
21-
specify 'returns an array of :psmodule providers' do
21+
specify 'returns an array of :windowspowershell providers' do
2222
instances = provider_class.instances
2323
expect(instances.count).to eq(3)
2424
expect(instances).to all(be_instance_of(provider_class))

spec/unit/puppet/provider/psrepository_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
provider_class = Puppet::Type.type(:psrepository).provider(:windows)
3+
provider_class = Puppet::Type.type(:psrepository).provider(:windowspowershell)
44

55
describe provider_class do
66
before(:each) do

0 commit comments

Comments
 (0)