Last active
June 29, 2017 15:06
-
-
Save tadcrazio/647894e10e18984589d452d25317280c to your computer and use it in GitHub Desktop.
Powershell function to make a URL tiny.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -AssemblyName System.Web | |
function Get-TinyURL | |
{ | |
<# | |
.SYNOPSIS | |
Make a URL tiny. | |
.DESCRIPTION | |
Enter a URL to make tiny. Will encode URL's ideal for url's that have reserverd characters. | |
.PARAMETER URL | |
The url you wish to make tiny. | |
.EXAMPLE | |
Get-TinyUrl -URL www.google.com | |
.NOTES | |
Nothing to see here. | |
#> | |
[OutputType([string])] | |
param | |
( | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
HelpMessage = 'Long URL')] | |
[ValidateNotNullOrEmpty()] | |
[string]$URL | |
) | |
try | |
{ | |
if ($URL -notcontains '^https?://') | |
{ | |
$URL = 'http://' + $URL | |
} | |
$Newurl = [System.Web.HttpUtility]::UrlEncode($URL) | |
Invoke-RestMethod -Uri "http://tinyurl.com/api-create.php?url=$Newurl" | |
} | |
catch | |
{ | |
Write-Output "Doh! That didn't work.`n $_.Exception.Message" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment