-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateWindows.ps1
84 lines (68 loc) · 2.02 KB
/
UpdateWindows.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
<#
.SYNOPSIS
Update Windows
.DESCRIPTION
To update computers in a cluster, put the script in a shared dir, say \\computer1\share1\Update-Windows.ps1. Then execute the following command line in a cmd shell on each computer:
powershell "& { Set-ExecutionPolicy -ExecutionPolicy bypass; \\computer1\share1\Update-Windows.ps1 -a -r }
.PARAMETER AcceptAll
.PARAMETER AutoReboot
#>
param(
[Alias("a")]
[switch]$AcceptAll,
[Alias("r")]
[switch]$AutoReboot
)
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (!(Get-Module -Name PSWindowsUpdate -ListAvailable))
{
"Preparing to install PSWindowsUpdate..." | out-string
try
{
Get-PackageProvider -Name NuGet -ListAvailable
}
catch
{
"Installing package provider NuGet..." | out-string
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
}
"Setting InstallationPolicy for PSGallery..." | out-string
$retry = 0
while ($retry -lt 5)
{
if ($retry -gt 0)
{
"Retrying..." | out-string
Sleep 5
}
# Sometimes (probablity <= 1%) 'Set-PSRepository -Name "PSGallery" ...' failed with error:
# Set-PSRepository : No repository with the name 'PSGallery' was found.
# Hopefully retry can help that.
try
{
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
break
}
catch
{
"Set-PSRepository failed with error:\n$_" | out-string
$retry += 1
}
}
if ($retry -eq 5)
{
throw "Abort!" | out-string
}
"Installing PSWindowsUpdate..." | out-string
Install-Module -Name PSWindowsUpdate
}
else
{
Import-Module PSWindowsUpdate
}
"Checking for updates..." | out-string
Get-WindowsUpdate | out-string
"Downloading and installing updates..." | out-string
Install-WindowsUpdate -AcceptAll:$AcceptAll -AutoReboot:$AutoReboot | out-string
"Done!" | out-string