-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChocolateyManagement.ps1
289 lines (236 loc) · 7.5 KB
/
ChocolateyManagement.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<#
.SYNOPSIS
Chocolatey Management Script
.DESCRIPTION
Script to be used in (e. g.) remote monitoring and management solutions to automatically install and update software.
.INPUTS
No parameters. Variables are supposed to be set by the rmm solution this script is used in.
.OUTPUTS
None
.RELATED LINKS
GitHub: https://github.com/MichaelSchoenburg/ChocolateyManagement
.NOTES
Author: Michael Schönburg
Version: v1.0
Last Edit: 03.04.2024
This projects code loosely follows the PowerShell Practice and Style guide, as well as Microsofts PowerShell scripting performance considerations.
Style guide: https://poshcode.gitbook.io/powershell-practice-and-style/
Performance Considerations: https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations?view=powershell-7.1
#>
#region INITIALIZATION
<#
Libraries, Modules, ...
#>
#endregion INITIALIZATION
#region DECLARATIONS
<#
Declare local variables and global variables
#>
# The following variables should be set through your rmm solution.
# If you want to install the package, set the respective variable to 1 (integer). If you want to uninstall the package, set the respective variable to 0 (integer). Everything other than 0 or 1 will result in the package not being installed, but ignored.
# Tip: PowerShell variables are not case sensitive.
<#
$VSCodeNoDesktopIcon = 1 # Will not create a desktop icon for Visual Studio Code
$VSCodeNoDesktopIcon = 0 # Will create a desktop icon for Visual Studio Code
$VSCode = 1 # Will be installed
$7zip = 0 # Will be uninstalled
$adobereaderdc = $null # Will be ignored thus not installed or uninstalled
...
#>
$AllPkgs = @()
$AllPkgs += [PSCustomObject]@{
FriendlyName = "Visual Studio Code"
PkgName = 'vscode'
Install = $VSCode
Arguments = if ($VSCodeNoDesktopIcon -eq 1) {
'/NoDesktopIcon'
} else {
$null
}
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "7-Zip"
PkgName='7zip'
Install = $7zip
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "Adobe Reader DC"
PkgName = 'adobereader'
Install = $adobereaderdc
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "ZoomIt"
PkgName = 'zoomit'
Install = $zoomit
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "Zoom"
PkgName = 'Zoom'
Install = $zoom
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "PowerToys"
PkgName = 'powertoys'
Install = $powertoys
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "Microsoft Teams (new)"
PkgName = 'microsoft-teams-new-bootstrapper'
Install = $MSTeams
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "PDF24"
PkgName = 'pdf24'
Install = $PDF24
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "VLC media player"
PkgName = 'vlc'
Install = $VLCmediaplayer
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "TreeSize Free"
PkgName = 'treesizefree'
Install = $TreeSizeFree
}
# Hint regarding Visual Studio Code PowerShell Extension from the author Pascal Berger:
# Unfortunately it is not possible to install a specific version of an extension through the Visual Studio Code CLI.
# Therefore this package always installs the latest version of the extension
# and the extension afterwards needs to be updated through the Visual Studio Code extension manager.
$AllPkgs += [PSCustomObject]@{
FriendlyName = "Visual Studio Code PowerShell Extension"
PkgName = 'vscode-powershell'
Install = $VSCodePowerShell
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "WinSCP"
PkgName = 'winscp.install'
Install = $WinSCP
}
$AllPkgs += [PSCustomObject]@{
FriendlyName = "PowerShell 7"
PkgName = 'powershell-core'
Install = $PS7
}
#endregion DECLARATIONS
#region FUNCTIONS
<#
Declare Functions
#>
function Write-ConsoleLog {
<#
.SYNOPSIS
Logs an event to the console.
.DESCRIPTION
Writes text to the console with the current date (US format) in front of it.
.PARAMETER Text
Event/text to be outputted to the console.
.EXAMPLE
Write-ConsoleLog -Text 'Subscript XYZ called.'
Long form
.EXAMPLE
Log 'Subscript XYZ called.
Short form
#>
[alias('Log')]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
Position = 0)]
[string]
$Text
)
# Save current VerbosePreference
$VerbosePreferenceBefore = $VerbosePreference
# Enable verbose output
$VerbosePreference = 'Continue'
# Write verbose output
Write-Output "$( Get-Date -Format 'MM/dd/yyyy HH:mm:ss' ) - $( $Text )"
# Restore current VerbosePreference
$VerbosePreference = $VerbosePreferenceBefore
}
function Install-ChocoPkg {
[CmdletBinding()]
param (
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName
)]
[string]
$PkgName,
# I couldn't find a way to read the package name programmatically using Chocolatey, so it has to be specified manually.
[Parameter(
ValueFromPipelineByPropertyName
)]
[string]
$FriendlyName = $PkgName,
[Parameter(
ValueFromPipelineByPropertyName
)]
[int]
$Install = 1,
[Parameter(
ValueFromPipelineByPropertyName
)]
[string]
$Arguments
)
process {
if ($Install -eq 1) {
Log "$($FriendlyName) should be installed."
$Result = choco list --limit-output --exact $PkgName | ConvertFrom-Csv -delimiter "|" -Header Id, Version
if ($Result.Count -eq 0) {
Log "$($FriendlyName) is not yet installed. Start installation..."
if ($Arguments) {
choco install $PkgName --params $Arguments --confirm
} else {
choco install $PkgName --confirm
}
} else {
Log "$($FriendlyName) is already installed in version $($Result.Version)."
}
} elseif ($Install -eq 0) {
Log "$($FriendlyName) should be uninstalled."
$Result = choco list --limit-output --exact $PkgName | ConvertFrom-Csv -delimiter "|" -Header Id, Version
if ($Result.Count -eq 0) {
Log "$($FriendlyName) is not even installed. Skipping..."
} else {
Log "$($FriendlyName) is installed in version $($Result.Version). Uninstalling..."
choco uninstall $PkgName --confirm
}
} else {
Log "$($FriendlyName) should NOT be installed."
}
}
}
#endregion FUNCTIONS
#region EXECUTION
#region ChocoInstall
<#
Install Chocolatey
#>
# Check if Chocolatey is installed
if (Get-Command -Name choco.exe -ErrorAction SilentlyContinue) {
Log "Chocolatey is already installed."
} else {
Log "Chocolatey is not installed yet. Start installation..."
# Install Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-WebRequest https://community.chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression
}
#endregion ChocoInstall
#region ChocoPkgs
<#
Install packages
#>
$AllPkgs | Install-ChocoPkg
#endregion ChocoPkgs
#region ChocoUpdate
<#
Update all packages
#>
Log 'Updating all packages...'
choco upgrade all --confirm
#endregion ChocoUpdate
#endregion EXECUTION