Sometimes, when I have nothing better to do, I kill some time giving Todd Klindt and Shane Young a hard time during their podcast. You should join me sometime. Anyway, during a recent show Todd mentioned a bit of PowerShell code he put together to resolve short links. You see these all the time in your Twitter stream and others. What is that link really pointing to? Todd's solution was to use Invoke-Webrequest to access the link and then look at the uri from the response. I took his code, as I usually do, and ran with it - straight into a simple WPF form.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Here's the essence of Todd's original code.
$short = https://bit.ly/PSMoL3 $wr = Invoke-WebRequest -UseBasicParsing –Uri $short $wr.baseresponse.ResponseUri.AbsoluteURI
If you run this you should get the result https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition. I figured is might be nice to have a form where I could copy and paste a link from Twitter and get the resolved link. So I put together a WPF-based script that uses a stack panel to display a simple input box.
Clicking OK runs the code in the associated scriptblock to resolve the link using Invoke-Webrequest and then closes the form. The function writes the result, which is an object, back to the pipeline.
Depending on the resolved link you may not see all of it, but hopefully enough to know whether you trust it. Or run the Resolve-ShortLink function and pipe it to Format-List.
You can get the code for my function from GitHub.
If you have problems or suggestions please post an issue on the gist page.
I hope you'll give it a try. Now if you'll excuse me I have some online heckling to attend to.
I like reviewing how other folks approach thing, and the in cool and all but, why the use of a form for this, just to be thrown back to the console, when you can just use a function with a param and stay in the console (still using copy and paste)?
Using this function…
Function Resolve-TinyUrl
{
[CmdletBinding()]
[Alias(‘rturl’)]
Param(
[Parameter(Mandatory=$true,
HelpMessage=”Enter a valid Url”)]
[ValidatePattern(‘(?# Enter a valid Url)^(ftp:|http:|https:|ftp:)(//.*)’)]
[string]$ShortUrl
)
“The URL entered – $ShortUrl is valid”
Start-Sleep -Seconds 1
$longURL = (Invoke-WebRequest -UseBasicParsing –Uri $ShortUrl).baseresponse.ResponseUri.AbsoluteURI
“The ShortUrl ‘$shortUrl’ Resolves to -> ‘$longUrl'”
}
… or for a bit more info or control aspects, this one
Function Expand-Url
{
[CmdletBinding()]
[Alias(‘eurl’)]
Param(
[Parameter(Mandatory=$true,
HelpMessage=”Enter a valid Url”)]
[ValidatePattern(‘(?# Enter a valid Url)^(ftp:|http:|https:|ftp:)(//.*)’)]
[string]$URLRaw
)
“The URL entered – $URLRaw is valid”
Start-Sleep -Seconds 1
(Invoke-WebRequest -Uri $URLRaw -MaximumRedirection 0 -ErrorAction Ignore).Headers.Location
$result = [System.uri](Invoke-WebRequest -Uri $URLRaw -MaximumRedirection 0 -ErrorAction Ignore).Headers.Location
“Port: $($result.port)”
“Page Authority: $($result.Authority)”
“Served using: $($result.Scheme)”
}
I agree it is just as easy to do this from the console. That’s what Todd Klindt was originally doing. I simply felt challenged to see if I could whip up a WPF script in the time it took him to talk about his code. There’s no added benefit of doing it with a form. And actually, if I were to continue with this I’d display result directly in the form. Maybe even write the code to use a runspace so it can run continuously. Lots of possibilities.