-
Notifications
You must be signed in to change notification settings - Fork 16
/
MSFT_OfficeOnlineServerProductUpdate.psm1
689 lines (581 loc) · 23.1 KB
/
MSFT_OfficeOnlineServerProductUpdate.psm1
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
$script:resourceHelperModulePath = @(
(Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\OfficeOnlineServerDsc.Util'),
(Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common')
)
Import-Module -Name $script:resourceHelperModulePath
$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
$script:OOSDscRegKey = "HKLM:\SOFTWARE\OOSDsc"
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$SetupFile,
[Parameter(Mandatory = $true)]
[System.String[]]
$Servers,
[Parameter()]
[ValidateSet("Present", "Absent")]
[System.String]
$Ensure = "Present",
[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)
if ($Ensure -eq "Absent")
{
throw [Exception] "Office Online Server does not support uninstalling updates."
}
Write-Verbose -Message "Getting install status of OOS binaries"
Write-Verbose -Message $script:localizedData.CheckIfFileExists
if (-not(Test-Path -Path $SetupFile))
{
throw "Setup file cannot be found: {$SetupFile}"
}
if ($Servers -notcontains $env:COMPUTERNAME)
{
throw "Parameter Servers should contain the current server name {$env:COMPUTERNAME}"
}
Write-Verbose -Message "Checking file status of $SetupFile"
$checkBlockedFile = $true
if (Split-Path -Path $SetupFile -IsAbsolute)
{
$driveLetter = (Split-Path -Path $SetupFile -Qualifier).TrimEnd(":")
Write-Verbose -Message "SetupFile refers to drive $driveLetter"
$volume = Get-Volume -DriveLetter $driveLetter -ErrorAction SilentlyContinue
if ($null -ne $volume)
{
if ($volume.DriveType -ne "CD-ROM")
{
Write-Verbose -Message $script:localizedData.VolumeIsFixedDrive
}
else
{
Write-Verbose -Message $script:localizedData.VolumeIsCDDrive
$checkBlockedFile = $false
}
}
else
{
Write-Verbose -Message $script:localizedData.VolumeNotFound
}
}
if ($checkBlockedFile -eq $true)
{
Write-Verbose -Message $script:localizedData.CheckingStatus
$zone = Get-Item -Path $SetupFile -Stream "Zone.Identifier" -EA SilentlyContinue
if ($null -ne $zone)
{
throw ("Setup file is blocked! Please use 'Unblock-File -Path $SetupFile' " + `
"to unblock the file before continuing.")
}
Write-Verbose -Message $script:localizedData.FileNotBlocked
}
Write-Verbose -Message $script:localizedData.GetFileInfo
$setupFileInfo = Get-ItemProperty -Path $SetupFile
$fileVersion = $setupFileInfo.VersionInfo.FileVersion
Write-Verbose -Message "Update has version $fileVersion"
$fileVersionInfo = New-Object -TypeName System.Version -ArgumentList $fileVersion
Write-Verbose -Message $script:localizedData.GetOOSInfo
$OfficeVersionInfoFile = "C:\ProgramData\Microsoft\OfficeWebApps\Data\local\OfficeVersion.inc"
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $false)
{
$OfficeVersionInfoFile = "C:\Program Files\Microsoft Office Web Apps\AgentManager\OfficeVersion.inc"
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $false)
{
throw "Cannot find file $OfficeVersionInfoFile"
}
}
$OfficeVersionInfo = Get-Content -Path $OfficeVersionInfoFile -Raw
if ($OfficeVersionInfo -match "RMJ = ([0-9]*)\r*\nRMM = ([0-9]*)\r*\nRUP = ([0-9]*)\r*\nRPR = ([0-9]*)")
{
[System.Version]$versionInfo = $matches[1] + "." + $matches[2] + "." + $matches[3] + "." + $matches[4]
}
else
{
throw "Cannot read Version information from file $OfficeVersionInfoFile"
}
Write-Verbose -Message "The version of Office Online Server is $($versionInfo)"
if ($versionInfo -lt $fileVersionInfo)
{
# Version of OfficeOnlineServer is lower than the patch version. Patch is not installed.
return @{
SetupFile = $SetupFile
Ensure = "Absent"
}
}
else
{
# Version of OfficeOnlineServer is equal or greater than the patch version. Patch is installed.
return @{
SetupFile = $SetupFile
Ensure = "Present"
}
}
}
function Set-TargetResource
{
# Supressing the global variable use to allow passing DSC the reboot message
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "")]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$SetupFile,
[Parameter(Mandatory = $true)]
[System.String[]]
$Servers,
[Parameter()]
[ValidateSet("Present", "Absent")]
[System.String]
$Ensure = "Present",
[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)
Write-Verbose -Message "Setting install status of OOS Update binaries"
if ($Ensure -eq "Absent")
{
throw [Exception] "Office Online Server does not support uninstalling updates."
}
Write-Verbose -Message "Check if the setup file exists"
if (-not(Test-Path -Path $SetupFile))
{
throw "Setup file cannot be found: {$SetupFile}"
}
if ($Servers -notcontains $env:COMPUTERNAME)
{
throw "Parameter Servers should contain the current server name {$env:COMPUTERNAME}"
}
$OfficeVersionInfoFile = "C:\ProgramData\Microsoft\OfficeWebApps\Data\local\OfficeVersion.inc"
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $false)
{
$OfficeVersionInfoFile = "C:\Program Files\Microsoft Office Web Apps\AgentManager\OfficeVersion.inc"
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $false)
{
throw "Cannot find file $OfficeVersionInfoFile. Is Office Online Server installed?"
}
}
Write-Verbose -Message "Checking file status of $SetupFile"
$checkBlockedFile = $true
if (Split-Path -Path $SetupFile -IsAbsolute)
{
$driveLetter = (Split-Path -Path $SetupFile -Qualifier).TrimEnd(":")
Write-Verbose -Message "SetupFile refers to drive $driveLetter"
$volume = Get-Volume -DriveLetter $driveLetter -ErrorAction SilentlyContinue
if ($null -ne $volume)
{
if ($volume.DriveType -ne "CD-ROM")
{
Write-Verbose -Message $script:localizedData.VolumeIsFixedDrive
}
else
{
Write-Verbose -Message $script:localizedData.VolumeIsCDDrive
$checkBlockedFile = $false
}
}
else
{
Write-Verbose -Message $script:localizedData.VolumeNotFound
}
}
if ($checkBlockedFile -eq $true)
{
Write-Verbose -Message $script:localizedData.CheckingStatus
$zone = Get-Item -Path $SetupFile -Stream "Zone.Identifier" -EA SilentlyContinue
if ($null -ne $zone)
{
throw ("Setup file is blocked! Please use 'Unblock-File -Path $SetupFile' " + `
"to unblock the file before continuing.")
}
Write-Verbose -Message $script:localizedData.FileNotBlocked
}
Write-Verbose -Message $script:localizedData.GetFileInfo
$setupFileInfo = Get-ItemProperty -Path $SetupFile
$fileVersion = $setupFileInfo.VersionInfo.FileVersion
Write-Verbose -Message "Update has version $fileVersion"
$PatchVersion = New-Object -TypeName System.Version -ArgumentList $fileVersion
Write-Verbose -Message $script:localizedData.GetOOSInfo
$serversInfo = Get-ServerInfo -Servers $Servers
# Get current server info
$currentServer = $serversInfo | Where-Object -FilterScript { $_.Name -eq $env:COMPUTERNAME }
if ($null -ne $currentServer.MasterMachine)
{
if ($env:COMPUTERNAME -eq $currentServer.MasterMachine)
{
# Server is MasterMachine
if ($currentServer.Machines.Count -gt 1)
{
Write-Verbose -Message "There are $($currentServer.Machines.Count - 1) more servers in the farm."
Write-Verbose -Message "Waiting for all other servers to be patched first."
$count = 0
$maxcount = 30
$machinesCount = (Get-OfficeWebAppsFarm).Machines.Count
while (($count -lt $maxCount) -and ($machinesCount -gt 1))
{
Write-Verbose -Message ("$([DateTime]::Now.ToShortTimeString()) - " + `
"Waiting for other OOS servers to be patched first " + `
"(waited $count of $maxCount minutes)")
Write-Verbose -Message "$($machinesCount -1) other servers still need to be patched"
Start-Sleep -Seconds 60
$machinesCount = (Get-OfficeWebAppsFarm).Machines.Count
$count++
}
if ($machinesCount -gt 1)
{
throw ("[ERROR] Cannot complete patching. Other servers need to be patched first. " + `
"Waited $maxcount minutes for other servers complete patching.")
}
}
Write-Verbose -Message "Current server is the last server in the OOS farm"
Write-Verbose -Message "Remove server from OOS farm and install OOS patch"
# Determine new MasterMachine (one of the already patched servers)
$patchedMachines = $serversInfo | Where-Object -FilterScript { $_.Version -eq $PatchVersion }
if ($null -eq $patchedMachines)
{
# No other machines are patched yet, probably single server. Set current machine to newMaster
$newMaster = $env:COMPUTERNAME
}
else
{
# Determine newMaster from other machines
$newMaster = $patchedMachines.MasterMachine | Sort-Object | Select-Object -Unique
if ($newMaster -is [System.Array])
{
Write-Verbose -Message "WARNING: Multiple masters found"
$newMaster = $newMaster | Select-Object -First
}
elseif ($null -eq $newMaster)
{
throw "Unable to determine new master. Cannot continue patching server."
}
}
Write-Verbose -Message "Determined the following server to be the new MasterMachine {$newMaster}"
if ($currentServer.Version -lt $PatchVersion)
{
Write-Verbose -Message "Current server is running an older path level, patch required"
Set-OOSDscRegKeys -NewMaster $newMaster -Config $currentServer.Config -Roles $currentServer.Roles
Write-Verbose -Message "Removing server from OOS farm"
Remove-OfficeWebAppsMachine
Write-Verbose -Message "Installing OOS Patch"
Install-OOSDscPatch -SetupFile $setupFile
Write-Verbose -Message "Rebooting server to complete installation"
$global:DSCMachineStatus = 1
}
else
{
Write-Verbose -Message "Server already on correct patch level"
}
}
else
{
Write-Verbose -Message "Current server is not the MasterMachine"
# Determine new MasterMachine (one of the already patched servers)
$patchedMachines = $serversInfo | Where-Object -FilterScript { $_.Version -eq $PatchVersion }
if ($null -eq $patchedMachines)
{
# No other machines are patched yet, probably single server. Set current machine to newMaster
$newMaster = $env:COMPUTERNAME
}
else
{
# Determine newMaster from other machines
$newMaster = $patchedMachines.NewMaster | Sort-Object | Select-Object -Unique
if ($newMaster -is [System.Array])
{
Write-Verbose -Message "WARNING: Multiple masters found"
$newMaster = $newMaster | Select-Object -First
}
}
Write-Verbose -Message "Determined the following server to be the new MasterMachine {$newMaster}"
if ($currentServer.Version -lt $PatchVersion)
{
Write-Output "Current version is running an older path level, patch required"
Set-OOSDscRegKeys -NewMaster $newMaster -Config $currentServer.Config -Roles $currentServer.Roles
Write-Verbose -Message "Removing server from OOS farm"
Remove-OfficeWebAppsMachine
Write-Verbose -Message "Installing OOS Patch"
Install-OOSDscPatch -SetupFile $setupFile
Write-Verbose -Message "Rebooting server to complete installation"
$global:DSCMachineStatus = 1
}
else
{
Write-Verbose -Message "Server already on correct patch level"
}
}
}
else
{
# Current server is not joined to a farm
$resume = $false
if (Test-Path -Path $OOSDscRegKey)
{
# Get NewMaster item
$key = Get-Item -Path $OOSDscRegKey
$state = $key.GetValue("State")
if ($state -eq "Patching")
{
$resume = $true
}
}
# Check if the patch is already installed
if ($resume -eq $false)
{
Write-Verbose -Message "Server does not have the patch installed yet."
Write-Verbose -Message "Probably new server which is not yet joined to a farm."
Write-Verbose -Message "Installing OOS Patch"
Install-OOSDscPatch -SetupFile $setupFile
Write-Verbose -Message "Rebooting server to complete installation"
$global:DSCMachineStatus = 1
}
else
{
Write-Verbose -Message "Server is continuing after the required reboot."
Write-Verbose -Message "Check if current machine needs to create new farm"
if ($currentServer.Name -eq $currentServer.NewMaster)
{
Write-Verbose -Message "This server is the new Master and needs to create a new farm"
Write-Verbose -Message "Read required config from registry"
$config = $key.GetValue("Config")
$objParams = $config | ConvertFrom-Json
$params = @{ }
$objParams | Get-Member -MemberType *Property | ForEach-Object -Process {
if ([string]::IsNullOrEmpty($objParams.($_.name)) -eq $false)
{
$params.($_.name) = $objParams.($_.name)
}
}
Write-Verbose -Message "Create new farm based on the old config"
$null = New-OfficeWebAppsFarm @params -Force
}
else
{
Write-Verbose -Message "This server needs to join an existing farm"
Write-Verbose -Message "Read server roles from registry"
$roles = $key.GetValue("Roles") -split ","
$domain = (Get-CimInstance -ClassName Win32_ComputerSystem).Domain
Write-Verbose -Message "Creating new farm with Master server $($currentServer.NewMaster).$domain and roles $($roles -join ", ")"
New-OfficeWebAppsMachine -MachineToJoin "$($currentServer.NewMaster).$domain" -Roles $roles
}
Write-Verbose -Message "Cleaning up registry data"
Remove-Item -Path $OOSDscRegKey -ErrorAction SilentlyContinue
}
}
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$SetupFile,
[Parameter(Mandatory = $true)]
[System.String[]]
$Servers,
[Parameter()]
[ValidateSet("Present", "Absent")]
[System.String]
$Ensure = "Present",
[Parameter()]
[System.Management.Automation.PSCredential]
$InstallAccount
)
Write-Verbose -Message "Testing install status of OOS Update binaries"
$PSBoundParameters.Ensure = $Ensure
if ($Ensure -eq "Absent")
{
throw [Exception] "Office Online Server does not support uninstalling updates."
return
}
# Check if server is continuing after a reboot
if (Test-Path -Path $OOSDscRegKey)
{
$key = Get-Item -Path $OOSDscRegKey
$state = $key.GetValue("State")
if ($state -eq "Patching")
{
Write-Verbose -Message "Server continuing after required reboot after patching."
Write-Verbose -Message "Returning False."
return $false
}
}
$CurrentValues = Get-TargetResource @PSBoundParameters
Write-Verbose -Message "Current Values: $(Convert-OosDscHashtableToString -Hashtable $CurrentValues)"
Write-Verbose -Message "Target Values: $(Convert-OosDscHashtableToString -Hashtable $PSBoundParameters)"
return Test-OosDscParameterState -CurrentValues $CurrentValues `
-DesiredValues $PSBoundParameters `
-ValuesToCheck @("Ensure")
}
Export-ModuleMember -Function *-TargetResource
function Get-ServerInfo
{
[OutputType([System.Array])]
param
(
[Parameter(Mandatory = $true)]
[System.String[]]
$Servers
)
$serversInfo = @()
foreach ($server in $Servers)
{
$serversInfo += Invoke-Command -ComputerName $server -ScriptBlock {
$returnval = @{
Name = $env:COMPUTERNAME
}
$OfficeVersionInfoFile = "C:\ProgramData\Microsoft\OfficeWebApps\Data\local\OfficeVersion.inc"
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $false)
{
$OfficeVersionInfoFile = "C:\Program Files\Microsoft Office Web Apps\AgentManager\OfficeVersion.inc"
}
if ((Test-Path -Path $OfficeVersionInfoFile) -eq $true)
{
$OfficeVersionInfo = Get-Content -Path $OfficeVersionInfoFile -Raw
if ($OfficeVersionInfo -match "RMJ = ([0-9]*)\r\nRMM = ([0-9]*)\r\nRUP = ([0-9]*)\r\nRPR = ([0-9]*)")
{
[System.Version]$versionInfo = $matches[1] + "." + $matches[2] + "." + $matches[3] + "." + $matches[4]
}
else
{
[System.Version]$versionInfo = "0.0.0.0"
}
}
else
{
[System.Version]$versionInfo = "0.0.0.0"
}
$returnval.Version = $versionInfo
try
{
$oosFarm = Get-OfficeWebAppsFarm -ErrorAction SilentlyContinue
if ($null -ne $oosFarm)
{
$oosMachine = Get-OfficeWebAppsMachine
$returnval.Machines = $oosFarm.Machines.MachineName
$returnval.MasterMachine = $oosMachine.MasterMachineName
$returnval.Roles = $oosMachine.Roles -join ","
# Retrieving OOS Farm properties and convert to JSON
$objProps = $oosFarm | Select-Object -Property * -ExcludeProperty ExcelEnableCrossForestKerberosAuthentication, Machines, OfficeAddinEnabled, OnlinePictureEnabled, OnlineVideoEnabled
$jsonProps = $objProps | ConvertTo-Json
$returnval.Config = $jsonProps
}
else
{
$returnval.Machines = $null
$returnval.MasterMachine = $null
}
}
catch
{
$returnval.Machines = $null
$returnval.MasterMachine = $null
}
# Check if NewMaster OOSDscRegKey has been created
$OOSDscRegKey = "HKLM:\SOFTWARE\OOSDsc"
if (Test-Path -Path $OOSDscRegKey)
{
# Get NewMaster item
$key = Get-Item $OOSDscRegKey
$returnval.NewMaster = $key.GetValue("NewMaster")
}
else
{
$returnval.NewMaster = $null
}
return $returnval
}
}
return $serversInfo
}
function Set-OOSDscRegKeys
{
param
(
[Parameter(Mandatory = $true)]
[System.String]
$NewMaster,
[Parameter(Mandatory = $true)]
[System.String]
$Config,
[Parameter(Mandatory = $true)]
[System.String]
$Roles
)
# Testing OOSDsc reg key
if ((Test-Path -Path $OOSDscRegKey) -eq $false)
{
$null = New-Item -Path $OOSDscRegKey
}
# Set "Patching" registry key
$null = New-ItemProperty -Path $OOSDscRegKey -Name State -PropertyType String -Value "Patching" -Force
# Set "NewMaster" registry key to $newMaster
$null = New-ItemProperty -Path $OOSDscRegKey -Name NewMaster -PropertyType String -Value $NewMaster -Force
# Set "Config" registry key to $newMaster
$null = New-ItemProperty -Path $OOSDscRegKey -Name Config -PropertyType String -Value $Config -Force
# Set "Roles" registry key to $newMaster
$null = New-ItemProperty -Path $OOSDscRegKey -Name Roles -PropertyType String -Value $Roles -Force
}
function Install-OOSDscPatch
{
param
(
[Parameter(Mandatory = $true)]
[System.String]
$SetupFile
)
Write-Verbose -Message "Beginning installation of the OOS update"
Write-Verbose -Message $script:localizedData.CheckIfUNC
$uncInstall = $false
if ($SetupFile.StartsWith("\\"))
{
Write-Verbose -Message $script:localizedData.PathIsUNC
$uncInstall = $true
if ($SetupFile -match "\\\\(.*?)\\.*")
{
$serverName = $Matches[1]
}
else
{
throw "Cannot extract servername from UNC path. Check if it is in the correct format."
}
Set-OosDscZoneMap -Server $serverName
}
$setup = Start-Process -FilePath $SetupFile `
-ArgumentList "/quiet /passive" `
-Wait `
-PassThru
if ($uncInstall -eq $true)
{
Write-Verbose -Message $script:localizedData.RemoveUNCPath
Remove-OosDscZoneMap -ServerName $serverName
}
# Error codes: https://aka.ms/installerrorcodes
switch ($setup.ExitCode)
{
0
{
Write-Verbose -Message $script:localizedData.InstallSucceeded
}
17022
{
Write-Verbose -Message $script:localizedData.RebootRequired
}
17025
{
Write-Verbose -Message $script:localizedData.AlreadyInstalled
}
Default
{
throw ("Office Online Server update install failed, exit code was $($setup.ExitCode). " + `
"Error codes can be found at https://aka.ms/installerrorcodes")
}
}
}