Created
July 9, 2018 14:27
-
-
Save jdhitsolutions/13974a0e5391198196d7cdd57fe7f121 to your computer and use it in GitHub Desktop.
A PowerShell WPF form to resolve shortlinks
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
Function Resolve-ShortLink { | |
[cmdletbinding()] | |
Param([string]$Url) | |
Add-Type -AssemblyName PresentationFramework | |
Add-Type –AssemblyName PresentationCore | |
Add-Type –AssemblyName WindowsBase | |
$form = New-Object System.Windows.Window | |
$stack = New-object System.Windows.Controls.StackPanel | |
#define what it looks like | |
$form.Title = "Link Lookup" | |
$form.Height = 150 | |
$form.Width = 350 | |
$label = New-Object System.Windows.Controls.Label | |
$label.Content = " Enter a short url" | |
$label.HorizontalAlignment = "left" | |
$stack.AddChild($label) | |
$inputbox = New-Object System.Windows.Controls.TextBox | |
$inputbox.Width = 300 | |
$inputbox.HorizontalAlignment = "center" | |
$inputbox.text = $url | |
$stack.AddChild($inputbox) | |
$space = new-object System.Windows.Controls.Label | |
$space.Height = 10 | |
$stack.AddChild($space) | |
$btn = New-Object System.Windows.Controls.Button | |
$btn.Content = "_OK" | |
$btn.Width = 65 | |
$btn.HorizontalAlignment = "center" | |
$btn.VerticalAlignment = "bottom" | |
#add an event handler | |
$btn.Add_click( { | |
#write-host "resolving $($inputbox.text)" | |
$resolve = (Invoke-WebRequest -UseBasicParsing –Uri $($inputbox.Text)).baseresponse.ResponseUri.AbsoluteURI | |
$script:result = [pscustomobject]@{ | |
ShortUrl = $inputbox.Text | |
Resolved = $resolve | |
} | |
$form.Close() | |
}) | |
$stack.AddChild($btn) | |
$space2 = new-object System.Windows.Controls.Label | |
$space2.Height = 10 | |
$stack.AddChild($space2) | |
$btn2 = New-Object System.Windows.Controls.Button | |
$btn2.Content = "_Cancel" | |
$btn2.Width = 65 | |
$btn2.HorizontalAlignment = "center" | |
$btn2.VerticalAlignment = "bottom" | |
#add an event handler | |
$btn2.Add_click( { | |
$script:result = $null | |
$form.Close() | |
}) | |
$stack.AddChild($btn2) | |
#add the stack to the form | |
$form.AddChild($stack) | |
#show the form | |
$inputbox.Focus() | Out-Null | |
$form.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen | |
$form.ShowDialog() | Out-Null | |
if ($script:result) { | |
#write result to the pipeline | |
$script:result | |
} | |
} | |
Set-Alias -Name rsl -Value Resolve-ShortLink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment