Expand-ShortUrl.psm1

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
Function Expand-ShortURL {
    <#
        .SYNOPSIS
            Expand Short URLs
        .DESCRIPTION
            Unshortens the short URL using WebRequest
        .PARAMETER Url
            Short URL to be expanded
        .EXAMPLE
            PS > Expand-ShortURL -URL https://goo.gl/2V2KdA, https://goo.gl/grzdSj

            ShortURL LongURL
            -------- -------
            https://goo.gl/2V2KdA https://www.thomas-illiet.fr/
            https://goo.gl/grzdSj https://cookbook.thomas-illiet.fr/
    #>

    Param(
            [Parameter(
                Mandatory = $true,
                ValueFromPipeline = $true,
                Position = 0
            )]
            [ValidateNotNullOrEmpty()]
            [string[]] $Url
    )
    Begin{
    }
    Process
    {
        Foreach($Item in $URL){
            try {
                Write-Verbose -Message "$URL - Querying..."
                [PSCustomObject]@{  
                    ShortURL = $Item
                    LongURL = (Invoke-WebRequest -Uri $item -MaximumRedirection 0 -ErrorAction Ignore).Headers.Location
                }
            }
            catch {
                $_.exception.Message
            }
        }
    }
    End{
    }
}