private/Find-AssetUri.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
function Find-AssetUri {
<#
    Get uri of Microsoft download asset specified by its GUID
#>

    [CmdletBinding()]
    Param(
        [String]
            [Parameter(
                Mandatory,
                ValueFromPipeline,
                HelpMessage = "GUID of Microsoft download asset."
            )]
        $GUID
    )

    BEGIN {
        $updateCatalogDownloadLink = 'http://www.catalog.update.microsoft.com/DownloadDialog.aspx'

        $assetUriPattern =  "https?://download\.windowsupdate\.com\/[^ \'\""]+"
        $postBodyTemplate = '"size": 0, "uidInfo": "{0}", "updateID": "{0}"' -replace ' ', ''
    }

    PROCESS {
        $postBody = @{ updateIDs = "[{$( $postBodyTemplate -f $GUID )}]" }

        Write-Verbose "Download description of asset $GUID"
        if ( ( Invoke-WebRequest -Uri $updateCatalogDownloadLink -Method Post -Body $postBody
             ).Content -match $assetUriPattern
        ) {
            $Matches[0]
        }
    }

    END {}
}