Hacer que los enlaces cortos sean largos con PowerShell y WPF
A veces, cuando no tengo nada mejor que hacer, mato el tiempo haciéndoles pasar un mal rato a Todd Klindt y Shane Young durante su podcast. Deberías unirte a mí alguna vez. De todos modos, durante un programa reciente, Todd mencionó un poco de código PowerShell que creó para resolver enlaces cortos. Los ves todo el tiempo en tu flujo de Twitter y otros. ¿A qué apunta realmente ese vínculo? La solución de Todd fue utilizar Invoke-Webrequest para acceder al enlace y luego mirar el uri de la respuesta. Tomé su código, como suelo hacer, y lo ejecuté, directamente en un formulario WPF simple.
Aquí está la esencia del código original de Todd.
$short = https://bit.ly/PSMoL3
$wr = Invoke-WebRequest -UseBasicParsing -Uri $short
$wr.baseresponse.ResponseUri.AbsoluteURI
Si ejecuta esto, debería obtener el resultado https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition. Pensé que sería bueno tener un formulario donde pudiera copiar y pegar un enlace de Twitter y obtener el enlace resuelto. Así que preparé un script basado en WPF que usa un panel de pila para mostrar un cuadro de entrada simple.
Al hacer clic en Aceptar, se ejecuta el código en el bloque de script asociado para resolver el vínculo mediante Invoke-Webrequest y luego se cierra el formulario. La función escribe el resultado, que es un objeto, en la canalización.
Dependiendo del enlace resuelto, es posible que no lo veas completo, pero con suerte lo suficiente para saber si confías en él. O ejecute la función Resolve-ShortLink y canalícela a Format-List.
Puede obtener el código de mi función en GitHub.
Resolve-ShortLink.ps1:
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-ShortLinkSi tiene problemas o sugerencias, publique un problema en la página principal.
Espero que lo pruebes. Ahora, si me disculpan, tengo que atender algunos abucheos en línea.