The Wayback Machine - https://web.archive.org/web/20171110011913/http://php.sys-con.com:80/node/2178554

Welcome!

PHP Authors: Liz McMillan, Carmen Gonzalez, Hovhannes Avoyan, Lori MacVittie, Trevor Parsons

Blog Feed Post

Shrink-Url – Use PowerShell To Shrink Your Urls

poshShrinkShrinking your Url’s is all the rage nowadays.  If you are on Twitter, then odds are you have used one.  Despite CodingHorror’s distaste for them in his recent blog post on Url Shorteners: Destroying the Web since 2002, they are a fact of life when we live in a world of 140 character status updates.

So what’s a URL shrinking service anyway?  Well, to put it simply, you supply them with a URL, they then supply you with a shorter URL containing a lookup “key”.  When future requests are made to this shorter URL, connections are routed to that services website where they convert the short URL to the original URL and issue a HTTP Redirect back to your browser to send you off to the original long url website.

So, what’s a guy, or gal, to do if they want to set their status programmatically on Twitter, Facebook, FriendFeed, or the other gazillion social networking sites out there today and need to post a very long URL?  Most of these shrinking services offer API’s to access their “shrinking” services so it’s just a matter of digging into the various APIs to get them implemented.  In my original PowerShell Twitter library PoshTweet, I included support for a couple for shrinking URL’s through a couple of shrinking services but I figured it would be good to separate that functionality out into it’s own library. 

So, here’s my first stab at a generic URL shortening library for PowerShell.  I’ve included the following services:

Some of these services have advanced features such as statistical reporting on all your submitted links.  Those services require users to create accounts and register for API keys.  Bit.ly is one of them, so if you want to use that service, you will have to supply this script with your bit.ly username and API key with the Set-BitlyServiceInfo function.

So, here’s the script, hope you all enjoy!

 

   1: param(
   2:   [string]$longurl = $null,
   3:   [string]$provider = "tinyurl"
   4: );
   5:  
   6: $script:DIGG_APPKEY = "https://devcentral.f5.com/PoshShrink";
   7: $script:BITLY_LOGIN = "";
   8: $script:BITLY_KEY = "";
   9:  
  10: #----------------------------------------------------------------------------
  11: # function Set-BitlyServiceInfo
  12: #----------------------------------------------------------------------------
  13: function Set-BitlyServiceInfo()
  14: {
  15:   param(
  16:     [string]$login = $null,
  17:     [string]$key = $null
  18:   );
  19:   if ( $login -and $key )
  20:   {
  21:     $script:BITLY_LOGIN = $login;
  22:     $script:BITLY_KEY = $key;
  23:   }
  24:   else
  25:   {
  26:     Write-Error "Usage: Set-BitlyServiceInfo -login login -key key";
  27:   }
  28: }
  29:  
  30: #----------------------------------------------------------------------------
  31: # function Shrink-Url
  32: #----------------------------------------------------------------------------
  33: function Shrink-Url()
  34: {
  35:   param(
  36:     [string]$longurl = $null,
  37:     [string]$provider = "tinyurl"
  38:   );
  39:   
  40:   $shorturl = $null;
  41:   if ( $longurl )
  42:   {
  43:     switch ($provider.ToLower())
  44:     {
  45:       "is.gd" {
  46:         $shorturl = Execute-HTTPGetCommand "http://is.gd/api.php?longurl=$longurl";
  47:       }
  48:       "tinyurl" {
  49:         $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
  50:       }
  51:       "snurl" {
  52:         $shorturl = Execute-HTTPGetCommand "http://snipr.com/site/snip?r=simple&link=$longurl";
  53:       }
  54:       "digg" {
  55:         [xml]$results = Execute-HTTPGetCommand `
  56:           "http://services.digg.com/url/short/create?url=${longurl}&appkey=${script:DIGG_APPKEY}";
  57:         $shorturl = $results.shorturls.shorturl.short_url;
  58:       }
  59:       "tr.im" {
  60:         [xml]$results = Execute-HTTPGetCommand "http://api.tr.im/api/trim_url.xml?url=$longurl";
  61:         $shorturl = $results.trim.url;
  62:       }
  63:       "bit.ly" {
  64:         if ( !$BITLY_LOGIN -or !$BITLY_KEY )
  65:         {
  66:           Write-Error "ERROR: You must configure your bit.ly LOGIN and APIKEY!"
  67:           exit;
  68:         }
  69:         else
  70:         {
  71:           $results = Execute-HTTPGetCommand `
  72:             "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=$script:BITLY_LOGIN&apiKey=$script:BITLY_KEY";
  73:           $shorturl = $results.Split("`n")[7].Split("""")[3];
  74:         }
  75:       }
  76:       default {
  77:         $shorturl = Execute-HTTPGetCommand "http://tinyurl.com/api-create.php?url=$longurl";
  78:       }
  79:     }
  80:   }
  81:   else
  82:   {
  83:     Write-Host "ERROR: Usage: Shrink-Url -longurl http://www.foo.com";
  84:   }
  85:   $shorturl;
  86: }
  87:  
  88: #----------------------------------------------------------------------------
  89: # function Execute-HTTPGetCommand
  90: #----------------------------------------------------------------------------
  91: function Execute-HTTPGetCommand()
  92: {
  93:   param([string] $url = $null);
  94:   
  95:   $user_agent = "PoshShrink";
  96:   
  97:   if ( $url )
  98:   {
  99:     $request = [System.Net.HttpWebRequest]::Create($url);
 100:     $request.UserAgent = $user_agent;
 101:     $request.Credentials = [System.Net.CredentialCache]::DefaultCredentials;
 102:     if ( $request.Proxy )
 103:     {
 104:       $request.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;
 105:     }
 106:     $response = $request.GetResponse();
 107:     $rs = $response.GetResponseStream();
 108:     [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
 109:     $sr.ReadToEnd();
 110:   }
 111: }
 112:  
 113: Shrink-Url -longurl $longurl -provider $provider;

 

You can download the full contents for this script here: Shrink-Url.ps1

Read the original blog entry...

More Stories By Joe Pruitt

Joe Pruitt is a Principal Strategic Architect at F5 Networks working with Network and Software Architects to allow them to build network intelligence into their applications.

@ThingsExpo Stories
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, will discuss how from store operations...
SYS-CON Events announced today that ECS Refining to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. With rapid advances in technology, the proliferation of consumer and enterprise electronics, and the exposure of unethical e-waste disposal methods, there is an increasing demand for responsible electronics recycling and reuse services. As a pioneer in the electronics recycling and ...
SYS-CON Events announced today that CA Technologies has been named “Platinum Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY, and the 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business – from apparel to energy – is being rewritten by software. From ...
SYS-CON Events announced today that Nirmata to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Nirmata provides comprehensive policy-based automation for deploying, operating, and optimizing containerized applications across clouds, via easy-to-use, intuitive interfaces. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of applicati...
Digital Transformation (DX) is not a "one-size-fits all" strategy. Each organization needs to develop its own unique, long-term DX plan. It must do so by realizing that we now live in a data-driven age, and that technologies such as Cloud Computing, Big Data, the IoT, Cognitive Computing, and Blockchain are only tools. In her general session at 21st Cloud Expo, Rebecca Wanta will explain how the strategy must focus on DX and include a commitment from top management to create great IT jobs, monit...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, will discuss how they bu...
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, will discuss some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he’ll go over some of the best practices for structured team migrat...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, will lead you through the exciting evolution of the cloud. He'll look at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering ...