-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathUpdate-Everything.ps1
More file actions
444 lines (385 loc) · 13 KB
/
Update-Everything.ps1
File metadata and controls
444 lines (385 loc) · 13 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<#
.SYNOPSIS
Updates everything it can on the system.
.FUNCTIONALITY
System and updates
.LINK
https://www.ehmiiz.se/blog/ps_resourceget/
.LINK
https://docs.microsoft.com/windows/package-manager/winget/
.LINK
https://github.com/microsoft/winget-cli
.LINK
https://www.microsoft.com/store/apps/windows
.LINK
https://powershellgallery.com/
.LINK
https://chocolatey.org/
.LINK
https://scoop.sh/
.LINK
https://npmjs.com/
.LINK
https://docs.microsoft.com/dotnet/core/tools/global-tools
.LINK
https://cli.github.com/
.LINK
https://code.visualstudio.com/
.LINK
https://www.dell.com/support/kbdoc/000177325/dell-command-update
.LINK
Update-Everything.cmd
.LINK
Uninstall-OldModules.ps1
.LINK
Get-DotNetGlobalTools.ps1
.LINK
Find-DotNetTools.ps1
.LINK
Get-Process
.LINK
Stop-Process
.LINK
Start-Process
.LINK
ConvertFrom-Csv
.LINK
Get-CimInstance
.LINK
Invoke-CimMethod
.LINK
Get-Module
.LINK
Find-Module
.LINK
Update-Module
.LINK
Update-Help
.LINK
Get-WindowsUpdate
.LINK
Install-WindowsUpdate
.EXAMPLE
Update-Everything.ps1
Attempts to update packages, features, and system.
#>
#Requires -Version 7
using module Microsoft.PowerShell.PSResourceGet
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost','',
Justification='This script is not intended for pipelining.')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','',
Justification='Some of these functions may deal with multiple updates.')]
[CmdletBinding()] Param(
# The sources of updates to install, in order.
[ValidateSet('Apt','Chocolatey','DellCommand','Dotnet','Essential','Flatpak','GitHubCli','Npm','ScriptsData',
'PSHelp','PSModule','Scoop','VSCodeExtenions','WindowsUpdate','WindowsStore','WinGet')]
[Parameter(Position=0,ValueFromRemainingArguments=$true)][string[]] $Steps =
@('Essential','WindowsStore','PSModule','Scoop','Chocolatey','WinGet','Npm','Dotnet',
'GitHubCli','VSCodeExtensions','ScriptsData','PSHelp','DellCommand','WindowsUpdate','Apt','Flatpak')
)
Begin
{
$Script:IsNotAdministrator = !(Test-Administrator.ps1)
Import-CharConstants.ps1 ':UP:' ':TOP:' -Scope Script
function Test-EmptyDesktop
{
[CmdletBinding()][OutputType([bool])] Param(
[switch] $Shared
)
if(!$IsWindows) {return}
if($Shared -and $Script:IsNotAdministrator) {return $false}
$dir = [environment]::GetFolderPath(($Shared ? 'CommonDesktopDirectory' : 'Desktop' ))
return !(Join-Path $dir *.lnk |Get-Item)
}
function Clear-Desktop
{
[CmdletBinding()] Param(
[switch] $Shared
)
if(!$IsWindows) {return}
if($Shared -and $Script:IsNotAdministrator) {return}
$dir = [environment]::GetFolderPath(($Shared ? 'CommonDesktopDirectory' : 'Desktop' ))
Remove-Item (Join-Path $dir *.lnk)
}
function Write-Step
{
[CmdletBinding()] Param(
[string]$Message
)
Write-Info.ps1 $Message -ForegroundColor White -BackgroundColor DarkGray
}
function Invoke-EssentialUpdate
{
[CmdletBinding()] Param()
Write-Step "$UP Updating PowerShell & Windows Terminal"
Get-Process powershell -ErrorAction Ignore |Where-Object Id -ne $PID |Stop-Process -Force
Get-Process pwsh -ErrorAction Ignore |Where-Object Id -ne $PID |Stop-Process -Force
Start-Process ([io.path]::ChangeExtension($PSCommandPath,'cmd')) -Verb RunAs -WindowStyle Maximized
$host.SetShouldExit(0)
exit
}
function Update-Essential
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping Essential."; return}
Write-Step "$TOP Checking for essential updates"
if(Get-Command winget -ErrorAction Ignore)
{
if(@(winget list -e --id Microsoft.WindowsTerminal -s winget --upgrade-available --disable-interactivity --nowarn |
Select-Object -Skip 2 -First 1 |
Select-String Available &&
winget list -e --id Microsoft.PowerShell -s winget --upgrade-available --disable-interactivity --nowarn |
Select-Object -Skip 2 -First 1 |
Select-String Available).Count -gt 0)
{Invoke-EssentialUpdate}
}
elseif(Get-Command choco -ErrorAction Ignore)
{
if(choco outdated -r |
ConvertFrom-Csv -Delimiter '|' -Header PackageName,LocalVersion,AvailableVersion |
Where-Object PackageName -in powershell,powershell-core,microsoft-windows-terminal)
{Invoke-EssentialUpdate}
}
else
{
Write-Verbose 'Neither Chocolatey nor WinGet found, skipping essential updates'
}
}
function Update-WindowsStore
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping WindowsStore."; return}
if(!(Get-Command Get-CimInstance -ErrorAction Ignore)) {Write-Verbose 'Get-CimInstance not found, skipping WindowsStore updates'; return}
Write-Step "$UP Updating Windows Store apps (asynchronously)"
Get-CimInstance MDM_EnterpriseModernAppManagement_AppManagement01 -Namespace root\cimv2\mdm\dmmap |
Invoke-CimMethod -MethodName UpdateScanMethod
}
function Test-NoProfileMode
{
[CmdletBinding()][OutputType([bool])] Param()
return [bool]([Environment]::GetCommandLineArgs() |Where-Object {$_ -ilike '-NoP*'})
}
function Restart-UpdateWithoutProfile
{
[CmdletBinding()] Param()
Get-Process powershell -ErrorAction Ignore |Where-Object Id -ne $PID |Stop-Process -Force
Get-Process pwsh -ErrorAction Ignore |Where-Object Id -ne $PID |Stop-Process -Force
Start-Process pwsh -ArgumentList '-NoProfile','-File',$PSCommandPath
[Environment]::Exit(0)
}
function Restart-UpdateWithProfile
{
[CmdletBinding()] Param()
Start-Process pwsh
[Environment]::Exit(0)
}
function Test-AzModulesHasUpdate
{
[CmdletBinding()][OutputType([bool])] Param()
$version = @(Get-PSResource Az -ErrorAction Ignore |Measure-Object Version -Maximum)
if($version.Count -eq 0) {return}
if($version.Maximum -ge (Find-PSResource Az -Repository PSGallery).Version) {return}
$allUsers = (Get-ModuleScope.ps1 Az).Scope -eq 'AllUsers'
if($allUsers -and $Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping AzModules."; return}
return $true
}
function Update-AzModules
{
[CmdletBinding()] Param()
if(!(Test-AzModulesHasUpdate)) {return}
if(Get-Module Az) {Restart-UpdateWithoutProfile}
Write-Step "$UP Updating PowerShell Az modules"
Update-PSResource Az -Scope AllUsers -Confirm:$false
}
function Test-DbatoolsHasUpdate
{
[CmdletBinding()][OutputType([bool])] Param()
$version = @(Get-PSResource dbatools -ErrorAction Ignore |Measure-Object Version -Maximum)
if($version.Count -eq 0) {return}
if($version.Maximum -ge (Find-PSResource dbatools -Repository PSGallery).Version) {return}
$allUsers = (Get-ModuleScope.ps1 dbatools).Scope -eq 'AllUsers'
if($allUsers -and $Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping Dbatools."; return}
return $true
}
function Update-Dbatools
{
[CmdletBinding()] Param()
if(!(Test-DbatoolsHasUpdate)) {return}
if(Get-Module dbatools) {Restart-UpdateWithoutProfile}
Write-Step "$UP Upgrading PowerShell dbatools module"
Uninstall-PSResource dbatools,dbatools.library -SkipDependencyCheck
Install-PSResource dbatools -Scope AllUsers -Repository PSGallery -TrustRepository
}
function Update-PSModule
{
[CmdletBinding()] Param()
Update-Dbatools
Update-AzModules
Write-Step "$UP Updating PowerShell modules"
$isAllUsers = @{}
Get-PSResource -ErrorAction Ignore |
Group-Object Name |
Where-Object {
$found = Find-PSResource $_.Name -ErrorAction Ignore
if(!$found) {return $false}
$isAllUsers[$_.Name] = ($_ |Get-ModuleScope.ps1) -eq 'AllUsers'
if($Script:IsNotAdministrator -and $isAllUsers[$_.Name])
{
Write-Warning "Skipping module '$($_.Name)' with scope 'AllUsers'"
return $false
}
($_.Group |Measure-Object Version -Maximum).Maximum -lt [version]$found.Version
} |
ForEach-Object {
$module = $_
try {$module |Update-PSResource -Scope:( $isAllUsers[$_.Name] ? 'AllUsers' : 'CurrentUser') -TrustRepository -ErrorAction Stop}
catch
{
if($_.Exception.Message -notlike "Module '*' was not installed by using Install-PSResource, so it cannot be updated.") {throw}
Write-Warning "Unable to automatically update module '$($module.Name)'"
}
}
if(Get-Command Uninstall-OldModules.ps1 -ErrorAction Ignore)
{
Write-Step "$UP Uninstalling old PowerShell modules"
Uninstall-OldModules.ps1 -Force
}
}
function Update-Scoop
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if(!(Get-Command scoop -ErrorAction Ignore)) {Write-Verbose 'Scoop not found, skipping'; return}
Write-Step "$UP Updating Scoop packages"
scoop update *
}
function Update-Chocolatey
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping Chocolatey."; return}
if(!(Get-Command choco -ErrorAction Ignore))
{Write-Verbose 'Chocolatey not found, skipping'; return}
Write-Step "$UP Updating Chocolatey packages"
choco upgrade all -y
}
function Update-WinGet
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if(!(Get-Command winget -ErrorAction Ignore)) {Write-Verbose 'WinGet not found, skipping'; return}
Write-Step "$UP Updating WinGet packages"
winget upgrade --all
}
function Update-Npm
{
[CmdletBinding()] Param()
if(!(Get-Command npm -ErrorAction Ignore))
{Write-Verbose 'Npm not found, skipping'; return}
Write-Step "$UP Updating npm packages"
npm update -g
}
function Update-Dotnet
{
[CmdletBinding()] Param()
if(!(Get-Command dotnet -ErrorAction Ignore))
{Write-Verbose 'Dotnet not found, skipping'; return}
Write-Step "$UP Updating dotnet global tools"
& "$PSScriptRoot\Get-DotNetGlobalTools.ps1" |
Where-Object {
$_.Version -lt (& "$PSScriptRoot\Find-DotNetTools.ps1" $_.PackageName |
Where-Object PackageName -eq $_.PackageName).Version
} |
ForEach-Object {dotnet tool update -g $_.PackageName}
}
function Update-GitHubCli
{
[CmdletBinding()] Param()
if(!(Get-Command gh -ErrorAction Ignore))
{Write-Verbose 'gh not found, skipping'; return}
Write-Step "$UP Updating GitHub CLI extensions"
gh extension upgrade --all
}
function Update-VSCodeExtensions
{
[CmdletBinding()] Param()
if(!(Get-Command code -ErrorAction Ignore))
{Write-Verbose 'VSCode not found, skipping.'; return}
Write-Step "$UP Updating VSCode extensions"
code --update-extensions
}
function Update-PSHelp
{
[CmdletBinding()] Param()
Write-Step "$UP Updating PowerShell help"
Update-Help
}
function Update-ScriptsData
{
[CmdletBinding()] Param()
Write-Step "$UP Updating scripts data"
Get-UnicodeName.ps1 -Update
Get-UnicodeByName.ps1 -Update
}
function Update-DellCommand
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping DellCommand."; return}
if(!(Resolve-Path "C:\Program Files*\Dell\CommandUpdate\dcu-cli.exe"))
{Write-Verbose 'Dell Command not found, skipping'; return}
Write-Step "$UP Updating Dell firmware & system software"
Set-Alias dcu-cli "$(Resolve-Path "C:\Program Files*\Dell\CommandUpdate\dcu-cli.exe")"
dcu-cli /scan
if($LASTEXITCODE -ne 500) {dcu-cli /applyUpdates -reboot=enable}
Write-Info.ps1 ''
}
function Update-WindowsUpdate
{
[CmdletBinding()] Param()
if(!$IsWindows) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping Windows."; return}
if(!(Get-Module PSWindowsUpdate -ListAvailable))
{Write-Verbose 'PSWindowsUpdate module not found, skipping Windows Updates'; return}
Write-Step "$UP Updating Windows"
Get-WindowsUpdate
Install-WindowsUpdate |Format-Table X,Result,KB,Size,Title
}
function Update-Apt
{
[CmdletBinding()] Param()
if(!$IsLinux) {return}
if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping apt."; return}
if(!(Get-Command apt -Type Application -ErrorAction Ignore))
{Write-Verbose 'apt not found, skipping'; return}
Write-Step "$UP Updating apt packages"
apt update
#apt dist-upgrade -y
apt autoremove -y
apt autoclean -y
}
function Update-Flatpak
{
[CmdletBinding()] Param()
if(!$IsLinux) {return}
#if($Script:IsNotAdministrator) {Write-Warning "Not running as admin; skipping flatpak."; return}
if(!(Get-Command flatpak -Type Application -ErrorAction Ignore))
{Write-Verbose 'flatpak not found, skipping'; return}
Write-Step "$UP Updating flatpak packages"
flatpak update -y --noninteractive
}
}
Process
{
$emptyShared = Test-EmptyDesktop -Shared
$empty = Test-EmptyDesktop
try {$Steps |ForEach-Object {& "Update-$_"}}
finally
{
if($emptyShared) {Clear-Desktop -Shared}
if($empty) {Clear-Desktop}
if(Test-NoProfileMode) {Restart-UpdateWithProfile}
}
}