param( [string]$longurl = $null, [string]$provider = "tinyurl" ); $script:DIGG_APPKEY = "http://devcentral.f5.com/PoshShrink"; $script:BITLY_LOGIN = "joepruitt"; $script:BITLY_KEY = "R_a49fa1ba91c12356a1ab223f08b2ba30"; #---------------------------------------------------------------------------- # function Set-BitlyServiceInfo #---------------------------------------------------------------------------- function Set-BitlyServiceInfo() { param( [string]$login = $null, [string]$key = $null ); if ( $login -and $key ) { $script:BITLY_LOGIN = $login; $script:BITLY_KEY = $key; } else { Write-Error "Usage: Set-BitlyServiceInfo -login login -key key"; } } #---------------------------------------------------------------------------- # function Shrink-Url #---------------------------------------------------------------------------- function Shrink-Url() { param( [string]$longurl = $null, [string]$provider = "tinyurl" ); $shorturl = $null; if ( $longurl ) { switch ($provider.ToLower()) { "is.gd" { $shorturl = Execute-HTTPGetCommand "http://is.gd/api.php?longurl=$longurl"; } "tinyurl" { $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl"; } "snurl" { $shorturl = Execute-HTTPGetCommand "http://snipr.com/site/snip?r=simple&link=$longurl"; } "digg" { [xml]$results = Execute-HTTPGetCommand ` "http://services.digg.com/url/short/create?url=${longurl}&appkey=${script:DIGG_APPKEY}"; $shorturl = $results.shorturls.shorturl.short_url; } "tr.im" { [xml]$results = Execute-HTTPGetCommand "http://api.tr.im/api/trim_url.xml?url=$longurl"; $shorturl = $results.trim.url; } "bit.ly" { if ( !$BITLY_LOGIN -or !$BITLY_KEY ) { Write-Error "ERROR: You must configure your bit.ly LOGIN and APIKEY!" exit; } else { $results = Execute-HTTPGetCommand ` "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=$script:BITLY_LOGIN&apiKey=$script:BITLY_KEY"; $shorturl = $results.Split("`n")[7].Split("""")[3]; } } default { $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl"; } } } else { Write-Host "ERROR: Usage: Shrink-Url -longurl http://www.foo.com"; } $shorturl; } #---------------------------------------------------------------------------- # function Execute-HTTPGetCommand #---------------------------------------------------------------------------- function Execute-HTTPGetCommand() { param([string] $url = $null); $user_agent = "PoshShrink"; if ( $url ) { $request = [System.Net.HttpWebRequest]::Create($url); $request.UserAgent = $user_agent; $request.Credentials = [System.Net.CredentialCache]::DefaultCredentials; if ( $request.Proxy ) { $request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials; } $response = $request.GetResponse(); $rs = $response.GetResponseStream(); [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs; $sr.ReadToEnd(); } } Shrink-Url -longurl $longurl -provider $provider;