-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.ps1
406 lines (337 loc) · 11.2 KB
/
update.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#region Init & Settings
Set-Location ~
$color1 = "DarkMagenta"
$color2 = "DarkRed"
$color3 = "Red"
$color4 = "Cyan"
$verbose = @{
all = $false; # Set to `$true` to turn on verbosity for all sections
# Sections that use verbosity
WSL = $true; #TODO: Not yet implemented
Chocolatey = $false; # Chocolatey verbosity isn't very useful
Winget = $true; #TODO: Not yet implemented
PowerShellGet = $true;
MSStore = $true;
ncu = $false; # ncu verbosity isn't very useful
WindowsUpdate = $true;
npmcache = $true;
yarncache = $false
}
$run = @{
WSL = $true; # Run Windows Subsystem for Linux (WSL) update
Chocolatey = $true;
Winget = $false;
PowerShellGet = $false;
MSStore = $true;
ncu = $true;
WindowsUpdate = $true;
ChocoCleaner = $true;
npmcache = $false;
yarncache = $false;
dotnetcache = $false;
}
#endregion
#region Functions
function Watch-Keypress ($sleepSeconds = 10) {
$timeout = New-TimeSpan -Seconds $sleepSeconds
$stopWatch = [Diagnostics.Stopwatch]::StartNew()
$interrupted = $false
while ($stopWatch.Elapsed -lt $timeout) {
if ($Host.UI.RawUI.KeyAvailable) {
$keyPressed = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown, IncludeKeyUp")
if ($keyPressed) {
$interrupted = $true
break
}
}
}
return $interrupted
}
function Test-CommandExists {
<#
.NOTES
Adapted from https://devblogs.microsoft.com/scripting/use-a-powershell-function-to-see-if-a-command-exists/
#>
param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = "stop"
try {
if (Get-Command $command) {
return $true
}
Write-Host "Command '$command' does not exist."
}
catch {
Write-Host "Checking for command '$command' failed."
return $false
}
finally {
$ErrorActionPreference = $oldPreference
}
}
#endregion
#region Opening
Write-Host "[[[UPDATE SCRIPT]]]" -ForegroundColor $color1
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
#endregion
#region Windows Subsystem for Linux (WSL)
<#
# TODO: Add list of default/recommend apt packages to install on first run
#>
if ($run.WSL -and (Test-CommandExists wsl)) {
Write-Host "Press any key to update WSL. (WSL update will be skipped in 10 seconds.)"
if (Watch-Keypress) {
Write-Host ""
Write-Host "Running WSL update."
Write-Host ""
$wslName = "WSL Ubuntu"
$wslUser = wsl whoami
Write-Host "[Update, upgrade, and autoremove in $wslName]" -ForegroundColor $color2
$sudopw = Read-Host -assecurestring "[sudo] password for $wslUser (blank to skip)"
Write-Host ""
if ($sudopw.Length -ne 0) {
Write-Host "Updating, upgrading, and autoremoving in $wslName..." -ForegroundColor $color3
Write-Host ""
$sudopw = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sudopw))
wsl export HISTIGNORE='*sudo -S*'
Write-Host "Updating in $wslName..." -ForegroundColor $color3
wsl echo "$sudopw" | wsl sudo -S -k apt update
Write-Host "Done updating in $wslName." -ForegroundColor $color3
Write-Host ""
Write-Host "Upgrading in $wslName..." -ForegroundColor $color3
wsl echo "$sudopw" | wsl sudo -S -k apt upgrade
Write-Host "Done upgrading in $wslName." -ForegroundColor $color3
Write-Host ""
Write-Host "Autoremoving in $wslName..." -ForegroundColor $color3
wsl echo "$sudopw" | wsl sudo -S -k apt autoremove
Write-Host "Done autoremoving in $wslName." -ForegroundColor $color3
Write-Host ""
Write-Host "Done with $wslName." -ForegroundColor $color3
}
else {
Write-Host "(skipped)" -ForegroundColor $color3
}
}
else {
Write-Host "Skipping WSL update."
}
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Chocolatey packages
<#
# TODO: Add list of default/recommended choco packages to install on first run
#>
if ($run.Chocolatey -and (Test-CommandExists choco)) {
Write-Host "[Upgrade Chocolatey Packages]" -ForegroundColor $color2
Write-Host ""
Write-Host "Upgrading all Chocolatey packages..." -ForegroundColor $color3
if ($verbose.all -or $verbose.Chocolatey) {
Write-Host "choco upgrade all --yes --exit-when-reboot-detected --verbose"
choco upgrade all --yes --exit-when-reboot-detected --verbose
}
else {
Write-Host "choco upgrade all --yes --exit-when-reboot-detected"
choco upgrade all --exit-when-reboot-detected --yes
}
Write-Host ""
Write-Host "Done upgrading all Chocolatey packages." -ForegroundColor $color3
Write-Host ""
Write-Host "If you encountered any 'already referencing a newer version' errors, try running this:"
Write-Host "choco upgrade all --yes --ignore-dependencies" -ForegroundColor $color4
Write-Host ""
Write-Host "Or, try to solve the issue by looking in C:\ProgramData\chocolatey\lib\ to verify the referenced package has only one nupkg folder (without a version number in the name of the nupkg). If you find any others, delete them."
Write-Host "Reference: https://github.com/chocolatey/choco/issues/227#issuecomment-1107213230"
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Winget packages
if ($run.Winget -and (Test-CommandExists winget)) {
Write-Host "[Upgrade Winget Packages]" -ForegroundColor $color2
Write-Host ""
Write-Host "Upgrading all Winget packages..." -ForegroundColor $color3
winget upgrade --all --accept-package-agreements --accept-source-agreements
Write-Host ""
Write-Host "Done upgrading all Winget packages." -ForegroundColor $color3
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region PowerShellGet modules
if ($run.PowerShellGet -and (Test-CommandExists Update-Module)) {
Write-Host "[Update PowerShellGet modules]" -ForegroundColor $color2
Write-Host ""
Write-Host "Updating PowerShellGet modules (this can be very slow)..." -ForegroundColor $color3
if ($verbose.all -or $verbose.PowerShellGet) {
Update-Module -Verbose
}
else {
Update-Module
}
Write-Host ""
# Update-Help
# Write-Host ""
Write-Host "Done updating PowerShellGet modules." -ForegroundColor $color3
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Microsoft Store apps
if ($run.MSStore -and (Test-CommandExists Get-CimInstance -and Test-CommandExists Invoke-CimMethod -and Test-CommandExists Start-Process)) {
Write-Host "[Update all Microsoft Store apps]" -ForegroundColor $color2
Write-Host ""
if (Test-CommandExists Get-CimInstance -and Test-CommandExists Invoke-CimMethod) {
Write-Host "Updating all Microsoft Store apps..." -ForegroundColor $color3
Write-Host ""
$namespaceName = "Root\cimv2\mdm\dmmap"
$className = "MDM_EnterpriseModernAppManagement_AppManagement01"
$methodName = "UpdateScanMethod"
if ($verbose.all -or $verbose.MSStore) {
Get-CimInstance -Namespace $namespaceName -ClassName $className -Verbose | Invoke-CimMethod -MethodName $methodName -Verbose
}
else {
Get-CimInstance -Namespace $namespaceName -ClassName $className | Invoke-CimMethod -MethodName $methodName
}
Write-Host ""
}
if (Test-CommandExists Start-Process) {
Write-Host "Opening Downloads and Updates in Microsoft Store..." -ForegroundColor $color3
# shell:appsFolder\Microsoft.WindowsStore_8wekyb3d8bbwe!App
Start-Process ms-windows-store://downloadsandupdates
Write-Host ""
}
Write-Host "Done updating all Microsoft Store apps." -ForegroundColor $color3
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Node Package Manager (npm) packages
if ($run.ncu -and (Test-CommandExists node -and Test-CommandExists npm)) {
<#
# TODO: Add list of default/recommend npm packages to install on first run
#>
Write-Host "[npm patch-level updates]" -ForegroundColor $color2
Write-Host ""
if (Test-CommandExists ncu) {
Write-Host "npm-check-updates is already installed." -ForegroundColor $color3
Write-Host ""
}
else {
# Install npm-check-updates
Write-Host "Installing npm-check-updates..." -ForegroundColor $color3
Write-Host "npm install npm-check-updates --global"
npm install npm-check-updates --global
Write-Host ""
}
Write-Host "Checking npm global for patch-level updates..." -ForegroundColor $color3
if ($verbose.all -or $verbose.ncu) {
Write-Host "ncu --global --target patch --loglevel verbose"
ncu --global --target patch --loglevel verbose
}
else {
Write-Host "ncu --global --target patch"
ncu --global --target patch
}
Write-Host ""
Write-Host "Done checking npm global for patch-level updates." -ForegroundColor $color3
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Windows Update and Microsoft Update
if ($run.WindowsUpdate -and (Test-CommandExists Get-WindowsUpdate -and Test-CommandExists Start-Process)) {
Write-Host "[Windows Update and Microsoft Update]" -ForegroundColor $color2
Write-Host ""
Write-Host "Running Windows Update and Microsoft Update..." -ForegroundColor $color3
Write-Host ""
<#
# Requires PowerShell >=5
$PSVersionTable.PSVersion
# Only needed on first run
# Install-Module PSWindowsUpdate
# Write-Host ""
Get-Command -module PSWindowsUpdate
Write-Host ""
# Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d
# Write-Host ""
#>
if ($verbose.all -or $verbose.WindowsUpdate) {
Get-WindowsUpdate -MicrosoftUpdate -Install -AcceptAll -Verbose
}
else {
Get-WindowsUpdate -MicrosoftUpdate -Install -AcceptAll
}
Write-Host ""
Write-Host "Opening Windows Update in Settings..." -ForegroundColor $color3
Start-Process ms-settings:windowsupdate
Write-Host ""
Write-Host "Done running Windows Update and Microsoft Update." -ForegroundColor $color3
Write-Host ""
Write-Host "..." -ForegroundColor $color3
Write-Host ""
}
#endregion
#region Finish & Clean-Up
Write-Host "[Finish & Clean-Up]" -ForegroundColor $color2
Write-Host ""
# Choco Cleaner
if ($run.ChocoCleaner -and (Test-CommandExists choco-cleaner)) {
Write-Host "Cleaning up chocolatey..." -ForegroundColor $color3
choco-cleaner
Write-Host ""
}
# Verify NPM cache (does garbage collection)
if ($run.npmcache -and (Test-CommandExists npm)) {
Write-Host "Cleaning up npm..." -ForegroundColor $color3
if ($verbose.all -or $verbose.npmcache) {
Write-Host "npm cache verify --verbose"
npm cache verify --verbose
}
else {
Write-Host "npm cache verify"
npm cache verify
}
Write-Host ""
}
# Clean yarn cache
if ($run.yarncache -and (Test-CommandExists yarn)) {
Write-Host "Cleaning up yarn..." -ForegroundColor $color3
if ($verbose.all -or $verbose.yarncache) {
Write-Host "yarn cache clean --verbose"
yarn cache clean --verbose
}
else {
Write-Host "yarn cache clean"
yarn cache clean
}
yarn cache clean
Write-Host ""
}
# Clear all local nuget caches
if ($run.dotnetcache -and (Test-CommandExists dotnet)) {
Write-Host "Cleaning up nuget..." -ForegroundColor $color3
dotnet nuget locals all --clear
Write-Host ""
}
if (Test-CommandExists refreshenv) {
Write-Host "Refreshing environment variables..." -ForegroundColor $color3
Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1
refreshenv # alias for Update-SessionEnvironment
Write-Host ""
}
Write-Host "..." -ForegroundColor $color3
Write-Host ""
#endregion
#region Done
Write-Host "Done!" -ForegroundColor $color1
#endregion