public/Find-LatestUpdate.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
function Find-LatestUpdate {
<#

    .SYNOPSIS
Finds the latest delta or cumulative update for Windows or Windows Server operating system


    .DESCRIPTION
This script will return the list of Cumulative updates for Windows 10 and Windows Server 2016 from the Microsoft Update Catalog.


    .PARAMETER Build
Windows 10 Build Number used to filter avaible Downloads

    10240 - Windows 10 Version 1507
    10586 - Windows 10 Version 1511
    14393 - Windows 10 Version 1607 and Windows Server 2016
    15063 - Windows 10 Version 1703
    16299 - WIndows 10 Version 1709


    .PARAMETER Filter
Search filter for updates. The default filter will find Cumulative updates for x86 and x64 platforms.

If multiple strings are specified, only update that match *ALL* strings will be found.

    Cumulative: find only Cumulative updates
    Delta: find only Delta updates
    x86: find only updates for x86 platform
    x64: find only updates for x64 platform


    .NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

Contributors:
    Andriy Melnyk (mao@bebee.xyz)


    .LINK
https://support.microsoft.com/en-us/help/4000823


    .EXAMPLE
Get link to the latest Cumulative update for Windows 10, both for x86 and x64 platforms

    Find-LatestUpdate -verbose


    .EXAMPLE
Get link to the latest Cumulative update for Windows 10 x86

    Find-LatestUpdate -filter Cumulative, x86


    .EXAMPLE
Get the latest Cumulative Update for Windows Server 2016

    Find-LatestUpdate -filter Cumulative, x64 -build 14393


    .EXAMPLE
Get the latest Cumulative Updates for Windows 10 (both x86 and x64) and download to the %TEMP% directory.

    Find-LatestUpdate | %{ Start-BitsTransfer -Source $_ -Destination $ENV:Temp }

#>


    [CmdletBinding()]
    Param(
        [String]
            [Parameter( HelpMessage = "Windows build number." )]
            [ValidateSet( '16299', '15063', '14393', '10586', '10240' )]
        $Build = '16299',

        [String[]]
            [Parameter( HelpMessage = "Windows update Catalog Search Filter." )]
            [ValidateSet( 'x64', 'x86', 'Cumulative', 'Delta', $null )]
        $Filter = @( "Cumulative", "x64" )
    )

    # download JSON with updates
    Write-Verbose "Downloading $startKB to retrieve the list of updates"
    $updates = Get-UpdateJson $startKB


    # Get list of all articles related to the requested build
    $articles = Find-KBArticleWithUpdate -source $updates -build $Build -verbose


    # Extract urls from the list of Article objects and select the latest one
    $kbId = Find-ArticleUri -article $articles |
        Sort-Object -Property Version -Descending |
        Select-Object -First 1

    # Get the article's content
    Write-Verbose "Found article: KB$( $kbId.articleId ), $( $kbId.Uri )"
    $kbContent = Invoke-WebRequest -Uri $kbId.Uri


    # Get html element IDs of buttons labeled as 'Download'
    $elementIDs = $kbContent.InputFields |
        Where-Object { $_.Type -eq 'Button' -and $_.Value -eq 'Download' } |
        Select-Object -ExpandProperty ID


    # Get GUIDs of assets which match all patterns in $Filter argument
    $kbGUIDs = $kbContent.Links |
        Where-Object ID -match '_link' |
        Where-Object { $_.OuterHTML -match ($Filter -join '.*') } |
        ForEach-Object { $_.Id.Replace('_link','') } |
        Where-Object { $_ -in $elementIDs }


    # Get direct download links for all GUIDs of assets and
    $kbGUIDs |
        Find-AssetUri |
        Select-Object -Unique


    <#
        ForEach-Object {
            [psCustomObject] @{ Source = $_ }
        }
    #>


}