-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathConfigure-WSUSServer.ps1
More file actions
268 lines (207 loc) · 7.75 KB
/
Configure-WSUSServer.ps1
File metadata and controls
268 lines (207 loc) · 7.75 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
<#
.DESCRIPTION
Installs and configures the WSUS role
.EXAMPLE
.\Configure-WSUSServer.ps1 -WSUSConfigJson ".\WSUS-Config.json"
.PARAMETER WSUSConfigJson
Pass location of configuration file
#>
Param (
[Parameter(Mandatory = $True, HelpMessage = "Specify complete path of the WSUS-Config.json")]
[string]
$WSUSConfigJson
)
$ErrorActionPreference = 'Stop'
$Json = Get-Content $WSUSConfigJson | Out-String | ConvertFrom-Json
Write-Output $Json
function HasScriptRun {
try {
$scriptrun = Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows" -Name WSUSConfigScriptRun -erroraction stop
$hasscriptrun = $scriptrun -ne 0
}
catch {
$hasscriptrun = $false
}
$hasscriptrun
}
function ShouldScriptRun {
$scriptshouldrun = $True
if ([string]::IsNullOrWhitespace($Json.Force) -or ($Json.Force -ine $true)) {
$scripthasrun = HasScriptRun
$scriptshouldrun = ($scripthasrun -ne $true)
}
$scriptshouldrun
}
function SetScriptRun {
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows" -Name WSUSConfigScriptRun -Value 1
}
function RunPostInstall {
$contentdir = $Json.ContentDir
if ([string]::IsNullOrWhitespace($contentdir)) {
throw "Must specify WSUS content directory."
}
& $env:SystemDrive'\Program Files\Update Services\Tools\WsusUtil.exe' postinstall CONTENT\_DIR=$contentdir
}
function InstallWSUS {
# Install WSUS Role i.e. WSUS Services and Management tools
Write-Host 'Installing WSUS for WID (Windows Internal Database)'
Install-WindowsFeature -Name UpdateServices -IncludeManagementTools
RunPostInstall
}
function ConfigWSUSAndPerformCategorySync {
Set-WsusServerSynchronization –SyncFromMU
$wsus = Get-WSUSServer
$wsusConfig = $wsus.GetConfiguration()
$languages = "all"
if (-not [string]::IsNullOrWhitespace($Json.Languages)) {
$languages = $Json.Languages
}
if ($languages -ieq "all") {
Write-Host "Enabling all WSUS languages"
$wsusConfig.AllUpdateLanguagesEnabled = $true
}
else {
Write-Host "Setting WSUS languages to $languages"
$wsusConfig.AllUpdateLanguagesEnabled = $false
$wsusConfig.SetEnabledUpdateLanguages($languages)
}
if (-not [string]::IsNullOrWhitespace($Json.StoreUpdatesLocally)) {
$storelocally = $Json.StoreUpdatesLocally -ieq $true
Write-Host "Setting WSUS local update storage to $storelocally"
$wsusConfig.HostBinariesOnMicrosoftUpdate = $storelocally
if ($storelocally) {
$useexpress = false
if (-not [string]::IsNullOrWhitespace($Json.UseExpressInstallationOption)) {
$useexpress = $Json.UseExpressInstallationOption -ieq $true
}
Write-Host "Setting WSUS express installation to $useexpress"
$wsusConfig.DownloadExpressPackages = $useexpress
}
}
$wsusConfig.Save()
$subscription = $wsus.GetSubscription()
Write-Host 'Beginning WSUS category sync'
$subscription.StartSynchronizationForCategoryOnly()
while ($subscription.GetSynchronizationStatus() -ne 'NotProcessing') {
Write-Host "." -NoNewline
Start-Sleep -Seconds 10
}
Write-Host "Initial sync is done."
}
function ConfigProductsForSync {
if ($Json.Products.Length -eq 0) {
Write-Host "No products specified"
}
else {
Write-Host 'Setting WSUS Products'
# De-select previous products
Get-WsusServer | Get-WsusProduct | Set-WSUSProduct -Disable
foreach ($Product in $Json.Products) {
$productFound = Get-WsusProduct | Where-Object -FilterScript { $_.product.title -like $Product.Value }
if (-not [string]::IsNullOrWhitespace($productFound)) {
Get-WsusServer | Get-WsusProduct | Where-Object -FilterScript { $_.product.title -like $Product.Value } | Set-WsusProduct
}
else {
throw "Product ($Product.Value) that requested to sync not found in the product categories"
}
}
}
}
function ConfigClassificationsForSync {
if ($Json.Classifications.Length -eq 0) {
Write-Host "No classifications specified"
}
else {
Write-Host 'Setting WSUS Classifications'
# De-select current classifications
Get-WsusServer | Get-WsusClassification | Set-WsusClassification -Disable
foreach ($Classification in $Json.Classifications) {
$classificationFound = Get-WsusClassification | Where-Object { $_.Classification.Title -Like $Classification.Value }
if (-not [string]::IsNullOrWhitespace($classificationFound)) {
Get-WsusServer | Get-WsusClassification | Where-Object { $_.Classification.Title -Like $Classification.Value } | Set-WsusClassification
}
else {
throw "Classification ($Classification.Value) that requested to sync not found in the classification list"
}
}
}
}
function EnableAutoApproval {
$wsus = Get-WsusServer
$approvalrule = $wsus.GetInstallApprovalRules() | Where-Object { $_.Name -eq "Approve All Updates" }
if ($approvalrule) {
Write-Host "Using existing auto-approval rule"
}
else {
$approvalrule = $wsus.CreateInstallApprovalRule("Approve All Updates")
Write-Host "Creating new auto-approval rule"
}
$ApprovalRule.Enabled = $true
$productcollection = New-Object -TypeName Microsoft.UpdateServices.Administration.UpdateCategoryCollection
$computergroupcollection = New-Object -TypeName Microsoft.UpdateServices.Administration.ComputerTargetGroupCollection
$allcomputers = $wsus.GetComputerTargetGroups() | Where-Object { $_.Name -eq "All Computers" }
if ($allcomputers) {
$computergroupcollection.Add($allcomputers)
}
else {
throw "Failed to find 'All Computers' target group"
}
$approvalrule.SetComputerTargetGroups($computergroupcollection)
$approvalrule.Save()
}
function DisableAutoApproval {
$wsus = Get-WsusServer
$approvalrule = $wsus.GetInstallApprovalRules() | Where-Object { $_.Name -eq "Approve All Updates" }
if ($approvalrule) {
Write-Host "Deleting existing auto-approval rule"
$wsus.DeleteInstallApprovalRule($approvalrule.Id)
}
else {
Write-Host "Approval rule already deleted"
}
}
function SetAutoApproval {
if ($Json.AutoApproveUpdates -ieq $true) {
EnableAutoApproval
}
elseif ($Json.AutoApproveUpdates -ieq $false) {
DisableAutoApproval
}
else {
Write-Host"Auto-approval unchanged"
}
}
function SetSynchronizationTypeAndStartFullSync {
$wsus = Get-WSUSServer
$subscription = $wsus.GetSubscription()
$autosynccount = 0
if (-not [string]::IsNullOrWhitespace($Json.AutoSynchronize)) {
$autosynccount = 0 + $Json.AutoSynchronize
}
if ($autosynccount -eq 0) {
Write-Host "Disabling automatic synchronization."
$subscription.SynchronizeAutomatically = $false
}
else {
Write-Host "Setting automatic synchronization to $autosynccount times per day."
$subscription.SynchronizeAutomatically = $true
$subscription.NumberOfSynchronizationsPerDay = $autosynccount
}
$subscription.Save()
RunPostInstall
$subscription.StartSynchronization()
Write-Host 'WSUS Sync started'
}
if (ShouldScriptRun) {
Write-Host "Configuring WSUS"
SetScriptRun
InstallWSUS
ConfigWSUSAndPerformCategorySync
ConfigProductsForSync
ConfigClassificationsForSync
SetAutoApproval
SetSynchronizationTypeAndStartFullSync
}
else {
Write-Host "WSUS configuration script has already run. Use parameter 'Force=true' in the configuration file to force script to run again."
}