11<#
22 . SYNOPSIS
3- Installs a Chocolatey package a repository.
3+ Installs a Chocolatey package a repository.
44
55 . DESCRIPTION
6- Installs a package from a Chocolatey repository like Chocolatey.org.
6+ Installs a package from a Chocolatey repository like Chocolatey.org.
77
8- Relevant Dependency metadata:
9- Name: The name of the package
10- Version: Used to identify existing installs meeting this criteria. Defaults to 'latest'
11- Source: Source Uri. Defaults to https://chocolatey.org/api/v2/
8+ Relevant Dependency metadata:
9+ Name: The name of the package
10+ Version: Used to identify existing installs meeting this criteria. Defaults to 'latest'
11+ Source: Source Uri. Defaults to https://chocolatey.org/api/v2/
1212
1313 . PARAMETER Force
14- If specified and the package is already installed, force the install again.
14+ If specified and the package is already installed, force the install again.
1515
1616 . PARAMETER PSDependAction
17- Test, or Install the package. Defaults to Install
17+ Test, or Install the package. Defaults to Install
1818
19- Test: Return true or false on whether the dependency is in place
20- Install: Install the dependency
19+ Test: Return true or false on whether the dependency is in place
20+ Install: Install the dependency
2121
2222 . EXAMPLE
23-
24- @{
25- 'git' = @{
26- DependencyType = 'Chocolatey'
27- Version = '2.0.2'
28- }
23+ @{
24+ 'git' = @{
25+ DependencyType = 'Chocolatey'
26+ Version = '2.0.2'
2927 }
28+ }
3029
31- # Install version 2.0.2 of git via Chocolatey.org
30+ # Install version 2.0.2 of git via Chocolatey.org
3231
3332 . EXAMPLE
34-
35- @{
36- 'git' = @{
37- DependencyType = 'Chocolatey'
38- Source = 'https://feed.mycompany.com'
39- }
33+ @{
34+ 'git' = @{
35+ DependencyType = 'Chocolatey'
36+ Source = 'https://feed.mycompany.com'
4037 }
38+ }
4139
42- # Install the latest version of git from the Chocolatey feed at https://feed.mycompany.com
40+ # Install the latest version of git from the Chocolatey feed at https://feed.mycompany.com
4341
4442 . EXAMPLE
45-
46- @{
47- PSDependOptions = @{
48- DependencyType = 'Chocolatey'
49- }
50- 'git.portable' = @{
51- Version = 'latest'
52- Parameters = @{
53- Force = $true
54- }
43+ @{
44+ PSDependOptions = @{
45+ DependencyType = 'Chocolatey'
46+ }
47+ 'git.portable' = @{
48+ Version = 'latest'
49+ Parameters = @{
50+ Force = $true
5551 }
56- 'lessmsi' = 'latest'
57- 'putty' = 'latest'
5852 }
53+ 'lessmsi' = 'latest'
54+ 'putty' = 'latest'
55+ }
5956
60- # Installs the list of Chocolatey packages from Chocolatey.org using the Global PSDependOptions to limit repetition.
57+ # Installs the list of Chocolatey packages from Chocolatey.org using the Global PSDependOptions to limit repetition.
6158
6259#>
6360[CmdletBinding ()]
6461param (
6562 [PSTypeName (' PSDepend.Dependency' )]
66- [psobject []]$Dependency ,
63+ [PSObject []]$Dependency ,
6764
6865 [switch ]$Force ,
6966
@@ -73,19 +70,32 @@ param(
7370 [string []]$PSDependAction = @ (' Install' )
7471)
7572
76- function Get-ChocoInstalledPackage
77- {
73+ function Get-ChocoInstalledPackage {
7874 [CmdletBinding ()]
7975 param (
8076 [string ]$Name
8177 )
8278
83- $chocoParams = @ (' list' , " $Name " , ' --limit-output' , ' --exact' , ' --local-only' )
84- Invoke-ExternalCommand - Command ' choco.exe' - Arguments $chocoParams - PassThru | ConvertFrom-Csv - Header ' Name' , ' Version' - Delimiter " |"
79+ $chocoParams = @ (
80+ ' list' ,
81+ " $Name " ,
82+ ' --limit-output' ,
83+ ' --exact' ,
84+ ' --local-only'
85+ )
86+ $invokeExternalCommandSplat = @ {
87+ Command = ' choco.exe'
88+ Arguments = $chocoParams
89+ PassThru = - PassThru
90+ }
91+ $convertFromCsvSplat = @ {
92+ Header = ' Name' , ' Version'
93+ Delimiter = " |"
94+ }
95+ Invoke-ExternalCommand @invokeExternalCommandSplat | ConvertFrom-Csv @convertFromCsvSplat
8596}
8697
87- function Get-ChocoLatestPackage
88- {
98+ function Get-ChocoLatestPackage {
8999 [CmdletBinding ()]
90100 param (
91101 [string ]$Name ,
@@ -96,24 +106,30 @@ function Get-ChocoLatestPackage
96106 )
97107
98108 $chocoParams = @ (' list' , " $Name " , ' --limit-output' , ' --exact' )
99- if ($Source )
100- {
109+ if ($Source ) {
101110 $chocoParams += " --source='$Source '"
102111 }
103112
104- if ($Credential )
105- {
113+ if ($Credential ) {
106114 $username = $credential.UserName
107115 $password = $credential.GetNetworkCredential ().Password
108116 $chocoParams += " --username='$username '"
109117 $chocoParams += " --password='$password '"
110118 }
111119
112- Invoke-ExternalCommand - Command ' choco.exe' - Arguments $chocoParams - PassThru | ConvertFrom-Csv - Header ' Name' , ' Version' - Delimiter " |"
120+ $invokeExternalCommandSplat = @ {
121+ Command = ' choco.exe'
122+ Arguments = $chocoParams
123+ PassThru = - PassThru
124+ }
125+ $convertFromCsvSplat = @ {
126+ Header = ' Name' , ' Version'
127+ Delimiter = " |"
128+ }
129+ Invoke-ExternalCommand @invokeExternalCommandSplat | ConvertFrom-Csv @convertFromCsvSplat
113130}
114131
115- function Invoke-ChocoInstallPackage
116- {
132+ function Invoke-ChocoInstallPackage {
117133 [CmdletBinding ()]
118134 param (
119135 [string ]$Name ,
@@ -127,49 +143,54 @@ function Invoke-ChocoInstallPackage
127143 [Management.Automation.PSCredential ]$Credential
128144 )
129145
130- $chocoParams = @ (' upgrade' , " $Name " , ' --limit-output' , ' --exact' , ' --no-progress' , ' --allow-downgrade' )
131- if ($Force.IsPresent )
132- {
146+ $chocoParams = @ (
147+ ' upgrade' ,
148+ " $Name " ,
149+ ' --limit-output' ,
150+ ' --exact' ,
151+ ' --no-progress' ,
152+ ' --allow-downgrade' ,
153+ ' --yes' # Ensure that we do not get prompted to confirm the install
154+ )
155+ if ($Force.IsPresent ) {
133156 $chocoParams += " --force"
134157 }
135158
136- if ($Source )
137- {
159+ if ($Source ) {
138160 $chocoParams += " --source='$Source '"
139161 }
140162
141- if ($Version -and $Version -ne ' latest' -and $Version -ne ' ' )
142- {
163+ if ($Version -and $Version -ne ' latest' -and $Version -ne ' ' ) {
143164 $chocoParams += " --version='$Version '"
144165 }
145166
146- if ($Credential )
147- {
167+ if ($Credential ) {
148168 $username = $credential.UserName
149169 $password = $credential.GetNetworkCredential ().Password
150170 $chocoParams += " --username='$username '"
151171 $chocoParams += " --password='$password '"
152172 }
153173
154- Invoke-ExternalCommand - Command ' choco.exe' - Arguments $chocoParams
174+ $invokeExternalCommandSplat = @ {
175+ Command = ' choco.exe'
176+ Arguments = $chocoParams
177+ }
178+ Invoke-ExternalCommand @invokeExternalCommandSplat
155179}
156180
157181# Extract data from Dependency
158182$Name = $Dependency.Name
159- if (-not $Name )
160- {
183+ if (-not $Name ) {
161184 $Name = $Dependency.DependencyName
162185}
163186
164187$Version = $Dependency.Version
165- if (-not $Dependency.Version -or $Version -eq ' ' )
166- {
188+ if (-not $Dependency.Version -or $Version -eq ' ' ) {
167189 $Version = ' latest'
168190}
169191
170192$Source = $Dependency.Source
171- if (-not $Dependency.Source -or $Source -eq ' ' )
172- {
193+ if (-not $Dependency.Source -or $Source -eq ' ' ) {
173194 $Source = ' https://chocolatey.org/api/v2/'
174195}
175196
@@ -181,34 +202,29 @@ if (-not (Get-Command -Name 'choco.exe' -ErrorAction SilentlyContinue)) {
181202 # Add TLS 1.2 support
182203 [Net.ServicePointManager ]::SecurityProtocol = [Net.ServicePointManager ]::SecurityProtocol -bor [Net.SecurityProtocolType ]::Tls12
183204
184- do
185- {
205+ do {
186206 $scriptPath = Join-Path - Path $env: TEMP - ChildPath (" {0}.ps1" -f [GUID ]::NewGuid().ToString())
187207 } while (Test-Path - Path $scriptPath )
188208
189- try
190- {
209+ try {
191210 Invoke-WebRequest - UseBasicParsing - Uri $ChocoInstallScriptUrl - OutFile $scriptPath
192211 & $scriptPath
193- }
194- catch
195- {
212+ } catch {
196213 throw " Unable to install Chocolatey from '$scriptUrl '."
197214 }
198215}
199216
200- # if this is a forced install we don't need to check anything, just install the package version requested
201- if ( $Force .IsPresent -and $PSDependAction -contains ' Install ' )
202- {
217+ # If this is a forced install we don't need to check anything,
218+ # just install the package version requested
219+ if ( $Force .IsPresent -and $PSDependAction -contains ' Install ' ) {
203220 $params = @ {
204221 Name = $Name
205222 Version = $Version
206223 Source = $Source
207224 Force = $Force.IsPresent
208225 }
209226
210- if ($Credential )
211- {
227+ if ($Credential ) {
212228 $params.Credential = $Credential
213229 }
214230
@@ -221,20 +237,16 @@ if ($Force.IsPresent -and $PSDependAction -contains 'Install')
221237# get the package if it is installed
222238Write-Verbose " Getting package [$Name ] version, if it is installed."
223239$existingVersion = (Get-ChocoInstalledPackage - Name $Name ).Version
224- if ($existingVersion )
225- {
240+ if ($existingVersion ) {
226241 Write-Verbose " Found package [$Name ] installed with version [$Version ]."
227- }
228- else {
242+ } else {
229243 Write-Verbose " Package [$Name ] not installed."
230244}
231245
232246# Version latest requested, and equal to current
233- if ($Version -ne ' latest' -and $Version -eq $existingVersion )
234- {
247+ if ($Version -ne ' latest' -and $Version -eq $existingVersion ) {
235248 Write-Verbose " You have the requested version [$Version ] of [$Name ]"
236- if ($PSDependAction -contains ' Test' )
237- {
249+ if ($PSDependAction -contains ' Test' ) {
238250 return $true
239251 }
240252
@@ -246,54 +258,48 @@ $repoParams = @{
246258 Name = $Name
247259 Source = $Source
248260}
249- if ($Credential )
250- {
251- $repoParams.Credential = Credential
261+ if ($Credential ) {
262+ $repoParams.Credential = Get-Credential
252263}
253264
254- Write-verbose " Getting latest package [$Name ] version from source [$Source ]."
265+ Write-Verbose " Getting latest package [$Name ] version from source [$Source ]."
255266$repositoryVersion = (Get-ChocoLatestPackage @repoParams ).Version
256- if ($repositoryVersion )
257- {
267+ if ($repositoryVersion ) {
258268 Write-Verbose " Found package [$Name ] version [$Version ] on source [$Source ]."
259- }
260- else
261- {
269+ } else {
262270 Write-Verbose " Package [$Name ] not found on source [$Source ]. Nothing more can be done."
263271 return # cannot continue
264272}
265273
266274# If the version in the remote repository is less than or equal to the version installed, then we have the latest already
267- if ($Version -eq ' latest' -and ([System.Version ]$repositoryVersion -le [System.Version ]$existingVersion ))
268- {
275+ if (
276+ $Version -eq ' latest' -and
277+ ([System.Version ]$repositoryVersion -le [System.Version ]$existingVersion )
278+ ) {
269279 Write-Verbose " You have the latest version of [$Name ], with installed version [$existingVersion ] and Source version [$repositoryVersion ]"
270- if ($PSDependAction -contains ' Test' )
271- {
280+ if ($PSDependAction -contains ' Test' ) {
272281 return $true
273282 }
274283
275284 return
276285}
277286
278- # if we get here then we do not have the latest version installed and that is what has been requested
287+ # if we get here then we do not have the latest version installed and that is
288+ # what has been requested
279289Write-Verbose " You do not have the version requested of [$Name ]: Requested version [$Version ], existing version [$existingVersion ], available version [$repositoryVersion ]."
280- if ($PSDependAction -contains ' Install' )
281- {
290+ if ($PSDependAction -contains ' Install' ) {
282291 $params = @ {
283292 Name = $Name
284293 Version = $Version
285294 Source = $Source
286295 Force = $Force.IsPresent
287296 }
288297
289- if ($Credential )
290- {
298+ if ($Credential ) {
291299 $params.Credential = $Credential
292300 }
293301
294302 Invoke-ChocoInstallPackage @params
295- }
296- elseif ($PSDependAction -contains ' Test' -and $PSDependAction.count -eq 1 )
297- {
303+ } elseif ($PSDependAction -contains ' Test' -and $PSDependAction.count -eq 1 ) {
298304 return $false
299305}
0 commit comments