Private/Get-AlternateLinks.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
function Get-AlternateLinks {
    [CmdletBinding()]
    param(
        [Parameter(Position=0)]
        [object]$Headers    # gotta be generic since editions have different types
    )

    # Desktop Header: System.Collections.Generic.Dictionary<string,string>
    # Core Header : System.Collections.Generic.Dictionary<string,IEnumerable<string>>

    $reAltLink = '<(?<uri>\S+)>;rel="alternate"'

    if ($Headers -and $Headers.ContainsKey('Link')) {
        # Regardless of how the link headers are formatted in the response, Desktop
        # edition will concatenate multiple values with a comma. But Core edition
        # will return each one in a string array. So we're going to split each string
        # on a comma to normalize the output.
        $links = $response.Headers['Link'] | ForEach-Object {
            $_.Split(',') | ForEach-Object { $_ }
        }
        Write-Debug "links has $($links.Count) entries"

        # now find and return the URIs for only the rel="alternate" ones
        $links | ForEach-Object {
            if ($_ -match $reAltLink) {
                $matches['uri']
            }
        }
    }

}