-
Notifications
You must be signed in to change notification settings - Fork 613
/
Copy pathConfigureOfficeApps.ps1
315 lines (234 loc) · 10 KB
/
ConfigureOfficeApps.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
<#Author : Akash Chawla
# Usage : Configure office applications for AVD
#>
#############################################
# Configure Office applications #
#############################################
[CmdletBinding()] Param (
[Parameter(
Mandatory
)]
[ValidateSet("Word","PowerPoint","Access","Excel","OneNote","Outlook","Publisher","Visio", "Project")]
[System.String[]]$Applications,
[Parameter(
Mandatory
)]
[ValidateSet("32", "64")]
[string]$Version,
[Parameter(
Mandatory
)]
[ValidateSet("Add", "Remove")]
[string]$Type
)
function AddProductsToConfigurationXML {
[CmdletBinding()] Param (
[Parameter(
Mandatory
)]
[ValidateSet("Visio","Project")]
[System.String[]]$Applications,
[Parameter(
Mandatory
)]
$xmlFile,
[Parameter(
Mandatory
)]
[string]$xmlFilePath,
[Parameter(
Mandatory
)]
[ValidateSet("32", "64")]
[string]$Version
)
Begin {
try {
$addElement = $xmlFile.DocumentElement.Add
if ($null -eq $addElement) {
Throw "Not able to access the xml element"
}
$addElement.setAttribute("OfficeClientEdition", $Version)
$VisioProductID = "VisioProRetail"
$projectProductID = "ProjectProRetail"
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
Process {
try {
foreach ($app in $Applications) {
Write-Host " AVD AIB Customization Office apps: Request to add $app"
$productElement = $xmlFile.CreateElement("Product");
$languageElement = $xmlFile.CreateElement("Language")
$languageElement.setAttribute("ID", "MatchOS")
$productElement.AppendChild($languageElement)
if ($app -eq "Visio") {
$productElement.setAttribute("ID", $VisioProductID)
}
if ($app -eq "Project") {
$productElement.setAttribute("ID", $ProjectProductID)
}
$addElement.AppendChild($productElement)
}
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
End {
$xmlFile.Save($xmlFilePath)
}
}
function RemoveProductsFromConfigurationXML {
[CmdletBinding()] Param (
[Parameter(
Mandatory
)]
[ValidateSet("Word","PowerPoint","Access","Excel","OneNote","Outlook","Publisher")]
[System.String[]]$Applications,
[Parameter(
Mandatory
)]
$xmlFile,
[Parameter(
Mandatory
)]
[string]$xmlFilePath,
[Parameter(
Mandatory
)]
[ValidateSet("32", "64")]
[string]$Version
)
Begin {
try {
$addElement = $xmlFile.DocumentElement.Add
$addElement.setAttribute("OfficeClientEdition", $Version)
$productID = "O365ProPlusRetail"
$productElement = $xmlFile.CreateElement("Product")
$productElement.setAttribute("ID", $productID)
$languageElement = $xmlFile.CreateElement("Language")
$languageElement.setAttribute("ID", "MatchOS")
$productElement.AppendChild($languageElement)
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
Process {
try {
foreach ($app in $Applications) {
Write-Host " AVD AIB Customization Office apps: Request to remove $app"
$excludeElement = $xmlFile.CreateElement("ExcludeApp")
$excludeElement.setAttribute("ID", $app);
$productElement.AppendChild($excludeElement)
}
$addElement.AppendChild($productElement)
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
End {
$xmlFile.Save($xmlFilePath)
}
}
function ConfigureOfficeXML($Applications, $xmlFile, $xmlFilePath, $Version, $Type) {
if ($Type -eq "Add") {
Write-Host " AVD AIB Customization Office apps: Adding office applications"
AddProductsToConfigurationXML -Applications $Applications -xmlFile $file -xmlFilePath $xmlFilePath -Version $Version
}
else {
Write-Host " AVD AIB Customization Office apps: Removing office applications"
RemoveProductsFromConfigurationXML -Applications $Applications -xmlFile $file -xmlFilePath $xmlFilePath -Version $Version
}
}
function installOfficeUsingODT($Applications, $Version, $Type) {
Begin {
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
Write-Host "Starting AVD AIB Customization : Office Apps : $((Get-Date).ToUniversalTime())"
$configXML = @'
<Configuration>
<Add Channel="Monthly">
</Add>
<RemoveMSI />
<Updates Enabled="FALSE" />
<Display Level="None" AcceptEULA="TRUE" />
<Property Name="FORCEAPPSHUTDOWN" Value="TRUE" />
<Property Name="SharedComputerLicensing" Value="1" />
</Configuration>
'@
$ODTDownloadLinkRegex = '/officedeploymenttool[a-z0-9_-]*\.exe$'
$guid = [guid]::NewGuid().Guid
$tempFolder = (Join-Path -Path "C:\temp\" -ChildPath $guid)
$ODTDownloadUrl = 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=49117'
$templateFilePathFolder = "C:\AVDImage"
if (!(Test-Path -Path $tempFolder)) {
New-Item -Path $tempFolder -ItemType Directory
}
Write-Host "AVD AIB Customization Office Apps : Created temp folder $tempFolder"
}
Process {
try {
$HttpContent = Invoke-WebRequest -Uri $ODTDownloadUrl -UseBasicParsing
if ($HttpContent.StatusCode -ne 200) {
throw "Office Installation script failed to find Office deployment tool link -- Response $($Response.StatusCode) ($($Response.StatusDescription))"
}
$ODTDownloadLinks = $HttpContent.Links | Where-Object { $_.href -Match $ODTDownloadLinkRegex }
#pick the first link in case there are multiple
$ODTToolLink = $ODTDownloadLinks[0].href
Write-Host "AVD AIB Customization Office Apps : Office deployment tool link is $ODTToolLink"
$ODTexePath = Join-Path -Path $tempFolder -ChildPath "officedeploymenttool.exe"
#download office deployment tool
Write-Host "AVD AIB Customization Office Apps : Downloading ODT tool into folder $ODTexePath"
$ODTResponse = Invoke-WebRequest -Uri "$ODTToolLink" -UseBasicParsing -UseDefaultCredentials -OutFile $ODTexePath -PassThru
if ($ODTResponse.StatusCode -ne 200) {
throw "Office Installation script failed to download Office deployment tool -- Response $($ODTResponse.StatusCode) ($($ODTResponse.StatusDescription))"
}
Write-Host "AVD AIB Customization Office Apps : Extracting setup.exe into $tempFolder"
# extract setup.exe
Start-Process -FilePath $ODTexePath -ArgumentList "/extract:`"$($tempFolder)`" /quiet" -PassThru -Wait -NoNewWindow
$setupExePath = Join-Path -Path $tempFolder -ChildPath 'setup.exe'
# Construct XML config file for Office Deployment Kit setup.exe
$xmlFilePath = Join-Path -Path $tempFolder -ChildPath 'installOffice.xml'
Write-Host "AVD AIB Customization Office Apps : Saving xml content into xml file : $xmlFilePath"
$configXML | Out-File -FilePath $xmlFilePath -Force -Encoding ascii
[XML]$file = Get-Content $xmlFilePath
ConfigureOfficeXML -Applications $Applications -xmlFile $file -xmlFilePath $xmlFilePath -Version $Version -Type $Type
Write-Host "AVD AIB Customization Office Apps : Running setup.exe to download Office"
$ODTRunSetupExe = Start-Process -FilePath $setupExePath -ArgumentList "/download $(Split-Path -Path $xmlFilePath -Leaf)" -PassThru -Wait -WorkingDirectory $tempFolder -WindowStyle Hidden
if (!$ODTRunSetupExe) {
Throw "AVD AIB Customization Office Apps : Failed to run `"$setupExePath`" to download Office"
}
if ( $ODTRunSetupExe.ExitCode) {
Throw "AVD AIB Customization Office Apps : Exit code $($ODTRunSetupExe.ExitCode) returned from `"$setupExePath`" to download Office"
}
Write-Host "AVD AIB Customization Office Apps : Running setup.exe to Install Office"
$InstallOffice = Start-Process -FilePath $setupExePath -ArgumentList "/configure $(Split-Path -Path $xmlFilePath -Leaf)" -PassThru -Wait -WorkingDirectory $tempFolder -WindowStyle Hidden
if (!$InstallOffice) {
Throw "AVD AIB Customization Office Apps : Failed to run `"$setupExePath`" to install Office"
}
if ( $ODTRunSetupExe.ExitCode ) {
Throw "AVD AIB Customization Office Apps : Exit code $($ODTRunSetupExe.ExitCode) returned from `"$setupExePath`" to download Office"
}
}
catch {
$PSCmdlet.ThrowTerminatingError($PSitem)
}
}
End {
#Cleanup
if ((Test-Path -Path $tempFolder -ErrorAction SilentlyContinue)) {
Remove-Item -Path $tempFolder -Force -Recurse -ErrorAction Continue
}
if ((Test-Path -Path $templateFilePathFolder -ErrorAction SilentlyContinue)) {
Remove-Item -Path $templateFilePathFolder -Force -Recurse -ErrorAction Continue
}
$stopwatch.Stop()
$elapsedTime = $stopwatch.Elapsed
Write-Host "Ending AVD AIB Customization : Office Apps - Time taken: $elapsedTime"
}
}
installOfficeUsingODT -Applications $Applications -Version $Version -Type $Type