-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChocolatey_Deploy.ps1
200 lines (174 loc) · 9.01 KB
/
Chocolatey_Deploy.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
<#
.SYNOPSIS
Install and configures the popular Windows package manager Chocolatey.
.DESCRIPTION
1. Checks whether chocolatey is already installed.
- Terminates the script if it is so as to avoid misconfiguring.
2. If it isn't installed yet, it install chocolatey.
3. Verifies if installation has been succesfull
4. Configures the default repository as per set parameters (see below)
5. Sets an auto-update configuration
6. Installs a package cache cleaning utility
7. Installs the Chocolatey GUI tool
Deployment tested on:
- Windows 10
- Windows 11
- Windows Sandbox
- Windows Server 2019
- Windows Server 2022
- Windows Server 2022 vNext (Windows Server 2025)
.PARAMETER LocalRepository
(Optional)
Enables a local repository.
Leave blank to use the Chocolatey Community Repository.
Note that using the Community Repository can run you into traffic
limits in a multi-machine deployment scenario.
.PARAMETER LocalRepositoryPath
(Optional)
Specifies the full FQDN or IP, port and path of the local repository.
.PARAMETER LocalRepositoryName
(Optional)
Specifies the name of the local repository.
.PARAMETER DisableCommunityRepository
(Optional)
Allows disabling the Community Repository entirely.
This will only work if a valid local repository has been added.
Usually there should be no need to use this unless you're specifically concerned
about the security of packages from outside your organisation.
.EXAMPLE
To use the default Chocolatey Community Repository, run this:
PS> ./Chocolatey_Deploy
To use a local repository, run either of these:
PS> ./Chocolatey_Deploy $True "http://10.10.10.1:8624/nuget/Thoth/" "THOTH" $False
PS> ./Chocolatey_Deploy $True "http://hercules.cerberus.local:8624/nuget/Hercules/" "HERCULES" $False
The exact port and form of the path for the local repository will depend on
the repository software you are using.
In the examples above, the repository software used is the Inedo ProGet free software.
https://inedo.com/proget
Inedo ProGet can be deployed on Windows (including Windows Server with a free ProGet license),
as well as Linux Docker containers.
The packages can be stored locally or on an SMB/NFS network share.
Please make sure sufficient storage is available.
ProGet can install its own SQL Express database, or be configured with
an external SQL database such as the free edition of Windows SQL Server.
The paid Inedo ProGet versions also support retention policies, LDAP/AD Integration, Load Balancing,
High Availability & Automatic Failover, Multi-Site Replication and AWS/Azure Packet Cloud Storage.
.LINK
https://github.com/gabrielvanca/Chocolatey
.NOTES
Author: Gabriel Vanca
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $False)] [Switch]$LocalRepository = $false,
[Parameter(Mandatory = $False)] [String]$LocalRepositoryPath,
[Parameter(Mandatory = $False)] [String]$LocalRepositoryName,
[Parameter(Mandatory = $False)] [Switch]$DisableCommunityRepository = $false
)
#Requires -RunAsAdministrator
# Force use of TLS 1.2 for all downloads.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Host "Running with the following paramters:"
Write-Host "LocalRepository: $LocalRepository"
if ($LocalRepository -eq $True) {
Write-Host "LocalRepositoryPath: $LocalRepositoryPath"
Write-Host "LocalRepositoryName: $LocalRepositoryName"
Write-Host "DisableCommunityRepository: $DisableCommunityRepository"
}
Write-Host "Starting proceedings"
# Expected path of the choco.exe file.
$chocoInstallPath = "$Env:ProgramData/chocolatey/choco.exe"
if (Test-Path "$chocoInstallPath") {
Write-Host ""
Write-Error "Chocolatey is already installed."
Write-Host "`n"
Start-Sleep -Seconds 4
throw "Chocolatey is already installed."
} else {
Write-Host "No existing Chocolatey installation found. Beginning installation."
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
if ($LastExitCode -eq 3010) {
Write-Host 'The recent changes indicate a reboot is necessary. Please reboot at your earliest convenience.' -ForegroundColor Magenta
}
Write-Host "Refreshing terminal"
# Make `refreshenv` available right away, by defining the $env:ChocolateyInstall
# variable and importing the Chocolatey profile module.
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."
Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
Update-SessionEnvironment
refreshenv
Write-Host "Testing that choco was installed..."
choco
if (Test-Path "$chocoInstallPath") {
Write-Host "Chocolatey installation succesful" -ForegroundColor DarkGreen
} else {
Write-Host ""
Write-Error "Chocolatey installation failure"
Write-Host "`n"
Start-Sleep -Seconds 4
throw "Chocolatey installation failure"
}
}
Write-Host "Configuring Chocolatey Sources"
# Auto confirm package installations (no need to pass -y)
choco feature enable -n allowGlobalConfirmation -y
# Configure Sources. Higher values means higher priority.
if($LocalRepository) {
Write-Host "Trying to use a local repository"
if([string]::IsNullOrEmpty($LocalRepositoryPath)) {
Write-Error "The local repository path is null or empty."
$LocalRepository = $False
} else {
if([string]::IsNullOrEmpty($LocalRepositoryName)) {
Write-Error "The local repository name is null or empty."
$LocalRepository = $False
} else {
try{
choco source add -n $LocalRepositoryName -s $LocalRepositoryPath --priority=10
Write-Host "Using Chocolatey local repository $LocalRepositoryName as main source."
if($DisableCommunityRepository) {
choco source disable -n=chocolatey
Write-Host "Disabled Chocolatey Community Repository"
}
} catch {
Write-Host "*****************************************************************" -ForegroundColor DarkRed
Write-Host "Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" -ForegroundColor DarkRed
Write-Host "Failed to add local repository and/or disable community repository." -ForegroundColor DarkRed
Write-Host "*****************************************************************" -ForegroundColor DarkRed
$LocalRepository = $False
}
}
}
}
if($LocalRepository -eq $False) {
Write-Host "Using Chocolatey Community Repository as main source."
Write-Host "Note that if you have multiple machines/VMs running on your local network, `n you will run into the Chocolatey Community Repository traffic limit." -ForegroundColor DarkYellow
}
Write-Host "Printing Chocolatey sources list:"
Write-Host "[START LIST]"
choco source list
Write-Host "[END LIST]"
# Write-Host "Enable remembered arguments" -ForegroundColor DarkBlue
# # https://github.com/chocolatey/choco/issues/797
# # We capture the arguments passed during install/upgrade. Now we need to pass them to the upgrades automatically.
# choco feature enable -n useRememberedArgumentsForUpgrades
# Before enabling this, make sure the following are fixed:
# - https://github.com/chocolatey/choco/issues/2886
# - https://github.com/chocolatey/choco/issues/2761
# - https://github.com/chocolatey/choco/pull/3003
Write-Host "Configuring Chocolatey Updates" -ForegroundColor DarkBlue
<#
Creates a Windows Scheduled Task to run "choco upgrade all -y" with enhanced options at a time and frequency you specify
And because sometimes package installations go wrong, it will also create a Windows Scheduled Task to
run "taskkill /im choco.exe /f /t" to stop the Chocolatey (choco.exe) process and all child processes at a time you specify.
Runs "choco upgrade all -y" daily at 3 AM and aborts it at 6 AM.
#>
choco install choco-upgrade-all-at -y --params "'/DAILY:yes /TIME:03:00 /ABORTTIME:06:00'"
Write-Host "Configuring Chocolatey Package Cleaning" -ForegroundColor DarkBlue
# Set it and forget it! Choco-Cleaner cleans up your Chocolatey installation
# every Sunday at 11 PM in the background so you don't have to be bothered with it.
choco install choco-cleaner -y
Write-Host "Installing and Configuring Chocolatey GUI" -ForegroundColor DarkBlue
# Chocolatey GUI
choco install chocolateygui -y --params "'/Global /ShowConsoleOutput=$true /PreventAutomatedOutdatedPackagesCheck=$true /DefaultToTileViewForLocalSource=$false /DefaultToTileViewForRemoteSource=$false /DefaultToDarkMode=$true'"
Write-Host "Chocolatey configuration completed." -ForegroundColor DarkGreen