2013-01-17

Another PowerShell vExpert.me URL Shortner

Building on Jonathan Medd’s excellent idea of Using PowerShell to access the vExpert.me URL Shortener, I decided to improve it a bit more.
Here is the completed script.
<#
 .SYNOPSIS
  Will create a new vExpert.me URL

 .DESCRIPTION
  Using the Invoke-Rest Cmdlet to invoke a creation of a new vExpert.me URL

 .PARAMETER  URL
  URL that should be shortened.
 
 .PARAMETER Custom
  The custom URL that should be used.

 .EXAMPLE
  PS C:\> New-vExpertURL -URL 'http://www.google.com'
  This example shows how to call the New-vExpertURL function with with the URL parameter and generate a random URL.

 .EXAMPLE 
  PS C:\> New-vExpertURL -URL 'http://www.google.com' -Custom this_is_my_link
  This will create a custome URL of http://vexpert.me/this_is_my_link pointing to http://www.google.com
 .INPUTS
  System.String

 .OUTPUTS
  System.String

 .NOTES
  For more information about advanced functions, call Get-Help with any
  of the topics in the links listed below.

#>
function New-vExpertURL {
 [CmdletBinding()]
 param(
  [Parameter(Position=0, Mandatory=$true)]
  [System.String]$URL,
  [Parameter(Position=1)]
  [System.String]$Custom
 )
 begin {
 if (!$($Custom) ) {
  $baseurl_2 = "&action=shorturl&format=json&url="
  } else {
  $baseurl_2 = "&action=shorturl&keyword=" + $Custom + "&format=json&url="
 }
 $baseurl_1 = "http://vexpert.me/yourls-api.php?signature="
 $secret = "xxxxxxxxx"
 
 }
 process {
 $invokeurl = $baseurl_1 + $secret + $baseurl_2 + $URL
 $vExpertme = Invoke-RestMethod -Uri $invokeurl
 $vExpertme.shorturl | clip
 Write-Host "The shortenend URL is now in your clipboard" -ForegroundColor Green
 }
 end {
 }
}
It is quite self explanatory. You will need to enter your personal secret code in Line 47.
So I added 4 things
  1. This is now a function – and it accepts parameters.
  2. One of the parameters is CustomURL which will allow you to enter your custom text if you please.
  3. The output will provide the URL and a success message.
  4. The URL will now be in your clipboard so you can use it.