-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathInstall-RSATv1809v1903v1909v2004v20H2.ps1
326 lines (299 loc) · 15.1 KB
/
Install-RSATv1809v1903v1909v2004v20H2.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
<#
.SYNOPSIS
Install RSAT features for Windows 10 1809 or 1903 or 1909 or 2004 or 20H2.
.DESCRIPTION
Install RSAT features for Windows 10 1809 or 1903 or 1909 or 2004 or 20H2. All features are installed online from Microsoft Update thus the script requires Internet access
.PARAMETER All
Installs all the features within RSAT. This takes several minutes, depending on your Internet connection
.PARAMETER Basic
Installs ADDS, DHCP, DNS, GPO, ServerManager
.PARAMETER ServerManager
Installs ServerManager
.PARAMETER Uninstall
Uninstalls all the RSAT features
.PARAMETER disableWSUS
Disables the use of WSUS prior to installing the RSAT features. This involves restarting the wuauserv service. The script will enable WSUS again post installing the features on demand
.NOTES
Filename: Install-RSATv1809v1903v1909v2004v20H2.ps1
Version: 1.6
Author: Martin Bengtsson
Blog: www.imab.dk
Twitter: @mwbengtsson
Version history:
1.0 - Script created
1.2 - Added test for pending reboots. If reboot is pending, RSAT features might not install successfully
Added test for configuration of local WSUS by Group Policy.
- If local WSUS is configured by Group Policy, history shows that additional settings might be needed for some environments
- This policy in question is located in administrative templates -> System
1.3 - Now using Get-CmiInstance instead of Get-WmiObject for determining OS buildnumber
1.4 - Script updated to support installing RSAT on Windows 10 v2004
1.5 - Script updated to support installing RSAT on Windows 10 v20H2
1.6 - Added option to disable WSUS prior to installing RSAT as features on demand
- Some environments seems to require this
- Will enable WSUS again post installation
#>
[CmdletBinding()]
param(
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[switch]$All,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[switch]$Basic,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[switch]$ServerManager,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[switch]$Uninstall,
[Parameter(Mandatory=$false)]
[switch]$DisableWSUS
)
# Check for administrative rights
if (-NOT([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning -Message "The script requires elevation"
break
}
# Create Write-Log function
function Write-Log() {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$path = "$env:windir\Install-RSAT.log",
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$level = "Info"
)
Begin {
# Set VerbosePreference to Continue so that verbose messages are displayed.
$verbosePreference = 'Continue'
}
Process {
if ((Test-Path $Path)) {
$logSize = (Get-Item -Path $Path).Length/1MB
$maxLogSize = 5
}
# Check for file size of the log. If greater than 5MB, it will create a new one and delete the old.
if ((Test-Path $Path) -AND $LogSize -gt $MaxLogSize) {
Write-Error "Log file $Path already exists and file exceeds maximum file size. Deleting the log and starting fresh."
Remove-Item $Path -Force
$newLogFile = New-Item $Path -Force -ItemType File
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
elseif (-NOT(Test-Path $Path)) {
Write-Verbose "Creating $Path."
$newLogFile = New-Item $Path -Force -ItemType File
}
else {
# Nothing to see here yet.
}
# Format Date for our Log File
$formattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Write message to error, warning, or verbose pipeline and specify $LevelText
switch ($level) {
'Error' {
Write-Error $message
$levelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$levelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$levelText = 'INFO:'
}
}
# Write log entry to $Path
"$formattedDate $levelText $message" | Out-File -FilePath $path -Append
}
End {
}
}
# Create Pending Reboot function for registry
function Test-PendingRebootRegistry {
$cbsRebootKey = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction Ignore
$wuRebootKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction Ignore
if (($cbsRebootKey -ne $null) -OR ($wuRebootKey -ne $null)) {
$true
}
else {
$false
}
}
# Minimum required Windows 10 build (v1809)
$1809Build = "17763"
# Get running Windows build
$windowsBuild = (Get-CimInstance -Class Win32_OperatingSystem).BuildNumber
# Get information about local WSUS server
$wuServer = (Get-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" -Name WUServer -ErrorAction Ignore).WUServer
$useWUServer = (Get-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" -ErrorAction Ignore).UseWuServer
# Look for pending reboots in the registry
$testPendingRebootRegistry = Test-PendingRebootRegistry
if ($windowsBuild -ge $1809Build) {
Write-Log -Message "Running correct Windows 10 build number for installing RSAT with Features on Demand. Build number is: $WindowsBuild"
Write-Log -Message "***********************************************************"
if ($wuServer -ne $null) {
Write-Log -Message "A local WSUS server was found configured by group policy: $wuServer"
Write-Log -Message "You might need to configure additional setting by GPO if things are not working"
Write-Log -Message "The GPO of interest is following: Specify settings for optional component installation and component repair"
Write-Log -Message "Check ON: Download repair content and optional features directly from Windows Update..."
Write-Log -Message "***********************************************************"
Write-Log -Message "Alternatively, run this script with parameter -disableWSUS to allow the script to temporarily disable WSUS"
}
if ($PSBoundParameters["DisableWSUS"]) {
if (-NOT[string]::IsNullOrEmpty($useWUServer)) {
if ($useWUServer -eq 1) {
Write-Log -Message "***********************************************************"
Write-Log -Message "DisableWSUS selected. Temporarily disabling WSUS in order to successfully install features on demand"
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWuServer" -Value 0
Restart-Service wuauserv
}
}
}
if ($testPendingRebootRegistry -eq $true) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Reboots are pending. The script will continue, but RSAT might not install successfully"
}
if ($PSBoundParameters["All"]) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Script is running with -All parameter. Installing all available RSAT features"
$install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "NotPresent"}
if ($install -ne $null) {
foreach ($item in $install) {
$rsatItem = $item.Name
Write-Log -Message "Adding $RsatItem to Windows"
try {
Add-WindowsCapability -Online -Name $rsatItem
}
catch [System.Exception] {
Write-Log -Message "Failed to add $rsatItem to Windows" -Level Warn
Write-Log -Message "$($_.Exception.Message)" -Level Warn
}
}
if ($PSBoundParameters["DisableWSUS"]) {
if (-NOT[string]::IsNullOrEmpty($useWUServer)) {
if ($useWUServer -eq 1) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Enabling WSUS again post installing features on demand"
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWuServer" -Value 1
Restart-Service wuauserv
}
}
}
}
else {
Write-Log -Message "All RSAT features seems to be installed already"
}
}
if ($PSBoundParameters["Basic"]) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Script is running with -Basic parameter. Installing basic RSAT features"
# Querying for what I see as the basic features of RSAT. Modify this if you think something is missing. :-)
$install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ActiveDirectory*" -OR $_.Name -like "Rsat.DHCP.Tools*" -OR $_.Name -like "Rsat.Dns.Tools*" -OR $_.Name -like "Rsat.GroupPolicy*" -OR $_.Name -like "Rsat.ServerManager*" -AND $_.State -eq "NotPresent" }
if ($install -ne $null) {
foreach ($item in $install) {
$rsatItem = $item.Name
Write-Log -Message "Adding $rsatItem to Windows"
try {
Add-WindowsCapability -Online -Name $rsatItem
}
catch [System.Exception] {
Write-Log -Message "Failed to add $rsatItem to Windows" -Level Warn
Write-Log -Message "$($_.Exception.Message)" -Level Warn
}
}
if ($PSBoundParameters["DisableWSUS"]) {
if (-NOT[string]::IsNullOrEmpty($useWUServer)) {
if ($useWUServer -eq 1) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Enabling WSUS again post installing features on demand"
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWuServer" -Value 1
Restart-Service wuauserv
}
}
}
}
else {
Write-Log -Message "The basic features of RSAT seems to be installed already"
}
}
if ($PSBoundParameters["ServerManager"]) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Script is running with -ServerManager parameter. Installing Server Manager RSAT feature"
$install = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat.ServerManager*" -AND $_.State -eq "NotPresent"}
if ($install -ne $null) {
$rsatItem = $Install.Name
Write-Log -Message "Adding $rsatItem to Windows"
try {
Add-WindowsCapability -Online -Name $rsatItem
}
catch [System.Exception] {
Write-Log -Message "Failed to add $rsatItem to Windows" -Level Warn
Write-Log -Message "$($_.Exception.Message)" -Level Warn
}
if ($PSBoundParameters["DisableWSUS"]) {
if (-NOT[string]::IsNullOrEmpty($useWUServer)) {
if ($useWUServer -eq 1) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Enabling WSUS again post installing features on demand"
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "UseWuServer" -Value 1
Restart-Service wuauserv
}
}
}
}
else {
Write-Log -Message "$rsatItem seems to be installed already"
}
}
if ($PSBoundParameters["Uninstall"]) {
Write-Log -Message "***********************************************************"
Write-Log -Message "Script is running with -Uninstall parameter. Uninstalling all RSAT features"
# Querying for installed RSAT features first time
$installed = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "Installed" -AND $_.Name -notlike "Rsat.ServerManager*" -AND $_.Name -notlike "Rsat.GroupPolicy*" -AND $_.Name -notlike "Rsat.ActiveDirectory*"}
if ($installed -ne $null) {
Write-Log -Message "Uninstalling the first round of RSAT features"
# Uninstalling first round of RSAT features - some features seems to be locked until others are uninstalled first
foreach ($item in $installed) {
$rsatItem = $item.Name
Write-Log -Message "Uninstalling $rsatItem from Windows"
try {
Remove-WindowsCapability -Name $rsatItem -Online
}
catch [System.Exception] {
Write-Log -Message "Failed to uninstall $rsatItem from Windows" -Level Warn
Write-Log -Message "$($_.Exception.Message)" -Level Warn
}
}
}
# Querying for installed RSAT features second time
$installed = Get-WindowsCapability -Online | Where-Object {$_.Name -like "Rsat*" -AND $_.State -eq "Installed"}
if ($installed -ne $null) {
Write-Log -Message "Uninstalling the second round of RSAT features"
# Uninstalling second round of RSAT features
foreach ($item in $installed) {
$rsatItem = $item.Name
Write-Log -Message "Uninstalling $rsatItem from Windows"
try {
Remove-WindowsCapability -Name $rsatItem -Online
}
catch [System.Exception] {
Write-Log -Message "Failed to remove $rsatItem from Windows" -Level Warn
Write-Log -Message "$($_.Exception.Message)" -Level Warn
}
}
}
else {
Write-Log -Message "All RSAT features seems to be uninstalled already"
}
}
}
else {
Write-Log -Message "Not running correct Windows 10 build: $windowsBuild" -Level Warn
}