-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsClickToRunTools.psm1
More file actions
558 lines (453 loc) · 18.5 KB
/
Copy pathPsClickToRunTools.psm1
File metadata and controls
558 lines (453 loc) · 18.5 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
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
function Get-WebRequestTable {
<#
.SYNOPSIS
Attempts to scrape table from webpage.
.DESCRIPTION
Scrapes a given numbered table for the provided Web
Request response from the Invoke-WebRequest cmdlet.
.PARAMETER WebRequest
HtmlWebResponseObject returned from Invoke-WebRequest cmdlet.
.PARAMETER TableNumber
Index number of the table on the page, in order. First table is default.
.EXAMPLE
$r = Invoke-WebRequest $url
Get-WebRequestTable $r -TableNumber 0 | Format-Table -Auto
P1 P2 P3 P4
-- -- -- --
Gardiner Number Hieroglyph Description of Glyph Details
Q1 Seat Phono. st, ws, . In st ?seat, place,? wsir ?Osiris,? ?tm ?perish.?
Q2 Portable seat Phono. ws. In wsir ?Osiris.?
Q3 Stool Phono. p.
Q4 Headrest Det. in wrs ?headrest.?
Q5 Chest Det. in hn ?box,? ?fdt ?chest.?
Q6 Coffin Det. or Ideo. in qrs ?bury,? krsw ?coffin.?
Q7 Brazier with flame Det. of fire. In ?t ?fire,? s?t ?flame,? srf ?temperature.?
.NOTES
From https://www.leeholmes.com/blog/2015/01/05/extracting-tables-from-powershells-invoke-webrequest/
#>
param(
[Parameter(Position=0,Mandatory = $true)]
[Microsoft.PowerShell.Commands.HtmlWebResponseObject]
$WebRequest,
[Parameter()]
[int]
$TableNumber = 0
)
## Extract the tables out of the web request
$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE"))
$table = $tables[$TableNumber]
$titles = @()
$rows = @($table.Rows)
## Go through all of the rows in the table
foreach($row in $rows) {
$cells = @($row.Cells)
## If we've found a table header, remember its titles
if($cells[0].tagName -eq "TH") {
$titles = @($cells | % { ("" + $_.InnerText).Trim() })
continue
}
## If we haven't found any table headers, make up names "P1", "P2", etc.
if(-not $titles) {
$titles = @(1..($cells.Count + 2) | % { "P$_" })
}
## Now go through the cells in the the row. For each, try to find the
## title that represents that column and create a hashtable mapping those
## titles to content
$resultObject = [Ordered] @{}
for($counter = 0; $counter -lt $cells.Count; $counter++) {
$title = $titles[$counter]
if(-not $title) { continue }
$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()
}
## And finally cast that hashtable to a PSCustomObject
[PSCustomObject] $resultObject
}
}#END: function Get-WebRequestTable
function Get-C2rSupportedVersions {
[CmdletBinding(DefaultParameterSetName='CacheExpires')]
param (
# Major version (15 is ProPlus 2013, 16 is MS 365)
[Parameter(Position=0,ParameterSetName='CacheExpires')]
[Parameter(Position=0,ParameterSetName='SkipCache')]
[Parameter(Position=0,ParameterSetName='ForceCache')]
[ValidateNotNull()]
[ValidateSet(15,16)]
[int]
$MajorVersion=16,
# Time in Days to wait before refreshing cache before next lookup
[Parameter(ParameterSetName='CacheExpires')]
[int]
$CacheExpires = 7,
# Force fresh lookup from web
[Parameter(ParameterSetName='SkipCache')]
[switch]
$SkipCache,
# Prevent fresh lookup from web
[Parameter(ParameterSetName='ForceCache')]
[switch]
$ForceCache
)
# Determine if we should force or check cache file
$PerformWebLookup = if ($ForceCache) {
# Always go to cache file
$false
} elseif ($SkipCache) {
# Always go to web check
$true
} elseif (Test-TableXmlInCache) {
# Perform cache file analysis by modified Date
$CacheDate = (Get-TableXmlInCacheItem).LastWriteTime
$Now = Get-Date
if ($Now.AddDays(-$CacheExpires) -ge $CacheDate) {
# Update the cache file
$true
} else {
# Use the cache file
$false
}
} else {
# No cache found, perform lookup
$true
}#END: $PerformWebLookup = if ($ForceCache)
if ($PerformWebLookup) {
# URL for the 2013/ProPlus online chart we will scrape
$Uri15page = 'https://docs.microsoft.com/en-us/officeupdates/update-history-office-2013'
# this is going to be legacy at some point, but we will support it on "day 2".
# URL for the MS 365 online chart we will scrape
$Uri16page = 'https://docs.microsoft.com/en-us/officeupdates/update-history-microsoft365-apps-by-date'
# Determine the use case and set local variable for web request
switch ($MajorVersion) {
15 {
$Uri = $Uri15page
}
16 {
$Uri = $Uri16page
}
}
# Get Web Request
$WebRequest = Invoke-WebRequest -Uri $Uri
$Table = Get-WebRequestTable $WebRequest -TableNumber 0
# Determine the use case and convert data to object data types
switch ($MajorVersion) {
15 {
foreach ($record in $Table) {
# Release year is an int
# the year is sometime null, so we infer the previous value
$year = if ($record.'Release year') {$record.'Release year' -as [int]} else {$year -as [int]}
$record.'Release year' = $year
# Release date is a partial date string. Leave as is for now
# Version number is a version
$record.'Version number' = $record.'Version number' -as [version]
# More information is a link, leave as string but remove the space
$record.'More information' = $record.'More information' -replace '\s'
# Create a new property as a datetime based off two previous fields
# the date is a string we can split into a month and a day
$tempArr = $record.'Release date' -split '\s'
$month = $tempArr[0]
$day = $tempArr[1]
# We can parse the string with its known format, then add the new member
$ReleasedOn = [datetime]::parseexact("$($month)-$($day)-$($year)", 'MMMM-d-yyyy', $null)
$record | Add-Member -MemberType NoteProperty -Name ReleasedOn -Value $ReleasedOn
}#END: foreach ($record in $Table)
}#END: 15
16 {
foreach ($record in $Table) {
# Channel is a string, OK
# Version is a number but sometimes is has a letter (20H2)
# Build is a PART of a version number, but we can leave it as a string here
# Release date is a datetime
$record.'Release date' = $record.'Release date' -as [datetime]
#Version supported until is a date but sometimes it's not. Leave as string for now
}#END: foreach ($record in $Table)
# Calculate which item should have latest build
# this will add a boolean property to Table
$grpChannelSupported = $Table |
Group-Object Channel
$Table = Foreach ($G in $grpChannelSupported) {
# If multiple items for this channel, do some logic
$LatestBuildShouldBe = if (@($G.Group).Count -gt 1) {
# Grab the builds and cast as versions, sort and select
[string]$LastestBuild = $G.Group.Build |
Foreach-Object {[version]$_} |
Sort-Object -Descending |
Select-Object -First 1
# Choose the item with the latest build
$G.Group | Where-Object {$_.Build -eq $LastestBuild} |
Select-Object -Expand 'Build'
} else {
# There is only 1, choose it.
@($G.Group.Build)[0]
}#END: $LatestBuildShouldBe = if (@($G.Group).Count -gt 1)
# Tag the item with latest build
$G.Group | Select-Object *, @{Name='isLatestBuild';Exp={
if ($_.Build -eq $LatestBuildShouldBe) {$true}else{$false}
}}
}#END: Foreach ($G in $grpChannelSupported)
}#END: 16
}#END: switch ($MajorVersion)
# Export the table to cache
Save-TableAsXmlInCache -Table $Table
} else {
# Pull table from the cache
$Table = Import-TableXmlInCache
}#END: if ($PerformWebLookup)
Write-Output $Table
}#END: function Get-C2rSupportedVersions
function Get-C2rChannelInfo {
<#
.SYNOPSIS
Gives the 'change' parameter value, GUID, and channel name for the given channel.
.DESCRIPTION
Returns the 'change' parameter value, GUID, and channel name required when changing the C2R update channel from the command line.
.EXAMPLE
$pValue = Get-C2rChannelInfo -ChannelName 'Monthly Enterprise Channel' | % ChangeParameterValue
icm ('"C:\Program Files\Common Files\Microsoft Shared\ClickToRun\OfficeC2RClient.exe" /changesetting Channel={0}' -f $pValue)
#>
[CmdletBinding(DefaultParameterSetName='All')]
param (
# Search by Channel Name (Default)
[Parameter(ParameterSetName='byChannelName')]
[AllowNull()]
[ValidateSet(
'Current Channel',
'Current (Preview)',
'Semi-Annual Enterprise Channel',
'Semi-Annual Enterprise Channel (Preview)',
'Monthly Enterprise Channel',
'Beta Channel'
)]
[string]
$ChannelName,
# Search by GUID
[Parameter(ParameterSetName='byGuid')]
[AllowNull()]
[ValidateSet(
'492350f6-3a01-4f97-b9c0-c7c6ddf67d60',
'64256afe-f5d9-4f86-8936-8840a6a4f5be',
'7ffbc6bf-bc32-4f92-8982-f9dd17fd3114',
'b8f9b850-328d-4355-9145-c59439a0c4cf',
'55336b82-a18d-4dd6-b5f6-9e5095c314a6',
'5440fd1f-7ecb-4221-8110-145efaa6372f',
'f2e724c1-748f-4b47-8fb8-8e0d210e9208',
'2e148de9-61c8-4051-b103-4af54baffbb4'
)]
[guid]
$Guid,
# Placeholder for null case
[Parameter(ParameterSetName='All')]
$All
)
# Define the table of objects in code
$srcTable = @(
[pscustomobject]@{
CdnUrlGuid = [guid]'492350f6-3a01-4f97-b9c0-c7c6ddf67d60'
ChangeParameterValue = 'Current'
OfficialName = 'Current Channel'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'64256afe-f5d9-4f86-8936-8840a6a4f5be'
ChangeParameterValue = 'FirstReleaseCurrent'
OfficialName = 'Current (Preview)'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'7ffbc6bf-bc32-4f92-8982-f9dd17fd3114'
ChangeParameterValue = 'Broad'
OfficialName = 'Semi-Annual Enterprise Channel'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'b8f9b850-328d-4355-9145-c59439a0c4cf'
ChangeParameterValue = 'Targeted'
OfficialName = 'Semi-Annual Enterprise Channel (Preview)'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'55336b82-a18d-4dd6-b5f6-9e5095c314a6'
ChangeParameterValue = 'MonthlyEnterpise'
OfficialName = 'Monthly Enterprise Channel'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'5440fd1f-7ecb-4221-8110-145efaa6372f'
ChangeParameterValue = 'BetaChannel'
OfficialName = 'Beta Channel'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'f2e724c1-748f-4b47-8fb8-8e0d210e9208'
ChangeParameterValue = 'N/A'
OfficialName = 'N/A'
},
[pscustomobject]@{
CdnUrlGuid = [guid]'2e148de9-61c8-4051-b103-4af54baffbb4'
ChangeParameterValue = 'N/A'
OfficialName = 'N/A'
}
)#END: $srcTable = @()
Write-Debug "Parameter Set: $($PSCmdlet.ParameterSetName)"
switch($PSCmdlet.ParameterSetName) {
'byChannelName' {
$srcTable | Where-Object {$_.OfficialName -eq $ChannelName}
}
'byGuid' {
$srcTable | Where-Object {$_.CdnUrlGuid -eq $Guid}
}
'All' {
$srcTable
}
}#END: switch($PSCmdlet.ParameterSetName) {}
}#END: function Get-C2rChannelInfo
function Test-Ms365RequiresUpdate {
<#
.SYNOPSIS
Checks the version info to see if the PC requires an update.
.DESCRIPTION
Checks the version info against the current online list to see if the item requires an update.
.EXAMPLE
Get-SoftwareList -Company XYZ -IncludeAppName 'Microsoft 365*' |
Where {$_.ComputerName -eq 'PC1'} |
Test-Ms365RequiresUpdate -Channel 'Monthly Enterprise Channel' |
Select -Expand RequiresUpdate
False
.EXAMPLE
Get-InstalledSoftware -ComputerName PC1 |
Where {$_.Name -like 'Microsoft 365*'} |
Test-Ms365RequiresUpdate -Channel 'Monthly Enterprise Channel' |
Select -Expand RequiresUpdate
False
.NOTES
v1.0 will support basic check of version vs channel name
Later versions will include
- check vs channel guid
- Option to pass if version is not latest but is still supported.
#>
[CmdletBinding(DefaultParameterSetName='byChannelName')]
param (
# Given Channel NAME to check the version against
[Parameter(Mandatory=$true,
ParameterSetName='byChannelName')]
[ValidateSet(
'Current Channel',
'Current (Preview)',
'Semi-Annual Enterprise Channel',
'Semi-Annual Enterprise Channel (Preview)',
'Monthly Enterprise Channel',
'Beta Channel'
)]
[string]
$Channel,
# Given Channel GUID to check the version against
[Parameter(Mandatory=$true,
ParameterSetName='byUniqueID')]
[guid]
$Guid,
# Object containing Version and ComputerName from the PC to test. Must not include non-MS365 software items.
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[ValidateScript({
([version]$_.Version) -is [version] -and
$_.ComputerName -is [string]
})]
[PsCustomObject]
$InputObject,
# Chart of supported MS365 versions
[Parameter()]
[ValidateNotNull()]
[pscustomobject]
$C2rSupportedVersions = (Get-C2rSupportedVersions -MajorVersion 16)
)
begin {
# Define an output object
$OutputObject = New-Object System.Collections.ArrayList
}
process {
Foreach ($obj in $InputObject) {
# Find the item for comparison
$LatestChannelBuild = $C2rSupportedVersions | Where-Object {
$_.isLatestBuild -and
$_.channel -eq $channel
} | Select-Object -Expand 'Build'
# Does the computer have the latest version?
$objBuild = "$(([version]($obj.Version)).Build).$(([version]($obj.Version)).Revision)"
$isLatestVersion = [version]($LatestChannelBuild) -le ([version]$objBuild)
# Create an output object with names, version, boolean
$thisObj = [PSCustomObject]@{
ComputerName = $obj.ComputerName
Channel = $Channel
RequiresUpdate = !$isLatestVersion
BuildShouldBe = $LatestChannelBuild
BuildIs = $objBuild
isLatestVersion = $isLatestVersion
ComputerId = $obj.ComputerId
}
[void]($OutputObject.Add($thisObj))
}
}
end {
Write-Output $OutputObject
}
}#END: function Test-Ms365RequiresUpdate
function Get-CacheDir {
$CacheDir = if ($PsScriptRoot) {
Join-Path $PsScriptRoot "cache"
} else {
$module = $env:PSModulePath -split ';' |
Where-Object {$_ -like '*users*'} |
Select-Object -First 1
Join-Path $module "PsClickToRunTools\cache"
}
$CacheDir
}#END: function Get-CacheDir
function Get-CachedXmlPath {
$CacheDir = Get-CacheDir
$CachedXmlPath = Join-Path $CacheDir "c2r-channels.xml"
$CachedXmlPath
}
function Save-TableAsXmlInCache {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,Position=0)]
[pscustomobject]
$Table
)
$CacheDir = Get-CacheDir
if ( -not (Test-Path $CacheDir)) {
New-Item $CacheDir -ItemType Directory -Force
}
$CachedXmlPath = Get-CachedXmlPath
$Table | Export-CliXml -Path $CachedXmlPath
}#END: function Save-TableAsXmlInCache
function Test-TableXmlInCache {
$CachedXmlPath = Get-CachedXmlPath
if (Test-Path $CachedXmlPath) {
$true
} else {
$false
}
}#END: function Test-TableXmlInCache
function Get-TableXmlInCacheItem {
$CachedXmlPath = Get-CachedXmlPath
if (Test-TableXmlInCache) {
Get-Item $CachedXmlPath
} else {
throw "Cache file not found!"
}
}#END: function Get-TableXmlInCacheItem
function Import-TableXmlInCache {
$CachedXmlPath = Get-CachedXmlPath
if (Test-TableXmlInCache) {
Import-CliXml -Path $CachedXmlPath
} else {
throw "Cache file not found!"
}
}#END: function Import-TableXmlInCache
# Use available handshake protocols
[Net.ServicePointManager]::SecurityProtocol =
[enum]::GetNames([Net.SecurityProtocolType]) | Foreach-Object {
[Net.SecurityProtocolType]::$_
}
<#
# Selenium might work better
$dllPath = "$($PsScriptRoot)\lib\WebDriver.dll"
$myPaths = $env:Path -split ';'
if ($myPaths -notcontains $dllPath) {
$env:Path += ";$dllPath"
}
Add-Type -Path $dllPath
#>