PoshCode Archive  Artifact [5c07082835]

Artifact 5c0708283571185497b8c75caa4cfec83e26c4a98301327bc39567ecdbdb1bd4:

  • File Get-ShortenedURL.ps1 — part of check-in [3290977726] at 2018-06-10 13:32:26 on branch trunk — A function that returns the actual URL from a http redirect. Accepts pipeline input but does nothing useful with errors. (user: Chris Campbell size: 2466)

# encoding: ascii
# api: powershell
# title: Get-ShortenedURL
# description: A function that returns the actual URL from a http redirect. Accepts pipeline input but does nothing useful with errors.
# version: 0.1
# type: function
# author: Chris Campbell 
# license: CC0
# function: Get-ShortenedURL
# x-poshcode-id: 3865
# x-archived: 2013-01-10T19:08:14
# x-published: 2013-01-07T20:51:00
#
#
Function Get-ShortenedURL {
 <#
.SYNOPSIS
 
    Get-ShortenedURL
    
    Author: Chris Campbell (@obscuresec)
    License: BSD 3-Clause
    
.DESCRIPTION

    A function that returns the actual URL from a http redirect.

.PARAMETER $ShortenedURL

    Specifies the shortened URL.

.EXAMPLE

    PS C:\> Get-ShortenedURL -ShortenedURL http://goo.gl/V4PKq
    PS C:\> Get-ShortenedURL -ShortenedURL http://goo.gl/V4PKq,http://bit.ly/IeWSIZ
    PS C:\> Get-Content C:\urls.txt | Get-ShortenedURL 
    PS C:\> Get-ShortenedURL -ShortenedURL (Get-Content C:\urls.txt)

.LINK

    http://obscuresecurity.blogspot.com/2013/01/Get-ShortenedURL.html
    https://github.com/obscuresec/random/blob/master/Get-ShortenedURL

#>

    [CmdletBinding()] Param(
            [Parameter(Mandatory=$True,ValueFromPipeline=$True)]             
            [string[]] $ShortenedURL 
            )

    BEGIN {}
        
    PROCESS {

       Try {
            #Loop through each URL in the array
            Foreach ($URL in $ShortenedURL) {
                
                #Create the WebClient Object and request
                $WebClientObject = New-Object System.Net.WebClient
                $WebRequest = [System.Net.WebRequest]::create($URL)
                $WebResponse = $WebRequest.GetResponse()
                
                #Parse out redirected URL
                $ActualDownloadURL = $WebResponse.ResponseUri.AbsoluteUri
                
                #Create custom object to store results
                $ObjectProperties = @{ 'Shortened URL' = $URL;
                                       'Actual URL' = $ActualDownloadURL}
                $ResultsObject = New-Object -TypeName PSObject -Property $ObjectProperties
                
                #Output the results
                Write-Output $ResultsObject
                
                #Close the webclient connection
                $WebResponse.Close()
            }       
       }

        Catch {}
    }

    END {}
}