Skip to content

Commit d097248

Browse files
committedJan 24, 2019
Added support for install_options argument.
install_options can now be specified to allow additional arguments to be passed to the PowerShell Install-Module command, e.g. package { 'foobarmodule': ensure => latest, provider => 'windowspowershell', source => 'PSGallery', install_options => [ '-AllowClobber' ] }
1 parent b6524cf commit d097248

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ package { 'xPSDesiredStateConfiguration':
149149
ensure => latest,
150150
provider => 'windowspowershell',
151151
source => 'PSGallery',
152+
install_options => [ '-AllowClobber' ]
152153
}
153154
154155
package { 'Pester':

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Puppet::Type.type(:package).provide :powershellcore, parent: Puppet::Provider::Package do
55
initvars
6-
has_feature :installable, :uninstallable, :upgradeable, :versionable
6+
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
77
commands pwsh: 'pwsh'
88

99
def self.invoke_ps_command(command)
@@ -44,6 +44,32 @@ def update
4444
self.class.invoke_ps_command update_command
4545
end
4646

47+
# Turns a array of install_options into flags to be passed to a command.
48+
# The options can be passed as a string or hash. Note that passing a hash
49+
# should only be used in case "-foo bar" must be passed,
50+
# Regular flags like '-foobar' must be passed as a string.
51+
# which can be accomplished with:
52+
# install_options => [ '-foobar',{ '-foo' => 'bar' } ]
53+
# This will result in the following being passed as arguments to the command:
54+
# -foobar -foo bar
55+
# @param options [Array]
56+
# @return Concatenated list of options
57+
# @api private
58+
def install_options
59+
return unless @resource[:install_options]
60+
61+
@resource[:install_options].collect do |val|
62+
case val
63+
when Hash
64+
val.keys.sort.collect do |k|
65+
"#{k} #{val[k]}"
66+
end
67+
else
68+
val
69+
end
70+
end.flatten.join(" ")
71+
end
72+
4773
def self.instances_command
4874
# Get-Package is way faster than Get-InstalledModule
4975
<<-COMMAND
@@ -62,6 +88,7 @@ def install_command
6288
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
6389
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
6490
command << " -Repository #{@resource[:source]}" if @resource[:source]
91+
command << " #{install_options}" if @resource[:install_options]
6592
command
6693
end
6794

@@ -76,6 +103,7 @@ def latest_command
76103
def update_command
77104
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
78105
command << " -Repository #{@resource[:source]}" if @resource[:source]
106+
command << " #{install_options}" if @resource[:install_options]
79107
command
80108
end
81109
end

‎lib/puppet/provider/package/windowspowershell.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
initvars
33
confine operatingsystem: :windows
44
confine feature: :powershellgetwindows
5-
has_feature :installable, :uninstallable, :upgradeable, :versionable
5+
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
66
commands powershell: 'powershell'
77

88
def self.invoke_ps_command(command)

0 commit comments

Comments
 (0)
Please sign in to comment.